Skip to content

Commit ad9d8f9

Browse files
committed
Allow for missing invisible close delim when reparsing an expression.
This can happen when invalid syntax is passed to a declarative macro. We shouldn't be too strict about the token stream position once the parser has rejected the invalid syntax. Fixes #139248 and #139445.
1 parent 4f0de4c commit ad9d8f9

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

compiler/rustc_parse/src/parser/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,10 @@ impl<'a> Parser<'a> {
778778
self.bump();
779779
Some(res)
780780
} else {
781-
panic!("no close delim when reparsing {mv_kind:?}");
781+
// This can occur when invalid syntax is passed to a decl macro. E.g. see #139248,
782+
// where the reparse attempt of an invalid expr consumed the trailing invisible
783+
// delimiter.
784+
None
782785
}
783786
} else {
784787
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This code caused a "no close delim when reparsing Expr" ICE in #139248.
2+
3+
macro_rules! m {
4+
(static a : () = $e:expr) => {
5+
static a : () = $e;
6+
}
7+
}
8+
9+
thread_local! { static a : () = (if b) }
10+
//~^ error: expected `{`, found `)`
11+
//~| error: expected `{`, found `)`
12+
//~| error: expected `{`, found `)`
13+
//~| error: expected expression, found end of macro arguments
14+
15+
fn main() {
16+
// This related case comes from #139445.
17+
assert_eq!('u, 'a,)
18+
//~^ ERROR expected `while`, `for`, `loop` or `{` after a label
19+
//~| ERROR expected `while`, `for`, `loop` or `{` after a label
20+
//~| ERROR expected `while`, `for`, `loop` or `{` after a label
21+
//~| ERROR expected `while`, `for`, `loop` or `{` after a label
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
error: expected `{`, found `)`
2+
--> $DIR/no-close-delim-issue-139248.rs:9:38
3+
|
4+
LL | thread_local! { static a : () = (if b) }
5+
| ^ expected `{`
6+
|
7+
note: the `if` expression is missing a block after this condition
8+
--> $DIR/no-close-delim-issue-139248.rs:9:37
9+
|
10+
LL | thread_local! { static a : () = (if b) }
11+
| ^
12+
13+
error: expected `{`, found `)`
14+
--> $DIR/no-close-delim-issue-139248.rs:9:38
15+
|
16+
LL | thread_local! { static a : () = (if b) }
17+
| ^ expected `{`
18+
|
19+
note: the `if` expression is missing a block after this condition
20+
--> $DIR/no-close-delim-issue-139248.rs:9:37
21+
|
22+
LL | thread_local! { static a : () = (if b) }
23+
| ^
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25+
26+
error: expected `{`, found `)`
27+
--> $DIR/no-close-delim-issue-139248.rs:9:38
28+
|
29+
LL | thread_local! { static a : () = (if b) }
30+
| ^ expected `{`
31+
|
32+
note: the `if` expression is missing a block after this condition
33+
--> $DIR/no-close-delim-issue-139248.rs:9:37
34+
|
35+
LL | thread_local! { static a : () = (if b) }
36+
| ^
37+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
38+
39+
error: expected expression, found end of macro arguments
40+
--> $DIR/no-close-delim-issue-139248.rs:9:1
41+
|
42+
LL | thread_local! { static a : () = (if b) }
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected expression
44+
--> $SRC_DIR/std/src/sys/thread_local/native/mod.rs:LL:COL
45+
|
46+
= note: while parsing argument for this `expr` macro fragment
47+
|
48+
= note: this error originates in the macro `thread_local` (in Nightly builds, run with -Z macro-backtrace for more info)
49+
50+
error: expected `while`, `for`, `loop` or `{` after a label
51+
--> $DIR/no-close-delim-issue-139248.rs:17:18
52+
|
53+
LL | assert_eq!('u, 'a,)
54+
| ^ expected `while`, `for`, `loop` or `{` after a label
55+
56+
error: expected `while`, `for`, `loop` or `{` after a label
57+
--> $DIR/no-close-delim-issue-139248.rs:17:22
58+
|
59+
LL | assert_eq!('u, 'a,)
60+
| ^ expected `while`, `for`, `loop` or `{` after a label
61+
62+
error: expected `while`, `for`, `loop` or `{` after a label
63+
--> $DIR/no-close-delim-issue-139248.rs:17:5
64+
|
65+
LL | assert_eq!('u, 'a,)
66+
| ^^^^^^^^^^^^^^^^^^^ expected `while`, `for`, `loop` or `{` after a label
67+
|
68+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
69+
help: add `'` to close the char literal
70+
|
71+
LL | assert_eq!('u', 'a,)
72+
| +
73+
74+
error: expected `while`, `for`, `loop` or `{` after a label
75+
--> $DIR/no-close-delim-issue-139248.rs:17:5
76+
|
77+
LL | assert_eq!('u, 'a,)
78+
| ^^^^^^^^^^^^^^^^^^^ expected `while`, `for`, `loop` or `{` after a label
79+
|
80+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
81+
help: add `'` to close the char literal
82+
|
83+
LL | assert_eq!('u, 'a',)
84+
| +
85+
86+
error: aborting due to 8 previous errors
87+

0 commit comments

Comments
 (0)