Skip to content

A long string in a match arm breaks formatting for the whole match expression #4797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
arnaudgolfouse opened this issue Apr 12, 2021 · 4 comments
Labels
1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release a-matches match arms, patterns, blocks, etc help wanted p-low

Comments

@arnaudgolfouse
Copy link

Describe the bug

If an arm of a match expression contains a string with >= 87 characters, formatting fails on the whole match

To Reproduce

match 0 {
    0 => {"hello"} 1 => { ""}
      _ => {"veeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string"}
};

Expected behavior

 match 0 {
    0 => "hello",
    1 => "",
    _ => {
        "veeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string"
    }
};

If the third string is less than 86 characters, formatting works as expected.

Meta

  • rustfmt version: rustfmt 1.4.36-stable (7de6968 2021-02-07)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt, cargo-fmt, and rust-analyzer formatting in VSCode.
@arnaudgolfouse arnaudgolfouse added the bug Panic, non-idempotency, invalid code, etc. label Apr 12, 2021
@calebcartwright calebcartwright added 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release help wanted and removed bug Panic, non-idempotency, invalid code, etc. labels Apr 12, 2021
@calebcartwright
Copy link
Member

Thanks for reaching out, this is in the same boat as #4800 and is also fixed in source (apparently) but hasn't been pulled into a release branch.

For anyone interested in working on this please note that the fix would need to be versioned gated to avoid violating the formatting stability guarantee. It was an intentional decision early on in rustfmt to bail in cases like this when it's completely impossible to format an element within the max_width constraint, so even though it's preferable now to format anyway (and let users turn on config options like error_on_line_overflow) we still have to gate that change

@ChinYing-Li
Copy link
Contributor

@calebcartwright I'd like to help with back-porting this.

@calebcartwright
Copy link
Member

@calebcartwright I'd like to help with back-porting this.

Brilliant, thanks!

@ChinYing-Li
Copy link
Contributor

Should I back-port to branch 1.4.36, or 1.4.37?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release a-matches match arms, patterns, blocks, etc help wanted p-low
Projects
None yet
Development

No branches or pull requests

4 participants