Skip to content

Commit

Permalink
[macro_metavar_expr_concat] Allow concat in repetitions
Browse files Browse the repository at this point in the history
  • Loading branch information
c410-f3r committed Jul 14, 2024
1 parent 6be96e3 commit cc888dc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_expand/src/mbe/macro_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ fn check_occurrences(
check_ops_is_prefix(psess, node_id, macros, binders, ops, span, name);
}
TokenTree::MetaVarExpr(dl, ref mve) => {
let Some(name) = mve.ident().map(MacroRulesNormalizedIdent::new) else {
return;
};
check_ops_is_prefix(psess, node_id, macros, binders, ops, dl.entire(), name);
mve.idents((), |_, ident| {
let name = MacroRulesNormalizedIdent::new(*ident);
check_ops_is_prefix(psess, node_id, macros, binders, ops, dl.entire(), name);
});
}
TokenTree::Delimited(.., ref del) => {
check_nested_occurrences(psess, node_id, &del.tts, macros, binders, ops, guar);
Expand Down
16 changes: 12 additions & 4 deletions compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ impl MetaVarExpr {
Ok(rslt)
}

pub(crate) fn ident(&self) -> Option<Ident> {
match *self {
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
MetaVarExpr::Concat { .. } | MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => None,
pub(crate) fn idents<A>(&self, mut aux: A, mut cb: impl FnMut(A, &Ident) -> A) -> A {
match self {
MetaVarExpr::Concat(elems) => {
for elem in elems {
if let MetaVarExprConcatElem::Var(ident) = elem {
aux = cb(aux, ident)
}
}
aux
}
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => cb(aux, ident),
MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => aux,
}
}
}
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,17 +556,13 @@ fn lockstep_iter_size(
}
}
TokenTree::MetaVarExpr(_, expr) => {
let default_rslt = LockstepIterSize::Unconstrained;
let Some(ident) = expr.ident() else {
return default_rslt;
};
let name = MacroRulesNormalizedIdent::new(ident);
match lookup_cur_matched(name, interpolations, repeats) {
Some(MatchedSeq(ads)) => {
default_rslt.with(LockstepIterSize::Constraint(ads.len(), name))
expr.idents(LockstepIterSize::Unconstrained, |mut lis, ident| {
let name = MacroRulesNormalizedIdent::new(*ident);
if let Some(MatchedSeq(ads)) = lookup_cur_matched(name, interpolations, repeats) {
lis = lis.with(LockstepIterSize::Constraint(ads.len(), name));
}
_ => default_rslt,
}
lis
})
}
TokenTree::Token(..) => LockstepIterSize::Unconstrained,
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/macros/macro-metavar-expr-concat/repetitions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(macro_metavar_expr_concat)]

macro_rules! many_idents_multi_metavar {
($($a:ident),*) => {
$(
const ${concat($a, B)}: i32 = 3;
//~^ ERROR: `${concat(..)}` currently only accepts identifiers or meta-variables as parameters
)*
};
}

fn main() {
many_idents_multi_metavar!(A, B, C)
}
8 changes: 8 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,8 @@
error: `${concat(..)}` currently only accepts identifiers or meta-variables as parameters
--> $DIR/repetitions.rs:6:29
|
LL | const ${concat($a, B)}: i32 = 3;
| ^

error: aborting due to 1 previous error

0 comments on commit cc888dc

Please sign in to comment.