about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2025-04-07 13:54:24 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2025-04-08 12:06:42 +1000
commite177921ae9bca5d697e682551c27c5baefcfbb60 (patch)
treeebe044c63f470cbd4742b362286763bd22886d7e
parenteb5d8923fce0a025921368d175905c9cab481954 (diff)
downloadrust-e177921ae9bca5d697e682551c27c5baefcfbb60.tar.gz
rust-e177921ae9bca5d697e682551c27c5baefcfbb60.zip
Allow for reparsing failure when reparsing a pasted metavar.
Fixes #139445.

The additional errors aren't great but the first one is still good and
it's the most important, and imperfect errors are better than ICEing.
-rw-r--r--compiler/rustc_parse/src/parser/mod.rs13
-rw-r--r--tests/ui/macros/failed-to-reparse-issue-139445.rs6
-rw-r--r--tests/ui/macros/failed-to-reparse-issue-139445.stderr24
3 files changed, 40 insertions, 3 deletions
diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs
index b277cb804fa..fafd1b1ae00 100644
--- a/compiler/rustc_parse/src/parser/mod.rs
+++ b/compiler/rustc_parse/src/parser/mod.rs
@@ -782,9 +782,16 @@ impl<'a> Parser<'a> {
             // Recovery is disabled when parsing macro arguments, so it must
             // also be disabled when reparsing pasted macro arguments,
             // otherwise we get inconsistent results (e.g. #137874).
-            let res = self.with_recovery(Recovery::Forbidden, |this| {
-                f(this).expect("failed to reparse {mv_kind:?}")
-            });
+            let res = self.with_recovery(Recovery::Forbidden, |this| f(this));
+
+            let res = match res {
+                Ok(res) => res,
+                Err(err) => {
+                    // This can occur in unusual error cases, e.g. #139445.
+                    err.delay_as_bug();
+                    return None;
+                }
+            };
 
             if let token::CloseDelim(delim) = self.token.kind
                 && let Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind)) = delim
diff --git a/tests/ui/macros/failed-to-reparse-issue-139445.rs b/tests/ui/macros/failed-to-reparse-issue-139445.rs
new file mode 100644
index 00000000000..babe26b9d29
--- /dev/null
+++ b/tests/ui/macros/failed-to-reparse-issue-139445.rs
@@ -0,0 +1,6 @@
+fn main() {
+    assert_eq!(3, 'a,)
+    //~^ ERROR expected `while`, `for`, `loop` or `{` after a label
+    //~| ERROR expected `while`, `for`, `loop` or `{` after a label
+    //~| ERROR expected expression, found ``
+}
diff --git a/tests/ui/macros/failed-to-reparse-issue-139445.stderr b/tests/ui/macros/failed-to-reparse-issue-139445.stderr
new file mode 100644
index 00000000000..6f7d88fb344
--- /dev/null
+++ b/tests/ui/macros/failed-to-reparse-issue-139445.stderr
@@ -0,0 +1,24 @@
+error: expected `while`, `for`, `loop` or `{` after a label
+  --> $DIR/failed-to-reparse-issue-139445.rs:2:21
+   |
+LL |     assert_eq!(3, 'a,)
+   |                     ^ expected `while`, `for`, `loop` or `{` after a label
+
+error: expected `while`, `for`, `loop` or `{` after a label
+  --> $DIR/failed-to-reparse-issue-139445.rs:2:5
+   |
+LL |     assert_eq!(3, 'a,)
+   |     ^^^^^^^^^^^^^^^^^^ expected `while`, `for`, `loop` or `{` after a label
+   |
+   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: expected expression, found ``
+  --> $DIR/failed-to-reparse-issue-139445.rs:2:5
+   |
+LL |     assert_eq!(3, 'a,)
+   |     ^^^^^^^^^^^^^^^^^^ expected expression
+   |
+   = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error: aborting due to 3 previous errors
+