Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl rewrite_result for ArmWrapper #6239

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

ding-young
Copy link
Contributor

Tracked by #6206

Description

This pr implements rewrite_result for ArmWrapper (wrapper for ast Arm), while modifying functions used for formatting match expression like rewrite_match, rewrite_match_arm and so on.

Comment on lines 595 to 605
let cond_shape = Shape::indented(shape.indent.block_indent(context.config), context.config)
.offset_left(3)
.and_then(|s| s.sub_width(5));
if let Some(cond_shape) = cond_shape {
if let Some(cond_str) = guard.rewrite(context, cond_shape) {
return Some(format!(
"{}if {}",
cond_shape.indent.to_string_with_newline(context.config),
cond_str
));
}
}

None
.and_then(|s| s.sub_width(5))
.max_width_error(shape.width, guard.span)?;
let cond_str = guard.rewrite_result(context, cond_shape)?;
Ok(format!(
"{}if {}",
cond_shape.indent.to_string_with_newline(context.config),
cond_str
))
} else {
Some(String::new())
Ok(String::new())
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made some small refactoring here: we can return RewriteError early when cond_shape exceeds the max width and when it fails to rewrite the arm guard. The original source code returns Some(..) only when both conditions are not met.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. I think this change is sound

@ytmimi ytmimi added the GSoC Google Summer of Code label Jul 17, 2024
@ding-young ding-young marked this pull request as ready for review July 18, 2024 00:08
src/matches.rs Outdated
Comment on lines 271 to 282
shape.sub_width(7 + label_len)?.offset_left(pipe_offset)?
shape
.sub_width(7 + label_len)
.max_width_error(shape.width, arm.span)?
.offset_left(pipe_offset)
.max_width_error(shape.width, arm.span)?
Copy link
Contributor

@ytmimi ytmimi Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunate that we have to call .max_width_error twice here, but it makes sense. I think we should eventually change Shap::sub_width and other methods to return Result<Shape, RewriteError>, but that can wait for a future PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can replace that with .sub_width().and_then(|| offset_left()).max_width_error() to avoid calling .max_width_error twice, but there might be a better approach.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I think that would work well here!

@ytmimi
Copy link
Contributor

ytmimi commented Jul 18, 2024

Running the Diff-Check job now.

Edit: The job passed ✅

@ytmimi ytmimi merged commit ea02de2 into rust-lang:master Jul 18, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC Google Summer of Code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants