Skip to content

reserved width for unsafe in match arm pattern - #7097

Open
AsthaMishra wants to merge 2 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-6848
Open

reserved width for unsafe in match arm pattern#7097
AsthaMishra wants to merge 2 commits into
rust-lang:mainfrom
AsthaMishra:fix-issue-6848

Conversation

@AsthaMishra

@AsthaMishra AsthaMishra commented Sep 3, 2026

Copy link
Copy Markdown
Contributor
  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

Issue : unsafe is a block with label None and when patterns code executes and label: None is found, execution goes to fallback arm where only 5 column spaces are reserved (which is correct for async, const, gen and try blocks) but unsafe needs 12 column spaces, this is what causing max_width violation and when run with --config error_on_line_overflow=true it does give line overflow error for max-width 80

 // Patterns
    let pat_shape = match &arm.body.as_ref().unknown_error()?.kind {
        ast::ExprKind::Block(_, Some(label)) => {
            // Some block with a label ` => 'label: {`
            // 7 = ` => : {`
            let label_len = label.ident.as_str().len();
            shape
                .sub_width(7 + label_len, arm.span)?
                .offset_left(pipe_offset, arm.span)?
        }
        _ => {
            // 5 = ` => {`
            shape
                .sub_width(5, arm.span)?
                .offset_left(pipe_offset, arm.span)?
        }
    };

Fix : added an arm for unsafe block to reserve required space for unsafe

 ast::ExprKind::Block(block, None) if is_unsafe_block(block) => {
            // 12 = ` => unsafe {`
            shape
                .sub_width(12, arm.span)?
                .offset_left(pipe_offset, arm.span)?
        }

Fixes : #6848

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

Copy link
Copy Markdown
Contributor Author

one question? should this change be gated?

@ytmimi

ytmimi commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

@AsthaMishra Thank you for working on this. Yeah, let's gate this fix on style_edition=2027

@ytmimi

ytmimi commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Also are we sure that things are correct for other types of blocks? Let's add test cases for them just to be sure.

try {}
}
_ => {}
}

@ytmimi ytmimi Sep 4, 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.

Looks like we're forcing const, async, gen, and try blocks to be wrapped in an outer block. Does that always happen or just when we can't fit everything onto one line?

unsafe 6 chars
const 5 chars
async 5 chars
gen 3 chars
try 3 chars

All of these are getting tested against the same ( ExampleTypeX::VariantAlphaSampleXYZ, ExampleTypeX::VariantBetaXYZ) tuple. To make sure we're exhaustively testing this I would like to test the following cases for each type of block:

  1. 1 char below the max_width limit when accounting for the pattern, =>, keyword, {, and any whitespace in between..
  2. everything properly fits on 1 line at exactly the max_width limit when accounting for the pattern, =>, keyword, {, and any whitespace in between..
  3. 1 char over the max_width limit when accounting for the pattern, =>, keyword, {, and any whitespace in between.

View changes since the review

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

Labels

S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

max_width not obeyed in match arm tuple

3 participants