Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,26 +558,29 @@ 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(_) => {
let mut matched = matched_from_ident(dcx, *ident, tscx.interp)?;
let mut repeats_iter = tscx.repeats.iter();

loop {
match matched {
NamedMatch::MatchedSingle(pnr) => {
let symbol = extract_symbol_from_pnr(dcx, pnr, ident.span)?;
concatenated.push_str(symbol.as_str());
break;
}
NamedMatch::MatchedSeq(named_matches) => {
if let Some((idx, _)) = repeats_iter.next() {
matched = &named_matches[*idx];
} else {
return Err(dcx.struct_span_err(
ident.span,
"nested repetitions with `${concat(...)}` metavariable expressions are not yet supported",
"`${concat(...)}` variable is still repeating at this depth",
));
}
MatchedSingle(pnr) => extract_symbol_from_pnr(dcx, pnr, ident.span)?,
}
}
NamedMatch::MatchedSingle(pnr) => {
extract_symbol_from_pnr(dcx, pnr, ident.span)?
}
}
continue;
}
};
concatenated.push_str(symbol.as_str());
Expand Down
35 changes: 35 additions & 0 deletions tests/ui/macros/concat-nested-repetition.rs
Original file line number Diff line number Diff line change
@@ -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() {}
2 changes: 1 addition & 1 deletion tests/ui/macros/macro-metavar-expr-concat/in-repetition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `::`
)*
)*
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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

7 changes: 3 additions & 4 deletions tests/ui/macros/metavar-expressions/concat-repetitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ 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
)*
};
}

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
}
}

Expand Down
18 changes: 9 additions & 9 deletions tests/ui/macros/metavar-expressions/concat-repetitions.stderr
Original file line number Diff line number Diff line change
@@ -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`

Expand Down
Loading