about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCaio <c410.f3r@gmail.com>2024-12-01 19:10:44 -0300
committerCaio <c410.f3r@gmail.com>2024-12-01 19:10:44 -0300
commitb661e98f09a195cd13457525b09b6db628835734 (patch)
treef179a4e43e9ea7fcf648b3ca9c98df8b00017e0b
parent1555074ca98de5a827411cf7d7a11779bcfc90f2 (diff)
downloadrust-b661e98f09a195cd13457525b09b6db628835734.tar.gz
rust-b661e98f09a195cd13457525b09b6db628835734.zip
Fix #128346
-rw-r--r--compiler/rustc_expand/src/mbe/transcribe.rs6
-rw-r--r--tests/crashes/128346.rs13
-rw-r--r--tests/ui/macros/macro-metavar-expr-concat/repetitions.rs22
-rw-r--r--tests/ui/macros/macro-metavar-expr-concat/repetitions.stderr22
4 files changed, 46 insertions, 17 deletions
diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs
index b77d02e630a..c780cfd0e8f 100644
--- a/compiler/rustc_expand/src/mbe/transcribe.rs
+++ b/compiler/rustc_expand/src/mbe/transcribe.rs
@@ -696,8 +696,10 @@ fn transcribe_metavar_expr<'a>(
                     MetaVarExprConcatElem::Var(ident) => {
                         match matched_from_ident(dcx, *ident, interp)? {
                             NamedMatch::MatchedSeq(named_matches) => {
-                                let curr_idx = repeats.last().unwrap().0;
-                                match &named_matches[curr_idx] {
+                                let Some((curr_idx, _)) = repeats.last() else {
+                                    return Err(dcx.struct_span_err(sp.entire(), "invalid syntax"));
+                                };
+                                match &named_matches[*curr_idx] {
                                     // FIXME(c410-f3r) Nested repetitions are unimplemented
                                     MatchedSeq(_) => unimplemented!(),
                                     MatchedSingle(pnr) => {
diff --git a/tests/crashes/128346.rs b/tests/crashes/128346.rs
deleted file mode 100644
index 93d9c40a544..00000000000
--- a/tests/crashes/128346.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-//@ known-bug: rust-lang/rust#128346
-
-macro_rules! one_rep {
-    ( $($a:ident)* ) => {
-        A(
-            const ${concat($a, Z)}: i32 = 3;
-        )*
-    };
-}
-
-fn main() {
-    one_rep!(A B C);
-}
diff --git a/tests/ui/macros/macro-metavar-expr-concat/repetitions.rs b/tests/ui/macros/macro-metavar-expr-concat/repetitions.rs
index 781443207ac..52a7d5cd8a7 100644
--- a/tests/ui/macros/macro-metavar-expr-concat/repetitions.rs
+++ b/tests/ui/macros/macro-metavar-expr-concat/repetitions.rs
@@ -1,5 +1,3 @@
-//@ run-pass
-
 #![feature(macro_metavar_expr_concat)]
 
 macro_rules! one_rep {
@@ -10,9 +8,29 @@ macro_rules! one_rep {
     };
 }
 
+macro_rules! issue_128346 {
+    ( $($a:ident)* ) => {
+        A(
+            const ${concat($a, Z)}: i32 = 3;
+            //~^ ERROR invalid syntax
+        )*
+    };
+}
+
+macro_rules! issue_131393 {
+    ($t:ident $($en:ident)?) => {
+        read::<${concat($t, $en)}>()
+        //~^ ERROR invalid syntax
+        //~| ERROR invalid syntax
+    }
+}
+
 fn main() {
     one_rep!(A B C);
     assert_eq!(AZ, 3);
     assert_eq!(BZ, 3);
     assert_eq!(CZ, 3);
+    issue_128346!(A B C);
+    issue_131393!(u8);
+    issue_131393!(u16 le);
 }
diff --git a/tests/ui/macros/macro-metavar-expr-concat/repetitions.stderr b/tests/ui/macros/macro-metavar-expr-concat/repetitions.stderr
new file mode 100644
index 00000000000..c3006c4be5d
--- /dev/null
+++ b/tests/ui/macros/macro-metavar-expr-concat/repetitions.stderr
@@ -0,0 +1,22 @@
+error: invalid syntax
+  --> $DIR/repetitions.rs:14:20
+   |
+LL |             const ${concat($a, Z)}: i32 = 3;
+   |                    ^^^^^^^^^^^^^^^
+
+error: invalid syntax
+  --> $DIR/repetitions.rs:22:17
+   |
+LL |         read::<${concat($t, $en)}>()
+   |                 ^^^^^^^^^^^^^^^^^
+
+error: invalid syntax
+  --> $DIR/repetitions.rs:22:17
+   |
+LL |         read::<${concat($t, $en)}>()
+   |                 ^^^^^^^^^^^^^^^^^
+   |
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+
+error: aborting due to 3 previous errors
+