Fix hybrid formatting of struct patterns containing ..#6875
Open
Souradip121 wants to merge 1 commit intorust-lang:mainfrom
Open
Fix hybrid formatting of struct patterns containing ..#6875Souradip121 wants to merge 1 commit intorust-lang:mainfrom
..#6875Souradip121 wants to merge 1 commit intorust-lang:mainfrom
Conversation
When a struct pattern's fields were within struct_lit_width but within
4 chars of it, appending ", .." would push fields_str.len() over
one_line_width in wrap_struct_field, producing a hybrid format:
let X {
field1, field2x, ..
} = x;
The root cause: struct_lit_shape already subtracts ellipsis_str.len()
from h_shape.width (via suffix_width), but after appending ", .." to
fields_str the wrap_struct_field check compared the augmented string
against the already-reduced one_line_width threshold.
Fix by adjusting one_line_width to account for the appended ellipsis
before passing it to wrap_struct_field. Update affected test targets
and rustfmt source files formatted by the new behaviour.
Fixes rust-lang#6827
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.
Summary
struct_lit_shapecomputesh_shape.widthalready reduced bylen(", ..")to reserve space for the ellipsis suffix. Afterwrite_list, we then append", .."tofields_str, making it 4 chars longer.wrap_struct_fieldwas comparing this augmented string against the original (already-reduced)one_line_width, so patterns with fields near the width budget got incorrectly flagged as too wide and wrapped vertically.adjusted_one_line_width = one_line_width + ellipsis_str.len()before thewrap_struct_fieldcall, so the threshold matches what's actually infields_str.single_line_let_else_max_widthtest targets andmatch.rswhere the fix causes struct patterns with..to stay on one line.src/imports.rsandsrc/visitor.rsto match the new output (required by the self-test).Test Plan
cargo testpassestests/source/issue-6827.rscovers all three cases from the issue: single-line, previously-hybrid (now single-line), and genuinely-too-wide (vertical)Fixes #6827