Skip to content

Commit 97e8897

Browse files
committed
fix panic on failure to format generics in enum
- instead of calling unwrap(), restore original snippet when we fail to format generics in enum - we need to propagate this rewrite failure later
1 parent 777e25a commit 97e8897

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

Diff for: src/items.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -558,9 +558,13 @@ impl<'a> FmtVisitor<'a> {
558558
// make a span that starts right after `enum Foo`
559559
mk_sp(ident.span.hi(), body_start),
560560
last_line_width(&enum_header),
561-
)
562-
.unwrap();
563-
self.push_str(&generics_str);
561+
);
562+
563+
if let Some(generics_str) = generics_str {
564+
self.push_str(&generics_str);
565+
} else {
566+
self.push_str(self.snippet(mk_sp(ident.span.hi(), body_start)));
567+
}
564568

565569
self.last_pos = body_start;
566570

Diff for: tests/source/issue-5738/enum_with_generics.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// rustfmt-style_edition: 2024
2+
enum En4<'x1, 'x2, T: Tr1<As1: >> {
3+
V0,
4+
V1,
5+
}
6+
7+
enum _En5<'x1, 'x2, T: Tr1<As1: >> {
8+
_V0,
9+
_V1,
10+
}
11+
12+
enum En6
13+
where
14+
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>>>>>>>>>>>>>>>>>>>>>>>>,
15+
{
16+
V0,
17+
V1,
18+
V2,
19+
V3,
20+
}
21+
22+
enum _En7
23+
where
24+
T: ,
25+
{
26+
V0,
27+
V1,
28+
}
29+
30+
fn _make_en7()
31+
where
32+
T: ,
33+
{
34+
35+
}
36+
37+
enum EnSelf<T> where Self: Tr1<As1: > {
38+
V0(T),
39+
V1,
40+
V2,
41+
}

Diff for: tests/target/issue-5738/enum_with_generics.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// rustfmt-style_edition: 2024
2+
enum En4<'x1, 'x2, T: Tr1<As1: >> {
3+
V0,
4+
V1,
5+
}
6+
7+
enum _En5<'x1, 'x2, T: Tr1<As1: >> {
8+
_V0,
9+
_V1,
10+
}
11+
12+
enum En6
13+
where
14+
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>>>>>>>>>>>>>>>>>>>>>>>>,
15+
{
16+
V0,
17+
V1,
18+
V2,
19+
V3,
20+
}
21+
22+
enum _En7
23+
where
24+
T:,
25+
{
26+
V0,
27+
V1,
28+
}
29+
30+
fn _make_en7()
31+
where
32+
T:,
33+
{
34+
}
35+
36+
enum EnSelf<T>
37+
where
38+
Self: Tr1<As1: >,
39+
{
40+
V0(T),
41+
V1,
42+
V2,
43+
}

0 commit comments

Comments
 (0)