Fix macro_metavar_expr_concat behavior with nested repetitions

This commit is contained in:
delta17920 2025-12-15 15:28:57 +00:00
parent ed0006a7ba
commit 3566b6775c
6 changed files with 63 additions and 34 deletions

View file

@ -558,25 +558,20 @@ fn metavar_expr_concat<'tx>(
MetaVarExprConcatElem::Ident(elem) => elem.name,
MetaVarExprConcatElem::Literal(elem) => *elem,
MetaVarExprConcatElem::Var(ident) => {
match matched_from_ident(dcx, *ident, tscx.interp)? {
NamedMatch::MatchedSeq(named_matches) => {
let Some((curr_idx, _)) = tscx.repeats.last() else {
return Err(dcx.struct_span_err(dspan.entire(), "invalid syntax"));
};
match &named_matches[*curr_idx] {
// FIXME(c410-f3r) Nested repetitions are unimplemented
MatchedSeq(_) => {
return Err(dcx.struct_span_err(
ident.span,
"nested repetitions with `${concat(...)}` metavariable expressions are not yet supported",
));
}
MatchedSingle(pnr) => extract_symbol_from_pnr(dcx, pnr, ident.span)?,
}
}
NamedMatch::MatchedSingle(pnr) => {
let key = MacroRulesNormalizedIdent::new(*ident);
match lookup_cur_matched(key, tscx.interp, &tscx.repeats) {
Some(NamedMatch::MatchedSingle(pnr)) => {
extract_symbol_from_pnr(dcx, pnr, ident.span)?
}
Some(NamedMatch::MatchedSeq(..)) => {
return Err(dcx.struct_span_err(
ident.span,
"`${concat(...)}` variable is still repeating at this depth",
));
}
None => {
return Err(dcx.create_err(MveUnrecognizedVar { span: ident.span, key }));
}
}
}
};

View file

@ -0,0 +1,35 @@
//@ check-pass
#![feature(macro_metavar_expr_concat)]
struct A;
struct B;
const AA: A = A;
const BB: B = B;
macro_rules! define_ioctl_data {
(struct $s:ident {
$($field:ident: $ty:ident $([$opt:ident])?,)*
}) => {
pub struct $s {
$($field: $ty,)*
}
impl $s {
$($(
fn ${concat(get_, $field)}(&self) -> $ty {
let _ = $opt;
todo!()
}
)?)*
}
};
}
define_ioctl_data! {
struct Foo {
a: A [AA],
b: B [BB],
}
}
fn main() {}

View file

@ -11,7 +11,7 @@ macro_rules! InRepetition {
) => {
$(
$(
${concat(_, $arg)} //~ ERROR nested repetitions with `${concat(...)}` metavariable expressions are not yet supported
${concat(_, $arg)} //~ ERROR macro expansion ends with an incomplete expression: expected one of `!` or `::`
)*
)*
};

View file

@ -1,8 +1,8 @@
error: nested repetitions with `${concat(...)}` metavariable expressions are not yet supported
--> $DIR/in-repetition.rs:14:30
error: macro expansion ends with an incomplete expression: expected one of `!` or `::`
--> $DIR/in-repetition.rs:14:35
|
LL | ${concat(_, $arg)}
| ^^^
| ^ expected one of `!` or `::`
error: aborting due to 1 previous error

View file

@ -11,8 +11,7 @@ macro_rules! one_rep {
macro_rules! issue_128346 {
( $($a:ident)* ) => {
A(
const ${concat($a, Z)}: i32 = 3;
//~^ ERROR invalid syntax
const ${concat($a, Z)}: i32 = 3; //~ ERROR `${concat(...)}` variable is still repeating at this depth
)*
};
}
@ -20,8 +19,8 @@ macro_rules! issue_128346 {
macro_rules! issue_131393 {
($t:ident $($en:ident)?) => {
read::<${concat($t, $en)}>()
//~^ ERROR invalid syntax
//~| ERROR invalid syntax
//~^ ERROR `${concat(...)}` variable is still repeating at this depth
//~| ERROR `${concat(...)}` variable is still repeating at this depth
}
}

View file

@ -1,20 +1,20 @@
error: invalid syntax
--> $DIR/concat-repetitions.rs:14:20
error: `${concat(...)}` variable is still repeating at this depth
--> $DIR/concat-repetitions.rs:14:29
|
LL | const ${concat($a, Z)}: i32 = 3;
| ^^^^^^^^^^^^^^^
| ^
error: invalid syntax
--> $DIR/concat-repetitions.rs:22:17
error: `${concat(...)}` variable is still repeating at this depth
--> $DIR/concat-repetitions.rs:21:30
|
LL | read::<${concat($t, $en)}>()
| ^^^^^^^^^^^^^^^^^
| ^^
error: invalid syntax
--> $DIR/concat-repetitions.rs:22:17
error: `${concat(...)}` variable is still repeating at this depth
--> $DIR/concat-repetitions.rs:21:30
|
LL | read::<${concat($t, $en)}>()
| ^^^^^^^^^^^^^^^^^
| ^^
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`