fix: prevent panic when formatting complex generics#6630
Open
avrabe wants to merge 1 commit intorust-lang:mainfrom
Open
fix: prevent panic when formatting complex generics#6630avrabe wants to merge 1 commit intorust-lang:mainfrom
avrabe wants to merge 1 commit intorust-lang:mainfrom
Conversation
Handle cases where rewrite_generics fails due to long generic constraints by implementing a fallback to infinite width and preserving original formatting when necessary.
Contributor
|
Hi, I just came across your PR. Would it be alright if I try it out to see if #6396 fixes the unwrap error in that test case? (This seems orthogonal to the mentioned PR though, so just to verify whether the issue is duplicate or not) |
Author
|
@ding-young Sure, would be good to see. https://github.com/pulseengine/wrt is where my problem originate from. My patch makes this repository format again. Otherwise it actually stripped away code. |
2 tasks
TheLostLambda
added a commit
to TheLostLambda/mzdata
that referenced
this pull request
Sep 30, 2025
This is now a different, newer PR that supports the 2024 edition: rust-lang/rustfmt#6630
mobiusklein
pushed a commit
to mobiusklein/mzdata
that referenced
this pull request
Oct 1, 2025
#31) * style: run (patched) rustfmt I was originally perplexed as to why the crate wasn't already formatted, but quickly found out why... Though the PR seems to have languished for some time, I've run a patched version of rustfmt that completes without crashing: rust-lang/rustfmt#6396 * style: fix `mismatched_lifetime_syntaxes` lint * style: remove unnecessary macro imports This is a strange case — I had to look this up myself! Because you have a `#[macro_use]` above the `params` module in `lib.rs`, all of the macros in `params.rs` are automatically made available crate-wide. This import of the macro in `scan_properties.rs` is therefore redundant! The same logic applies to `impl_param_described`. https://lukaswirth.dev/tlborm/decl-macros/minutiae/scoping.html * style: fix clippy `derivable_impls` For `enum`s, you can add a tag and `#[derive(Default)]` instead of manually implementing the same code: https://doc.rust-lang.org/std/default/trait.Default.html#enums * style: fix clippy `single_match` * style: fix clippy `redundant_closure` * style: clippy fix `unnecessary_unwrap` * style: clippy fix `manual_is_multiple_of` NOTE: This method was added in a recent version of Rust (1.87.0), so changing to this code means that our minimum supported Rust version would become that! If you'd rather keep compatibility with older-than-stable versions of Rust, we should leave this unchanged! * chore(edition): run `cargo update` * style(edition): run `cargo --fix edition` Performs some automatic migration to the 2024 edition of Rust. Will be followed by a cleanup commit. * style(edition): bump to Rust 2024 * style(edition): reformat using (patched) rustfmt This is now a different, newer PR that supports the 2024 edition: rust-lang/rustfmt#6630 * style(edition): `expr_2021` -> `expr` in macros The 2024 edition changed `expr`s to accept `const` and `_` expressions, but because no macros in `mzdata` included any `const` or `_` keywords in their syntax, no change needs to be made. * style(edition): `match` back to `if let` None of the migrated code seemed to depend on any exact `Drop` timing, so the overly-cautious `if let` -> `match` migration was unnecessary. * style(edition): change `gen` to `this_generation` In the 2024 edition, `gen` became a reserved Rust keyword. Using `r#gen` is still possible, but probably not best practice, so I've renamed the old `gen` variables. * style: fix clippy `let_and_return` * style: fix clippy `collapsible_if`
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 #6571
Problem
rustfmt panics with
Option::unwrap() on a None valuewhen formatting enums with very long generic type constraints. This occurs when the generic constraints exceed the available width budget (~80-100 characters), causingrewrite_genericsto fail andformat_genericsto returnNone, which is then unwrapped.Solution
This PR implements a three-tier fallback strategy in
format_generics:shape.infinite_width()Changes
format_genericsfunction insrc/items.rsto handle width budget exhaustion gracefullyissue-6571.rsto prevent future regressionsTesting
This fix ensures rustfmt can process files with complex generic constraints without crashing, while maintaining proper formatting where possible.