Skip to content

Reserve width for const when formatting inline const blocks - #7065

Open
AsthaMishra wants to merge 3 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-7055
Open

Reserve width for const when formatting inline const blocks#7065
AsthaMishra wants to merge 3 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-7055

Conversation

@AsthaMishra

Copy link
Copy Markdown
Contributor

for inline const block, we format the { ... } part first and only afterwards prepend const . 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_width and rustfmt reported an internal error:

error[internal]: line formatted, but exceeded maximum width (maximum: 100, found: 102)

Fix: take the width of const off the available space before formatting the block, so it plans accordingly.

Fixes #7055

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 24, 2026

@ytmimi ytmimi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These formatting changes need to be gated. Also, I think we should review the test cases to make sure that they're not redundant.

View changes since this review

Comment thread src/expr.rs Outdated
Comment on lines +189 to +191

// Shrink the shape by `"const ".len()` before rewriting the block
let shape = shape.offset_left(6, expr.span)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we need to gate this fix for style_edition=2027

Comment on lines +11 to +27
fn main() {
const {
S::new(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
}
.go();
}

fn second_issue() {
const {
S::new(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
}
.go();
} No newline at end of file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the difference between main and second_issue?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There's a space after S::new( in second_issue. #7055 should provide context

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@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.

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 24, 2026
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ytmimi ytmimi added the B-next-style-edition-gate Blocked: can only be done over a style edition; needs a style edition gate label Aug 24, 2026
Comment thread src/expr.rs
Comment on lines +189 to +194
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
};

@ytmimi ytmimi Aug 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

@AsthaMishra AsthaMishra Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

try , async , and gen blocks pass their prefix into rewrite_single_line_block , so these works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

B-next-style-edition-gate Blocked: can only be done over a style edition; needs a style edition gate S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

const {} as a method call receiver is silently left unformatted

4 participants