diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2022-03-28 16:08:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-28 16:08:07 +0200 |
| commit | ae037a86f981e2c7776440c0ac71f03f7ee65089 (patch) | |
| tree | aaf076b80cee52367c140ced2726732e30a8eac4 /compiler/rustc_parse/src | |
| parent | 0e4524e5b4a9c5656fef69f532f96eb9959a1803 (diff) | |
| parent | 364b908d574a8e416ceb18fac968d5142765e5e5 (diff) | |
| download | rust-ae037a86f981e2c7776440c0ac71f03f7ee65089.tar.gz rust-ae037a86f981e2c7776440c0ac71f03f7ee65089.zip | |
Rollup merge of #95301 - nnethercote:rm-NtTT, r=petrochenkov
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. r? `@petrochenkov`
Diffstat (limited to 'compiler/rustc_parse/src')
| -rw-r--r-- | compiler/rustc_parse/src/lib.rs | 1 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/mod.rs | 8 | ||||
| -rw-r--r-- | compiler/rustc_parse/src/parser/nonterminal.rs | 29 |
3 files changed, 24 insertions, 14 deletions
diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index 1118caf5075..28c2a63db27 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -290,7 +290,6 @@ pub fn nt_to_tokenstream( Nonterminal::NtMeta(ref attr) => convert_tokens(attr.tokens.as_ref()), Nonterminal::NtPath(ref path) => convert_tokens(path.tokens.as_ref()), Nonterminal::NtVis(ref vis) => convert_tokens(vis.tokens.as_ref()), - Nonterminal::NtTT(ref tt) => Some(tt.clone().into()), Nonterminal::NtExpr(ref expr) | Nonterminal::NtLiteral(ref expr) => { prepend_attrs(&expr.attrs, expr.tokens.as_ref()) } diff --git a/compiler/rustc_parse/src/parser/mod.rs b/compiler/rustc_parse/src/parser/mod.rs index 3a2f193d319..5d244ef9118 100644 --- a/compiler/rustc_parse/src/parser/mod.rs +++ b/compiler/rustc_parse/src/parser/mod.rs @@ -19,7 +19,7 @@ pub use pat::{CommaRecoveryMode, RecoverColon, RecoverComma}; pub use path::PathStyle; use rustc_ast::ptr::P; -use rustc_ast::token::{self, DelimToken, Token, TokenKind}; +use rustc_ast::token::{self, DelimToken, Nonterminal, Token, TokenKind}; use rustc_ast::tokenstream::AttributesData; use rustc_ast::tokenstream::{self, DelimSpan, Spacing}; use rustc_ast::tokenstream::{TokenStream, TokenTree}; @@ -1507,3 +1507,9 @@ pub enum FlatToken { /// handling of replace ranges. Empty, } + +#[derive(Debug)] +pub enum NtOrTt { + Nt(Nonterminal), + Tt(TokenTree), +} diff --git a/compiler/rustc_parse/src/parser/nonterminal.rs b/compiler/rustc_parse/src/parser/nonterminal.rs index 40902fa1833..c105fbfaee0 100644 --- a/compiler/rustc_parse/src/parser/nonterminal.rs +++ b/compiler/rustc_parse/src/parser/nonterminal.rs @@ -1,12 +1,12 @@ use rustc_ast::ptr::P; -use rustc_ast::token::{self, Nonterminal, NonterminalKind, Token}; +use rustc_ast::token::{self, NonterminalKind, Token}; use rustc_ast::AstLike; use rustc_ast_pretty::pprust; use rustc_errors::PResult; use rustc_span::symbol::{kw, Ident}; use crate::parser::pat::{CommaRecoveryMode, RecoverColon, RecoverComma}; -use crate::parser::{FollowedByType, ForceCollect, Parser, PathStyle}; +use crate::parser::{FollowedByType, ForceCollect, NtOrTt, Parser, PathStyle}; impl<'a> Parser<'a> { /// Checks whether a non-terminal may begin with a particular token. @@ -85,7 +85,7 @@ impl<'a> Parser<'a> { NonterminalKind::Lifetime => match token.kind { token::Lifetime(_) => true, token::Interpolated(ref nt) => { - matches!(**nt, token::NtLifetime(_) | token::NtTT(_)) + matches!(**nt, token::NtLifetime(_)) } _ => false, }, @@ -96,7 +96,7 @@ impl<'a> Parser<'a> { } /// Parse a non-terminal (e.g. MBE `:pat` or `:ident`). - pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, Nonterminal> { + pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, NtOrTt> { // Any `Nonterminal` which stores its tokens (currently `NtItem` and `NtExpr`) // needs to have them force-captured here. // A `macro_rules!` invocation may pass a captured item/expr to a proc-macro, @@ -104,6 +104,8 @@ impl<'a> Parser<'a> { // in advance whether or not a proc-macro will be (transitively) invoked, // we always capture tokens for any `Nonterminal` which needs them. let mut nt = match kind { + // Note that TT is treated differently to all the others. + NonterminalKind::TT => return Ok(NtOrTt::Tt(self.parse_token_tree())), NonterminalKind::Item => match self.parse_item(ForceCollect::Yes)? { Some(item) => token::NtItem(item), None => { @@ -124,9 +126,12 @@ impl<'a> Parser<'a> { NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr { .. } => { token::NtPat(self.collect_tokens_no_attrs(|this| match kind { NonterminalKind::PatParam { .. } => this.parse_pat_no_top_alt(None), - NonterminalKind::PatWithOr { .. } => { - this.parse_pat_allow_top_alt(None, RecoverComma::No, RecoverColon::No, CommaRecoveryMode::EitherTupleOrPipe) - } + NonterminalKind::PatWithOr { .. } => this.parse_pat_allow_top_alt( + None, + RecoverComma::No, + RecoverColon::No, + CommaRecoveryMode::EitherTupleOrPipe, + ), _ => unreachable!(), })?) } @@ -139,9 +144,10 @@ impl<'a> Parser<'a> { ) } - NonterminalKind::Ty => { - token::NtTy(self.collect_tokens_no_attrs(|this| this.parse_no_question_mark_recover())?) - } + NonterminalKind::Ty => token::NtTy( + self.collect_tokens_no_attrs(|this| this.parse_no_question_mark_recover())?, + ), + // this could be handled like a token, since it is one NonterminalKind::Ident if let Some((ident, is_raw)) = get_macro_ident(&self.token) => @@ -158,7 +164,6 @@ impl<'a> Parser<'a> { self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?, ), NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item(true)?)), - NonterminalKind::TT => token::NtTT(self.parse_token_tree()), NonterminalKind::Vis => token::NtVis( self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?, ), @@ -183,7 +188,7 @@ impl<'a> Parser<'a> { ); } - Ok(nt) + Ok(NtOrTt::Nt(nt)) } } |
