Reserve width for const when formatting inline const blocks - #7065
Reserve width for const when formatting inline const blocks#7065AsthaMishra wants to merge 3 commits into
const when formatting inline const blocks#7065Conversation
|
|
||
| // Shrink the shape by `"const ".len()` before rewriting the block | ||
| let shape = shape.offset_left(6, expr.span)?; |
There was a problem hiding this comment.
I think we need to gate this fix for style_edition=2027
| fn main() { | ||
| const { | ||
| S::new( | ||
| "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
| ) | ||
| } | ||
| .go(); | ||
| } | ||
|
|
||
| fn second_issue() { | ||
| const { | ||
| S::new( | ||
| "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
| ) | ||
| } | ||
| .go(); | ||
| } No newline at end of file |
There was a problem hiding this comment.
What's the difference between main and second_issue?
There was a problem hiding this comment.
There's a space after S::new( in second_issue. #7055 should provide context
There was a problem hiding this comment.
@astral4 thanks for pointing that out. @AsthaMishra the trailing whitespace isn't so obvious. Instead of naming it second_issue Maybe you could name the case something like removes_trailing_whitespace, which would make the intent clearer in my opinion.
|
Reminder, once the PR becomes ready for a review, use |
b8b2c03 to
f6b8d36
Compare
| let shape = if context.config.style_edition() >= StyleEdition::Edition2027 { | ||
| // Shrink the shape by `"const ".len()` before rewriting the block | ||
| shape.offset_left(6, expr.span)? | ||
| } else { | ||
| shape | ||
| }; |
There was a problem hiding this comment.
Thanks for adding the gate. This looks right to me, but I want to confirm something first before moving forward. #7055 mentioned that unsafe blocks didn't have this problem. I'd like to understand why that's the case. Maybe there's something going on inside rewrite_block where the shape is properly updated?
There are also try, gen, and async blocks. Might be good to check that we're handling those correctly too.
There was a problem hiding this comment.
if there is unsafe , we get it in prefix - let prefix = block_prefix(context, block, shape)?; and in function rewrite_single_line_block result contains it
let result = format!("{prefix}{label_str}{{ {expr_str} }}");
if result.len() <= shape.width && !result.contains('\n') {
return Ok(result);
}
but for const - prefix is empty
There was a problem hiding this comment.
try , async , and gen blocks pass their prefix into rewrite_single_line_block , so these works fine
for inline const block, we format the
{ ... }part first and only afterwards prependconst. so the block makes its decision without knowing that six characters were about to be added.when block just barely fit, those six characters pushed the emitted line past
max_widthand rustfmt reported an internal error:error[internal]: line formatted, but exceeded maximum width (maximum: 100, found: 102)Fix: take the width of
constoff the available space before formatting the block, so it plans accordingly.Fixes #7055