Skip to content

Commit

Permalink
Rollup merge of rust-lang#95531 - petrochenkov:metacount, r=nnethercote
Browse files Browse the repository at this point in the history
expand: Do not count metavar declarations on RHS of `macro_rules`

They are 0 by definition there.

Addresses rust-lang#95425 (comment)
r? `@nnethercote`
  • Loading branch information
matthiaskrgr authored Mar 31, 2022
2 parents 6facf7a + 9ab4f73 commit 7061eda
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
18 changes: 6 additions & 12 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,12 @@ crate type NamedParseResult = ParseResult<FxHashMap<MacroRulesNormalizedIdent, N
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,
// 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()
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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,
}),
)
}
Expand Down

0 comments on commit 7061eda

Please sign in to comment.