Shrink the const block shape by the width of the const keyword - #7073
Closed
devangpratap wants to merge 1 commit into
Closed
Shrink the const block shape by the width of the const keyword#7073devangpratap wants to merge 1 commit into
const keyword#7073devangpratap wants to merge 1 commit into
Conversation
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.
Contributor
|
Please don't open new PRs for issues that already have one open (#7065). This is unhelpful noise. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #7055.
What
format_expr'sConstBlockarm callsrewrite_blockwith the fullshapeand then prepends the literalconst, 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 insiderewrite_block_inner, whererewrite_single_line_blockalready doesshape.offset_left(last_line_width(prefix)).offset_left_opt(..).unwrap_or(shape)rather thanoffset_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 assrc/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_widthof 100 rustfmt today normalisesinto 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 nestedconstblock, and a block with inner attributes (rustfmt removes inner attributes from inline const blocks #6158).issue_7055_narrow_max_width.rs, aconstblock with fewer than six columns of budget atmax_width=30, guarding against bailing out and emitting verbatim.cargo testis green: 234 system tests, no existing target file changed, and rustfmt still bootstraps on its own source.