Skip to content

Commit 24369ad

Browse files
authored
Rollup merge of rust-lang#139464 - nnethercote:fix-139248-AND-fix-139445, r=petrochenkov
Allow for reparsing failure when reparsing a pasted metavar. Fix some metavar reparsing issues. Fixes rust-lang#139248 and rust-lang#139445. r? `@petrochenkov`
2 parents 056756c + e177921 commit 24369ad

File tree

5 files changed

+93
-4
lines changed

5 files changed

+93
-4
lines changed

compiler/rustc_parse/src/parser/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -782,9 +782,16 @@ impl<'a> Parser<'a> {
782782
// Recovery is disabled when parsing macro arguments, so it must
783783
// also be disabled when reparsing pasted macro arguments,
784784
// otherwise we get inconsistent results (e.g. #137874).
785-
let res = self.with_recovery(Recovery::Forbidden, |this| {
786-
f(this).expect("failed to reparse {mv_kind:?}")
787-
});
785+
let res = self.with_recovery(Recovery::Forbidden, |this| f(this));
786+
787+
let res = match res {
788+
Ok(res) => res,
789+
Err(err) => {
790+
// This can occur in unusual error cases, e.g. #139445.
791+
err.delay_as_bug();
792+
return None;
793+
}
794+
};
788795

789796
if let token::CloseDelim(delim) = self.token.kind
790797
&& let Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)) = delim
@@ -793,7 +800,12 @@ impl<'a> Parser<'a> {
793800
self.bump();
794801
Some(res)
795802
} else {
796-
panic!("no close delim when reparsing {mv_kind:?}");
803+
// This can occur when invalid syntax is passed to a decl macro. E.g. see #139248,
804+
// where the reparse attempt of an invalid expr consumed the trailing invisible
805+
// delimiter.
806+
self.dcx()
807+
.span_delayed_bug(self.token.span, "no close delim with reparsing {mv_kind:?}");
808+
None
797809
}
798810
} else {
799811
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
assert_eq!(3, 'a,)
3+
//~^ ERROR expected `while`, `for`, `loop` or `{` after a label
4+
//~| ERROR expected `while`, `for`, `loop` or `{` after a label
5+
//~| ERROR expected expression, found ``
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: expected `while`, `for`, `loop` or `{` after a label
2+
--> $DIR/failed-to-reparse-issue-139445.rs:2:21
3+
|
4+
LL | assert_eq!(3, 'a,)
5+
| ^ expected `while`, `for`, `loop` or `{` after a label
6+
7+
error: expected `while`, `for`, `loop` or `{` after a label
8+
--> $DIR/failed-to-reparse-issue-139445.rs:2:5
9+
|
10+
LL | assert_eq!(3, 'a,)
11+
| ^^^^^^^^^^^^^^^^^^ expected `while`, `for`, `loop` or `{` after a label
12+
|
13+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
error: expected expression, found ``
16+
--> $DIR/failed-to-reparse-issue-139445.rs:2:5
17+
|
18+
LL | assert_eq!(3, 'a,)
19+
| ^^^^^^^^^^^^^^^^^^ expected expression
20+
|
21+
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error: aborting due to 3 previous errors
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
//~^ ERROR macro expansion ends with an incomplete expression: expected expression
7+
}
8+
}
9+
10+
m! { static a : () = (if b) }
11+
//~^ ERROR expected `{`, found `)`
12+
//~| ERROR expected `{`, found `)`
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error: expected `{`, found `)`
2+
--> $DIR/no-close-delim-issue-139248.rs:10:27
3+
|
4+
LL | m! { 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:10:26
9+
|
10+
LL | m! { static a : () = (if b) }
11+
| ^
12+
13+
error: expected `{`, found `)`
14+
--> $DIR/no-close-delim-issue-139248.rs:10:27
15+
|
16+
LL | m! { 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:10:26
21+
|
22+
LL | m! { static a : () = (if b) }
23+
| ^
24+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25+
26+
error: macro expansion ends with an incomplete expression: expected expression
27+
--> $DIR/no-close-delim-issue-139248.rs:5:28
28+
|
29+
LL | static a : () = $e;
30+
| ^ expected expression
31+
32+
error: aborting due to 3 previous errors
33+

0 commit comments

Comments
 (0)