From 5d333c155e01e303da1e1b5704a0d4d6032c92a6 Mon Sep 17 00:00:00 2001
From: Caio <c410.f3r@gmail.com>
Date: Mon, 14 Mar 2022 08:29:20 -0300
Subject: [PATCH] Fix remaining meta-variable expression TODOs

---
 compiler/rustc_expand/src/mbe/macro_check.rs  | 8 ++++++--
 compiler/rustc_expand/src/mbe/macro_parser.rs | 7 +++++--
 compiler/rustc_expand/src/mbe/metavar_expr.rs | 6 +++---
 compiler/rustc_expand/src/mbe/transcribe.rs   | 4 ++--
 4 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/compiler/rustc_expand/src/mbe/macro_check.rs b/compiler/rustc_expand/src/mbe/macro_check.rs
index 88e1169322093..f18cf95a2bf11 100644
--- a/compiler/rustc_expand/src/mbe/macro_check.rs
+++ b/compiler/rustc_expand/src/mbe/macro_check.rs
@@ -337,8 +337,12 @@ fn check_occurrences(
             let name = MacroRulesNormalizedIdent::new(name);
             check_ops_is_prefix(sess, node_id, macros, binders, ops, span, name);
         }
-        // FIXME(c410-f3r) Check token (https://github.com/rust-lang/rust/issues/93902)
-        TokenTree::MetaVarExpr(..) => {}
+        TokenTree::MetaVarExpr(dl, ref mve) => {
+            let Some(name) = mve.ident().map(MacroRulesNormalizedIdent::new) else {
+                return;
+            };
+            check_ops_is_prefix(sess, node_id, macros, binders, ops, dl.entire(), name);
+        }
         TokenTree::Delimited(_, ref del) => {
             check_nested_occurrences(sess, node_id, &del.tts, macros, binders, ops, valid);
         }
diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs
index dedfd779bb416..07081c75d46f0 100644
--- a/compiler/rustc_expand/src/mbe/macro_parser.rs
+++ b/compiler/rustc_expand/src/mbe/macro_parser.rs
@@ -324,8 +324,11 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
                 TokenTree::Delimited(_, ref delim) => count_names(&delim.tts),
                 TokenTree::MetaVar(..) => 0,
                 TokenTree::MetaVarDecl(..) => 1,
-                // FIXME(c410-f3r) MetaVarExpr should be handled instead of being ignored
-                // https://github.com/rust-lang/rust/issues/9390
+                // Panicking here would abort execution because `parse_tree` makes use of this
+                // function. In other words, RHS meta-variable expressions eventually end-up here.
+                //
+                // `0` is still returned to inform that no meta-variable was found. `Meta-variables
+                // != Meta-variable expressions`
                 TokenTree::MetaVarExpr(..) => 0,
                 TokenTree::Sequence(_, ref seq) => seq.num_captures,
                 TokenTree::Token(..) => 0,
diff --git a/compiler/rustc_expand/src/mbe/metavar_expr.rs b/compiler/rustc_expand/src/mbe/metavar_expr.rs
index 6c5a755da6f40..2949ca716b230 100644
--- a/compiler/rustc_expand/src/mbe/metavar_expr.rs
+++ b/compiler/rustc_expand/src/mbe/metavar_expr.rs
@@ -62,9 +62,9 @@ impl MetaVarExpr {
         Ok(rslt)
     }
 
-    crate fn ident(&self) -> Option<&Ident> {
-        match self {
-            MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(&ident),
+    crate fn ident(&self) -> Option<Ident> {
+        match *self {
+            MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
             MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None,
         }
     }
diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs
index 387d5895e24d7..5ec63739cf574 100644
--- a/compiler/rustc_expand/src/mbe/transcribe.rs
+++ b/compiler/rustc_expand/src/mbe/transcribe.rs
@@ -399,7 +399,7 @@ fn lockstep_iter_size(
         TokenTree::MetaVarExpr(_, ref expr) => {
             let default_rslt = LockstepIterSize::Unconstrained;
             let Some(ident) = expr.ident() else { return default_rslt; };
-            let name = MacroRulesNormalizedIdent::new(ident.clone());
+            let name = MacroRulesNormalizedIdent::new(ident);
             match lookup_cur_matched(name, interpolations, repeats) {
                 Some(MatchedSeq(ref ads)) => {
                     default_rslt.with(LockstepIterSize::Constraint(ads.len(), name))
@@ -479,7 +479,7 @@ fn count_repetitions<'a>(
     count(cx, 0, depth_opt, matched, sp)
 }
 
-/// Returns a `NamedMatch` item declared on the RHS given an arbitrary [Ident]
+/// Returns a `NamedMatch` item declared on the LHS given an arbitrary [Ident]
 fn matched_from_ident<'ctx, 'interp, 'rslt>(
     cx: &ExtCtxt<'ctx>,
     ident: Ident,