From 9ab4f732cb52eb07ea64a7f65038973bc4e0156c Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Thu, 31 Mar 2022 18:56:40 +0300 Subject: [PATCH] expand: Do not count metavar declarations on RHS of `macro_rules` They are 0 by definition there. --- compiler/rustc_expand/src/mbe/macro_parser.rs | 18 ++++++------------ compiler/rustc_expand/src/mbe/quoted.rs | 7 ++++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_expand/src/mbe/macro_parser.rs b/compiler/rustc_expand/src/mbe/macro_parser.rs index 18302ffdb8e10..0086983f3d984 100644 --- a/compiler/rustc_expand/src/mbe/macro_parser.rs +++ b/compiler/rustc_expand/src/mbe/macro_parser.rs @@ -263,18 +263,12 @@ crate type NamedParseResult = ParseResult usize { matcher .iter() - .map(|tt| { - match tt { - TokenTree::Delimited(_, delim) => count_metavar_decls(delim.inner_tts()), - TokenTree::MetaVar(..) => 0, - TokenTree::MetaVarDecl(..) => 1, - // 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, - } + .map(|tt| match tt { + TokenTree::MetaVarDecl(..) => 1, + TokenTree::Sequence(_, seq) => seq.num_captures, + TokenTree::Delimited(_, delim) => count_metavar_decls(delim.inner_tts()), + TokenTree::Token(..) => 0, + TokenTree::MetaVar(..) | TokenTree::MetaVarExpr(..) => unreachable!(), }) .sum() } diff --git a/compiler/rustc_expand/src/mbe/quoted.rs b/compiler/rustc_expand/src/mbe/quoted.rs index d91871c7e57db..48abbd7c18e14 100644 --- a/compiler/rustc_expand/src/mbe/quoted.rs +++ b/compiler/rustc_expand/src/mbe/quoted.rs @@ -1,4 +1,4 @@ -use crate::mbe::macro_parser; +use crate::mbe::macro_parser::count_metavar_decls; use crate::mbe::{Delimited, KleeneOp, KleeneToken, MetaVarExpr, SequenceRepetition, TokenTree}; use rustc_ast::token::{self, Token}; @@ -211,14 +211,15 @@ 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_metavar_decls(&sequence); + let num_captures = + if parsing_patterns { count_metavar_decls(&sequence) } else { 0 }; TokenTree::Sequence( delim_span, Lrc::new(SequenceRepetition { tts: sequence, separator, kleene, - num_captures: name_captures, + num_captures, }), ) }