Skip to content

Commit 7073bcd

Browse files
Rollup merge of #151948 - chenyukang:yukang-fix-149954-tokenstream-ice, r=Kivooeo
Handle unbalanced delimiters gracefully in make_attr_token_stream Fixes #149954
2 parents 03c5398 + 2bab7a0 commit 7073bcd

3 files changed

Lines changed: 139 additions & 1 deletion

File tree

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,13 @@ fn make_attr_token_stream(
354354
FrameData { open_delim_sp: Some((delim, span, spacing)), inner: vec![] },
355355
));
356356
} else if let Some(delim) = kind.close_delim() {
357-
let frame_data = mem::replace(&mut stack_top, stack_rest.pop().unwrap());
357+
// If there's no matching opening delimiter, the token stream is malformed,
358+
// likely due to a improper delimiter positions in the source code.
359+
// It's not delimiter mismatch, and lexer can not detect it, so we just ignore it here.
360+
let Some(frame) = stack_rest.pop() else {
361+
return AttrTokenStream::new(stack_top.inner);
362+
};
363+
let frame_data = mem::replace(&mut stack_top, frame);
358364
let (open_delim, open_sp, open_spacing) = frame_data.open_delim_sp.unwrap();
359365
assert!(
360366
open_delim.eq_ignoring_invisible_origin(&delim),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Regression test for ICE https://github.com/rust-lang/rust/issues/149954
2+
//@ edition: 2024
3+
4+
enum A {
5+
A
6+
const A: A = { //~ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found keyword `const`
7+
#[derive(Debug)]
8+
struct A
9+
where
10+
A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
11+
//~^ ERROR malformed `cfg` attribute input
12+
//~| ERROR malformed `cfg` attribute input
13+
//~| ERROR expected trait, found struct `A`
14+
//~| ERROR expected trait, found type parameter `A`
15+
//~| ERROR expected trait, found struct `A`
16+
//~| ERROR expected trait, found type parameter `A`
17+
//~| ERROR expected one of `<`, `where`, or `{`, found `}`
18+
//~| ERROR expected one of `<`, `where`, or `{`, found `}`
19+
//~| ERROR expected one of `,`, `>`, or `}`, found `<eof>`
20+
}
21+
>;
22+
}; //~ ERROR `main` function not found in crate
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found keyword `const`
2+
--> $DIR/tokenstream-ice-issue-149954.rs:6:5
3+
|
4+
LL | A
5+
| - expected one of `(`, `,`, `=`, `{`, or `}`
6+
LL | const A: A = {
7+
| ^^^^^ unexpected token
8+
|
9+
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
10+
11+
error: expected one of `<`, `where`, or `{`, found `}`
12+
--> $DIR/tokenstream-ice-issue-149954.rs:10:60
13+
|
14+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
15+
| - ^ expected one of `<`, `where`, or `{`
16+
| |
17+
| while parsing this enum
18+
19+
error: expected one of `<`, `where`, or `{`, found `}`
20+
--> $DIR/tokenstream-ice-issue-149954.rs:10:60
21+
|
22+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
23+
| - ^ expected one of `<`, `where`, or `{`
24+
| |
25+
| while parsing this enum
26+
|
27+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
28+
29+
error: expected one of `,`, `>`, or `}`, found `<eof>`
30+
--> $DIR/tokenstream-ice-issue-149954.rs:10:60
31+
|
32+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
33+
| ^ expected one of `,`, `>`, or `}`
34+
|
35+
help: you might have meant to end the type parameters here
36+
|
37+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }>
38+
| +
39+
40+
error[E0539]: malformed `cfg` attribute input
41+
--> $DIR/tokenstream-ice-issue-149954.rs:10:36
42+
|
43+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
44+
| ^^^^^^
45+
| |
46+
| expected this to be a list
47+
| help: must be of the form: `#[cfg(predicate)]`
48+
|
49+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
50+
51+
error[E0539]: malformed `cfg` attribute input
52+
--> $DIR/tokenstream-ice-issue-149954.rs:10:36
53+
|
54+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
55+
| ^^^^^^
56+
| |
57+
| expected this to be a list
58+
| help: must be of the form: `#[cfg(predicate)]`
59+
|
60+
= note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg-attribute>
61+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
62+
63+
error[E0404]: expected trait, found struct `A`
64+
--> $DIR/tokenstream-ice-issue-149954.rs:10:16
65+
|
66+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
67+
| ________________^
68+
... |
69+
LL | | >;
70+
| |_________^ not a trait
71+
72+
error[E0404]: expected trait, found type parameter `A`
73+
--> $DIR/tokenstream-ice-issue-149954.rs:10:32
74+
|
75+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
76+
| - ^^^^^^^^^^^^^^^^ not a trait
77+
| |
78+
| found this type parameter
79+
80+
error[E0404]: expected trait, found struct `A`
81+
--> $DIR/tokenstream-ice-issue-149954.rs:10:16
82+
|
83+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
84+
| ________________^
85+
... |
86+
LL | | >;
87+
| |_________^ not a trait
88+
|
89+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
90+
91+
error[E0404]: expected trait, found type parameter `A`
92+
--> $DIR/tokenstream-ice-issue-149954.rs:10:32
93+
|
94+
LL | A: A<{ struct A<A: A<{ #[cfg] () }>> ; enum A }
95+
| - ^^^^^^^^^^^^^^^^ not a trait
96+
| |
97+
| found this type parameter
98+
|
99+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
100+
101+
error[E0601]: `main` function not found in crate `tokenstream_ice_issue_149954`
102+
--> $DIR/tokenstream-ice-issue-149954.rs:22:3
103+
|
104+
LL | };
105+
| ^ consider adding a `main` function to `$DIR/tokenstream-ice-issue-149954.rs`
106+
107+
error: aborting due to 11 previous errors
108+
109+
Some errors have detailed explanations: E0404, E0539, E0601.
110+
For more information about an error, try `rustc --explain E0404`.

0 commit comments

Comments
 (0)