Remove Nonterminal::NtTT.

It's only needed for macro expansion, not as a general element in the
AST. This commit removes it, adds `NtOrTt` for the parser and macro
expansion cases, and renames the variants in `NamedMatch` to better
match the new type.
This commit is contained in:
Nicholas Nethercote 2022-03-25 12:39:12 +11:00
parent 8a0c55046c
commit 364b908d57
10 changed files with 52 additions and 55 deletions

View file

@ -76,7 +76,7 @@ crate use ParseResult::*;
use crate::mbe::{self, SequenceRepetition, TokenTree};
use rustc_ast::token::{self, DocComment, Nonterminal, Token};
use rustc_parse::parser::Parser;
use rustc_parse::parser::{NtOrTt, Parser};
use rustc_session::parse::ParseSess;
use rustc_span::symbol::MacroRulesNormalizedIdent;
@ -275,7 +275,7 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
}
/// `NamedMatch` is a pattern-match result for a single metavar. All
/// `MatchedNtNonTt`s in the `NamedMatch` have the same non-terminal type
/// `MatchedNonterminal`s in the `NamedMatch` have the same non-terminal type
/// (expr, item, etc).
///
/// The in-memory structure of a particular `NamedMatch` represents the match
@ -306,17 +306,17 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
/// ```rust
/// MatchedSeq([
/// MatchedSeq([
/// MatchedNtNonTt(a),
/// MatchedNtNonTt(b),
/// MatchedNtNonTt(c),
/// MatchedNtNonTt(d),
/// MatchedNonterminal(a),
/// MatchedNonterminal(b),
/// MatchedNonterminal(c),
/// MatchedNonterminal(d),
/// ]),
/// MatchedSeq([
/// MatchedNtNonTt(a),
/// MatchedNtNonTt(b),
/// MatchedNtNonTt(c),
/// MatchedNtNonTt(d),
/// MatchedNtNonTt(e),
/// MatchedNonterminal(a),
/// MatchedNonterminal(b),
/// MatchedNonterminal(c),
/// MatchedNonterminal(d),
/// MatchedNonterminal(e),
/// ])
/// ])
/// ```
@ -324,14 +324,11 @@ pub(super) fn count_names(ms: &[TokenTree]) -> usize {
crate enum NamedMatch {
MatchedSeq(Lrc<NamedMatchVec>),
// This variant should never hold an `NtTT`. `MatchedNtTt` should be used
// for that case.
MatchedNtNonTt(Lrc<Nonterminal>),
// A metavar match of type `tt`.
MatchedTokenTree(rustc_ast::tokenstream::TokenTree),
// `NtTT` is handled without any cloning when transcribing, unlike other
// nonterminals. Therefore, an `Lrc` isn't helpful and causes unnecessary
// allocations. Hence this separate variant.
MatchedNtTt(rustc_ast::tokenstream::TokenTree),
// A metavar match of any type other than `tt`.
MatchedNonterminal(Lrc<Nonterminal>),
}
/// Takes a slice of token trees `ms` representing a matcher which successfully matched input
@ -677,8 +674,8 @@ impl<'tt> TtParser<'tt> {
Ok(nt) => nt,
};
let m = match nt {
Nonterminal::NtTT(tt) => MatchedNtTt(tt),
_ => MatchedNtNonTt(Lrc::new(nt)),
NtOrTt::Nt(nt) => MatchedNonterminal(Lrc::new(nt)),
NtOrTt::Tt(tt) => MatchedTokenTree(tt),
};
item.push_match(match_cur, m);
item.idx += 1;

View file

@ -4,7 +4,7 @@ use crate::expand::{ensure_complete_parse, parse_ast_fragment, AstFragment, AstF
use crate::mbe;
use crate::mbe::macro_check;
use crate::mbe::macro_parser::{Error, ErrorReported, Failure, Success, TtParser};
use crate::mbe::macro_parser::{MatchedNtTt, MatchedSeq};
use crate::mbe::macro_parser::{MatchedSeq, MatchedTokenTree};
use crate::mbe::transcribe::transcribe;
use rustc_ast as ast;
@ -470,7 +470,7 @@ pub fn compile_declarative_macro(
MatchedSeq(ref s) => s
.iter()
.map(|m| {
if let MatchedNtTt(ref tt) = *m {
if let MatchedTokenTree(ref tt) = *m {
let mut tts = vec![];
mbe::quoted::parse(
tt.clone().into(),
@ -495,7 +495,7 @@ pub fn compile_declarative_macro(
MatchedSeq(ref s) => s
.iter()
.map(|m| {
if let MatchedNtTt(ref tt) = *m {
if let MatchedTokenTree(ref tt) = *m {
let mut tts = vec![];
mbe::quoted::parse(
tt.clone().into(),

View file

@ -1,8 +1,8 @@
use crate::base::ExtCtxt;
use crate::mbe::macro_parser::{MatchedNtNonTt, MatchedNtTt, MatchedSeq, NamedMatch};
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
use crate::mbe::{self, MetaVarExpr};
use rustc_ast::mut_visit::{self, MutVisitor};
use rustc_ast::token::{self, Nonterminal, Token, TokenKind};
use rustc_ast::token::{self, Token, TokenKind};
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndSpacing};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lrc;
@ -234,17 +234,16 @@ pub(super) fn transcribe<'a>(
let ident = MacroRulesNormalizedIdent::new(orignal_ident);
if let Some(cur_matched) = lookup_cur_matched(ident, interp, &repeats) {
match cur_matched {
MatchedNtTt(ref tt) => {
MatchedTokenTree(ref tt) => {
// `tt`s are emitted into the output stream directly as "raw tokens",
// without wrapping them into groups.
let token = tt.clone();
result.push(token.into());
}
MatchedNtNonTt(ref nt) => {
MatchedNonterminal(ref nt) => {
// Other variables are emitted into the output stream as groups with
// `Delimiter::None` to maintain parsing priorities.
// `Interpolated` is currently used for such groups in rustc parser.
debug_assert!(!matches!(**nt, Nonterminal::NtTT(_)));
marker.visit_span(&mut sp);
let token = TokenTree::token(token::Interpolated(nt.clone()), sp);
result.push(token.into());
@ -312,7 +311,7 @@ fn lookup_cur_matched<'a>(
let mut matched = matched;
for &(idx, _) in repeats {
match matched {
MatchedNtTt(_) | MatchedNtNonTt(_) => break,
MatchedTokenTree(_) | MatchedNonterminal(_) => break,
MatchedSeq(ref ads) => matched = ads.get(idx).unwrap(),
}
}
@ -402,7 +401,7 @@ fn lockstep_iter_size(
let name = MacroRulesNormalizedIdent::new(name);
match lookup_cur_matched(name, interpolations, repeats) {
Some(matched) => match matched {
MatchedNtTt(_) | MatchedNtNonTt(_) => LockstepIterSize::Unconstrained,
MatchedTokenTree(_) | MatchedNonterminal(_) => LockstepIterSize::Unconstrained,
MatchedSeq(ref ads) => LockstepIterSize::Constraint(ads.len(), name),
},
_ => LockstepIterSize::Unconstrained,
@ -449,7 +448,7 @@ fn count_repetitions<'a>(
sp: &DelimSpan,
) -> PResult<'a, usize> {
match matched {
MatchedNtTt(_) | MatchedNtNonTt(_) => {
MatchedTokenTree(_) | MatchedNonterminal(_) => {
if declared_lhs_depth == 0 {
return Err(cx.struct_span_err(
sp.entire(),