Skip to content

Commit

Permalink
[macro_metavar_expr_concat] Fix #128346
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Oct 10, 2024
1 parent d0141af commit 74f6b19
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,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) => {
Expand Down
22 changes: 20 additions & 2 deletions tests/ui/macros/macro-metavar-expr-concat/repetitions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//@ run-pass

#![feature(macro_metavar_expr_concat)]

macro_rules! one_rep {
Expand All @@ -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);
}
22 changes: 22 additions & 0 deletions tests/ui/macros/macro-metavar-expr-concat/repetitions.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 74f6b19

Please sign in to comment.