Skip to content

Commit 2b60cc0

Browse files
committed
Simplify and rename count_names.
1 parent df6ead5 commit 2b60cc0

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl<'tt> MatcherPos<'tt> {
177177
/// Generates the top-level matcher position in which the "dot" is before the first token of
178178
/// the matcher `ms`.
179179
fn new(ms: &'tt [TokenTree]) -> Self {
180-
let match_idx_hi = count_names(ms);
180+
let match_idx_hi = count_metavar_decls(ms);
181181
MatcherPos {
182182
// Start with the top level matcher given to us.
183183
top_elts: ms,
@@ -254,24 +254,24 @@ crate enum ParseResult<T> {
254254
/// of metavars to the token trees they bind to.
255255
crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, NamedMatch>>;
256256

257-
/// Count how many metavars are named in the given matcher `ms`.
258-
pub(super) fn count_names(ms: &[TokenTree]) -> usize {
259-
ms.iter().fold(0, |count, elt| {
260-
count
261-
+ match elt {
262-
TokenTree::Delimited(_, delim) => count_names(delim.inner_tts()),
257+
/// Count how many metavars declarations are in `matcher`.
258+
pub(super) fn count_metavar_decls(matcher: &[TokenTree]) -> usize {
259+
matcher
260+
.iter()
261+
.map(|tt| {
262+
match tt {
263+
TokenTree::Delimited(_, delim) => count_metavar_decls(delim.inner_tts()),
263264
TokenTree::MetaVar(..) => 0,
264265
TokenTree::MetaVarDecl(..) => 1,
265-
// Panicking here would abort execution because `parse_tree` makes use of this
266-
// function. In other words, RHS meta-variable expressions eventually end-up here.
267-
//
268-
// `0` is still returned to inform that no meta-variable was found. `Meta-variables
269-
// != Meta-variable expressions`
266+
// RHS meta-variable expressions eventually end-up here. `0` is returned to inform
267+
// that no meta-variable was found, because "meta-variables" != "meta-variable
268+
// expressions".
270269
TokenTree::MetaVarExpr(..) => 0,
271270
TokenTree::Sequence(_, seq) => seq.num_captures,
272271
TokenTree::Token(..) => 0,
273272
}
274-
})
273+
})
274+
.sum()
275275
}
276276

277277
/// `NamedMatch` is a pattern-match result for a single metavar. All

compiler/rustc_expand/src/mbe/quoted.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn parse_tree(
211211
let (separator, kleene) =
212212
parse_sep_and_kleene_op(&mut trees, delim_span.entire(), sess);
213213
// Count the number of captured "names" (i.e., named metavars)
214-
let name_captures = macro_parser::count_names(&sequence);
214+
let name_captures = macro_parser::count_metavar_decls(&sequence);
215215
TokenTree::Sequence(
216216
delim_span,
217217
Lrc::new(SequenceRepetition {

0 commit comments

Comments
 (0)