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
- 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 19, 2024
1 parent 777e25a commit 733e1ef
Show file tree
Hide file tree
Showing 3 changed files with 89 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 @@ -558,9 +558,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
40 changes: 40 additions & 0 deletions tests/source/issue-5738/enum_with_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
enum En4<'x1, 'x2, T: Tr1<As1: >> {
V0,
V1,
}

enum _En5<'x1, 'x2, T: Tr1<As1: >> {
_V0,
_V1,
}

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,
}

enum _En7
where
T: ,
{
V0,
V1,
}

fn _make_en7()
where
T: ,
{

}

enum EnSelf<T> where Self: Tr1<As1: > {
V0(T),
V1,
V2,
}
42 changes: 42 additions & 0 deletions tests/target/issue-5738/enum_with_generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
enum En4<'x1, 'x2, T: Tr1<As1: >> {
V0,
V1,
}

enum _En5<'x1, 'x2, T: Tr1<As1: >> {
_V0,
_V1,
}

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,
}

enum _En7
where
T:,
{
V0,
V1,
}

fn _make_en7()
where
T:,
{
}

enum EnSelf<T>
where
Self: Tr1<As1: >,
{
V0(T),
V1,
V2,
}

0 comments on commit 733e1ef

Please sign in to comment.