Skip to content

Commit

Permalink
fix panic on failure to format generics in enum
Browse files Browse the repository at this point in the history
- related issues: #5738, #6137, #6318, #6378
- instead of calling unwrap(), restore original snippet when we fail to format generics in enum
- we need to propagate this rewrite failure later
  • Loading branch information
ding-young committed Nov 29, 2024
1 parent 0a32a02 commit ff6ebd3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,9 +550,13 @@ impl<'a> FmtVisitor<'a> {
// make a span that starts right after `enum Foo`
mk_sp(ident.span.hi(), body_start),
last_line_width(&enum_header),
)
.unwrap();
self.push_str(&generics_str);
);

if let Some(generics_str) = generics_str {
self.push_str(&generics_str);
} else {
self.push_str(self.snippet(mk_sp(ident.span.hi(), body_start)));
}

self.last_pos = body_start;

Expand Down
13 changes: 13 additions & 0 deletions tests/source/crash-on-enum-generics/issue_5738.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum Node where P::<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S>>>>>>>>>>>>>>>>>>>>>>>>: {
Cons,
}

enum En6
where
T: Tr1<En2<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S>>>>>>>>>>>>>>>>>>>>>>>>,
{
V0,
V1,
V2,
V3,
}
3 changes: 3 additions & 0 deletions tests/source/crash-on-enum-generics/issue_6137.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum MZReaderType<
D: DeconvolutedCentroidLike + Default + From<DeconvolutedPeak> + BuildFromArrayMap=DeconvolutedPeak
> {}
13 changes: 13 additions & 0 deletions tests/source/crash-on-enum-generics/issue_6318.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// rustfmt-max_width: 80
fn my_fn() {
enum MyEnum
where
SomeTypeA___: SomeTrait__<
_A,
Archived = <SomeTypeB____ as SomeTrait__<
Option<[u8; 4]>,
>>::Archived,
>,
{
}
}
13 changes: 13 additions & 0 deletions tests/target/crash-on-enum-generics/issue_5738.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
enum Node where P::<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S>>>>>>>>>>>>>>>>>>>>>>>>: {
Cons,
}

enum En6
where
T: Tr1<En2<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S<S>>>>>>>>>>>>>>>>>>>>>>>>,
{
V0,
V1,
V2,
V3,
}
3 changes: 3 additions & 0 deletions tests/target/crash-on-enum-generics/issue_6137.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enum MZReaderType<
D: DeconvolutedCentroidLike + Default + From<DeconvolutedPeak> + BuildFromArrayMap=DeconvolutedPeak
> {}
12 changes: 12 additions & 0 deletions tests/target/crash-on-enum-generics/issue_6318.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// rustfmt-max_width: 80
fn my_fn() {
enum MyEnum
where
SomeTypeA___: SomeTrait__<
_A,
Archived = <SomeTypeB____ as SomeTrait__<
Option<[u8; 4]>,
>>::Archived,
>,
{}
}

0 comments on commit ff6ebd3

Please sign in to comment.