Skip to content

fix: prevent panic when formatting complex generics #6630

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3325,7 +3325,20 @@ fn format_generics(
used_width: usize,
) -> Option<String> {
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
let mut result = rewrite_generics(context, "", generics, shape).ok()?;
let mut result = match rewrite_generics(context, "", generics, shape) {
Ok(r) => r,
Err(_) => {
// First attempt failed, try with infinite width for complex generics
let wide_shape = shape.infinite_width();
match rewrite_generics(context, "", generics, wide_shape) {
Ok(r) => r,
Err(_) => {
// If even that fails, preserve original formatting from source
context.snippet(generics.span).to_string()
}
}
}
};

// If the generics are not parameterized then generics.span.hi() == 0,
// so we use span.lo(), which is the position after `struct Foo`.
Expand Down
16 changes: 16 additions & 0 deletions tests/source/issue-6571.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Regression test for panic when formatting very long generic constraints
// This used to cause rustfmt to panic at src/items.rs:556 with "Option::unwrap() on a None value"
pub enum TestEnum<
T: std::collections::HashMap<String, Vec<Box<dyn std::fmt::Debug + Send + Sync + 'static>>> + Clone + Default + PartialEq + Eq + std::fmt::Debug + serde::Serialize + serde::Deserialize<'static> + Send + Sync + 'static = std::collections::HashMap<String, Vec<Box<dyn std::fmt::Debug + Send + Sync + 'static>>>,
> {
Variant1(T),
Variant2 { field: T },
}

// More realistic example from real codebase
pub enum ElementInit<
P: wrt_foundation::MemoryProvider + Clone + Default + PartialEq + Eq = wrt_foundation::NoStdProvider<1024>,
> {
FuncIndices(crate::WasmVec<u32, P>),
Expressions(crate::WasmVec<crate::WasmVec<u8, P>, P>),
}
16 changes: 16 additions & 0 deletions tests/target/issue-6571.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Regression test for panic when formatting very long generic constraints
// This used to cause rustfmt to panic at src/items.rs:556 with "Option::unwrap() on a None value"
pub enum TestEnum<
T: std::collections::HashMap<String, Vec<Box<dyn std::fmt::Debug + Send + Sync + 'static>>> + Clone + Default + PartialEq + Eq + std::fmt::Debug + serde::Serialize + serde::Deserialize<'static> + Send + Sync + 'static = std::collections::HashMap<String, Vec<Box<dyn std::fmt::Debug + Send + Sync + 'static>>>,
> {
Variant1(T),
Variant2 { field: T },
}

// More realistic example from real codebase
pub enum ElementInit<
P: wrt_foundation::MemoryProvider + Clone + Default + PartialEq + Eq = wrt_foundation::NoStdProvider<1024>,
> {
FuncIndices(crate::WasmVec<u32, P>),
Expressions(crate::WasmVec<crate::WasmVec<u8, P>, P>),
}
Loading