Skip to content

Shrink the const block shape by the width of the const keyword - #7073

Closed
devangpratap wants to merge 1 commit into
rust-lang:mainfrom
devangpratap:fix-7055-const-block-shape
Closed

Shrink the const block shape by the width of the const keyword#7073
devangpratap wants to merge 1 commit into
rust-lang:mainfrom
devangpratap:fix-7055-const-block-shape

Conversation

@devangpratap

Copy link
Copy Markdown

Fixes #7055.

What

format_expr's ConstBlock arm calls rewrite_block with the full shape and then prepends the literal const , so the block is laid out with six more columns than it actually has. This shrinks the shape by those six columns first.

unsafe { ... } and a plain { ... } are unaffected today because their prefix is built inside rewrite_block_inner, where rewrite_single_line_block already does shape.offset_left(last_line_width(prefix)).

offset_left_opt(..).unwrap_or(shape) rather than offset_left(..)?: a block body's indentation is fixed by the enclosing indent, so there is no alternative layout to fall back to, and erroring on a narrow shape would only discard the formatting of the whole statement. Same idiom as src/items.rs:2677.

Why it is gated

The issue reports the method-receiver case, where the over-wide rewrite is rejected by the chain and the expression is silently emitted verbatim. But outside a chain the same over-wide rewrite is accepted, so at the default max_width of 100 rustfmt today normalises

    const {  S::new( "<77 a's>" )  } ;

into a 102 column line, idempotently. That is stable formatting, so the change is gated behind style_edition=2027, following bdab101 ("Check if function return type fits before rewriting", #6831).

One consequence worth flagging: gating means the reported symptom stays broken on style editions 2015 through 2024. If you would rather have it ungated on the grounds that the receiver case emits nothing at all today, I am happy to drop the gate and the 2021/2024 test pairs.

Tests

  • issue_7055_style_edition_{2021,2024,2027}.rs, source and target. 2021 and 2024 are byte-identical to each other and to pre-fix output. 2027 covers the reported receiver case, the boundary case just below it that must not change, the bare statement case that motivates the gate, a nested const block, and a block with inner attributes (rustfmt removes inner attributes from inline const blocks #6158).
  • issue_7055_narrow_max_width.rs, a const block with fewer than six columns of budget at max_width=30, guarding against bailing out and emitting verbatim.

cargo test is green: 234 system tests, no existing target file changed, and rustfmt still bootstraps on its own source.

fixes rust-lang#7055

`format_expr`'s `ConstBlock` arm rewrote the block at the full shape and
then prepended `const `, so the block was laid out as if it had six more
columns than it really did. An `unsafe` block is unaffected because its
prefix is built inside `rewrite_block_inner`, where
`rewrite_single_line_block` subtracts the prefix width from the shape.

The visible symptom is that a `const` block used as the receiver of a
method call is silently left unformatted: the over-wide rewrite is
rejected by the chain, so the whole expression falls back to the
original snippet with no diff and exit code 0.

Gated behind style_edition=2027 because outside a chain the over-wide
rewrite was accepted and emitted, so it is stable formatting.
@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 26, 2026
@ytmimi

ytmimi commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Please don't open new PRs for issues that already have one open (#7065). This is unhelpful noise.

@ytmimi ytmimi closed this Aug 26, 2026
@rustbot rustbot removed the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants