about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2022-03-25 16:23:26 +1100
committerNicholas Nethercote <n.nethercote@gmail.com>2022-03-30 10:42:34 +1100
commit2b60cc081b31c48a5d9037ba559b6be32974a14f (patch)
tree13d4346cef347fc4b445db298be4a9ca1daf1084
parentdf6ead557d6c1f2ec87793d95b8a8bc9453e0f66 (diff)
downloadrust-2b60cc081b31c48a5d9037ba559b6be32974a14f.tar.gz
rust-2b60cc081b31c48a5d9037ba559b6be32974a14f.zip
Simplify and rename `count_names`.
-rw-r--r--compiler/rustc_expand/src/mbe/macro_parser.rs26
-rw-r--r--compiler/rustc_expand/src/mbe/quoted.rs2
2 files changed, 14 insertions, 14 deletions
diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs
index 69da3f232a0..9d134e398cc 100644
--- a/compiler/rustc_expand/src/mbe/macro_parser.rs
+++ b/compiler/rustc_expand/src/mbe/macro_parser.rs
@@ -177,7 +177,7 @@ impl<'tt> MatcherPos<'tt> {
     /// Generates the top-level matcher position in which the "dot" is before the first token of
     /// the matcher `ms`.
     fn new(ms: &'tt [TokenTree]) -> Self {
-        let match_idx_hi = count_names(ms);
+        let match_idx_hi = count_metavar_decls(ms);
         MatcherPos {
             // Start with the top level matcher given to us.
             top_elts: ms,
@@ -254,24 +254,24 @@ crate enum ParseResult<T> {
 /// of metavars to the token trees they bind to.
 crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, NamedMatch>>;
 
-/// Count how many metavars are named in the given matcher `ms`.
-pub(super) fn count_names(ms: &[TokenTree]) -> usize {
-    ms.iter().fold(0, |count, elt| {
-        count
-            + match elt {
-                TokenTree::Delimited(_, delim) => count_names(delim.inner_tts()),
+/// Count how many metavars declarations are in `matcher`.
+pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
+    matcher
+        .iter()
+        .map(|tt| {
+            match tt {
+                TokenTree::Delimited(_, delim) => count_metavar_decls(delim.inner_tts()),
                 TokenTree::MetaVar(..) => 0,
                 TokenTree::MetaVarDecl(..) => 1,
-                // 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`
+                // RHS meta-variable expressions eventually end-up here. `0` is returned to inform
+                // that no meta-variable was found, because "meta-variables" != "meta-variable
+                // expressions".
                 TokenTree::MetaVarExpr(..) => 0,
                 TokenTree::Sequence(_, seq) => seq.num_captures,
                 TokenTree::Token(..) => 0,
             }
-    })
+        })
+        .sum()
 }
 
 /// `NamedMatch` is a pattern-match result for a single metavar. All
diff --git a/compiler/rustc_expand/src/mbe/quoted.rs b/compiler/rustc_expand/src/mbe/quoted.rs
index b3ed6b8e4db..d91871c7e57 100644
--- a/compiler/rustc_expand/src/mbe/quoted.rs
+++ b/compiler/rustc_expand/src/mbe/quoted.rs
@@ -211,7 +211,7 @@ fn parse_tree(
                     let (separator, kleene) =
                         parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess);
                     // Count the number of captured "names" (i.e., named metavars)
-                    let name_captures = macro_parser::count_names(&sequence);
+                    let name_captures = macro_parser::count_metavar_decls(&sequence);
                     TokenTree::Sequence(
                         delim_span,
                         Lrc::new(SequenceRepetition {