rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

Fix generic default type assignment panic

Open MarcusGrass opened this issue 1 year ago • 0 comments

Fixes https://github.com/rust-lang/rustfmt/issues/6052

The cause of the issue is when there's an = type assignment with the generics that overflows a line.

The budget given to def.rewrite is too low, this causes a cascading Option::None which eventually is unwrapped and causes the panic.

One more interaction caused another Option::None, the first was the formatting mentioned above, the second is that overflow.rs hardcodes to leave the last entry, which causes nothing to be written on a single entry, as was my test-case. I changed that logic to leave_last if greater than 1 length. I'm not familiar enough with the codebase to tell whether that could cause problems that aren't covered by tests.

I tried out two solutions to the formatting issue:

  1. Just give a budget of usize::MAX, then the user gets a line too long instead of a panic.
  2. Split the eq_str lhs and rhs into separate lines if it's too long, if it's still too long after that the user gets a line too long. Still usinge usize::MAX as budget.

Ending up going with the second one, since that seems nicer, it's very easy to change to the first one though.

E: Added the reported example as is as a test. Also ran it on tokio, cause it has a bunch of code, to check for unintended consequences, not the best way to check for those apart from the tests, but it's something

MarcusGrass avatar Feb 22 '24 15:02 MarcusGrass