Skip to content

Commit f7df213

Browse files
committed
Remove NtTy.
Notes about tests: - tests/ui/parser/macro/trait-object-macro-matcher.rs: the syntax error is duplicated, because it occurs now when parsing the decl macro input, and also when parsing the expanded decl macro. But this won't show up for normal users due to error de-duplication. - The output of stringify! is uglier, due to `print_tts` producing uglier output than AST pretty-printing.
1 parent cbec0e1 commit f7df213

20 files changed

+112
-77
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ impl HasTokens for Nonterminal {
235235
Nonterminal::NtStmt(stmt) => stmt.tokens(),
236236
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
237237
Nonterminal::NtPat(pat) => pat.tokens(),
238-
Nonterminal::NtTy(ty) => ty.tokens(),
239238
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
240239
Nonterminal::NtPath(path) => path.tokens(),
241240
Nonterminal::NtBlock(block) => block.tokens(),
@@ -248,7 +247,6 @@ impl HasTokens for Nonterminal {
248247
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
249248
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
250249
Nonterminal::NtPat(pat) => pat.tokens_mut(),
251-
Nonterminal::NtTy(ty) => ty.tokens_mut(),
252250
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
253251
Nonterminal::NtPath(path) => path.tokens_mut(),
254252
Nonterminal::NtBlock(block) => block.tokens_mut(),

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
804804
}),
805805
token::NtPat(pat) => vis.visit_pat(pat),
806806
token::NtExpr(expr) => vis.visit_expr(expr),
807-
token::NtTy(ty) => vis.visit_ty(ty),
808807
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
809808
token::NtLifetime(ident) => vis.visit_ident(ident),
810809
token::NtLiteral(expr) => vis.visit_expr(expr),

compiler/rustc_ast/src/token.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,16 @@ impl Token {
508508
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
509509
OpenDelim(Delimiter::Parenthesis) | // tuple
510510
OpenDelim(Delimiter::Bracket) | // array
511-
Not | // never
512-
BinOp(Star) | // raw pointer
513-
BinOp(And) | // reference
514-
AndAnd | // double reference
515-
Question | // maybe bound in trait object
516-
Lifetime(..) | // lifetime bound in trait object
517-
Lt | BinOp(Shl) | // associated path
518-
ModSep => true, // global path
519-
Interpolated(ref nt) => matches!(**nt, NtTy(..) | NtPath(..)),
511+
Not | // never
512+
BinOp(Star) | // raw pointer
513+
BinOp(And) | // reference
514+
AndAnd | // double reference
515+
Question | // maybe bound in trait object
516+
Lifetime(..) | // lifetime bound in trait object
517+
Lt | BinOp(Shl) | // associated path
518+
ModSep => true, // global path
519+
Interpolated(ref nt) => matches!(**nt, NtPath(..)),
520+
OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Ty))) => true,
520521
_ => false,
521522
}
522523
}
@@ -846,7 +847,6 @@ pub enum Nonterminal {
846847
NtStmt(P<ast::Stmt>),
847848
NtPat(P<ast::Pat>),
848849
NtExpr(P<ast::Expr>),
849-
NtTy(P<ast::Ty>),
850850
NtIdent(Ident, /* is_raw */ bool),
851851
NtLifetime(Ident),
852852
NtLiteral(P<ast::Expr>),
@@ -941,7 +941,6 @@ impl Nonterminal {
941941
NtStmt(stmt) => stmt.span,
942942
NtPat(pat) => pat.span,
943943
NtExpr(expr) | NtLiteral(expr) => expr.span,
944-
NtTy(ty) => ty.span,
945944
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
946945
NtMeta(attr_item) => attr_item.span(),
947946
NtPath(path) => path.span,
@@ -973,7 +972,6 @@ impl fmt::Debug for Nonterminal {
973972
NtStmt(..) => f.pad("NtStmt(..)"),
974973
NtPat(..) => f.pad("NtPat(..)"),
975974
NtExpr(..) => f.pad("NtExpr(..)"),
976-
NtTy(..) => f.pad("NtTy(..)"),
977975
NtIdent(..) => f.pad("NtIdent(..)"),
978976
NtLiteral(..) => f.pad("NtLiteral(..)"),
979977
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ impl TokenStream {
471471
}
472472
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
473473
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
474-
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
475474
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
476475
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
477476
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
728728
match nt {
729729
token::NtExpr(e) => self.expr_to_string(e),
730730
token::NtMeta(e) => self.attr_item_to_string(e),
731-
token::NtTy(e) => self.ty_to_string(e),
732731
token::NtPath(e) => self.path_to_string(e),
733732
token::NtItem(e) => self.item_to_string(e),
734733
token::NtBlock(e) => self.block_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ pub(super) fn transcribe<'a>(
232232
// without wrapping them into groups.
233233
tt.clone()
234234
}
235+
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
236+
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
237+
}
235238
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
236239
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
237240
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,17 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
141141
($self: expr, $allow_qpath_recovery: expr) => {
142142
if $allow_qpath_recovery
143143
&& $self.may_recover()
144-
&& $self.look_ahead(1, |t| t == &token::ModSep)
145-
&& let token::Interpolated(nt) = &$self.token.kind
146-
&& let token::NtTy(ty) = &**nt
144+
&& let Some(token::NonterminalKind::Ty) = $self.token.is_metavar_seq()
145+
&& $self.check_noexpect_past_close_delim(&token::ModSep)
147146
{
148-
let ty = ty.clone();
149-
$self.bump();
147+
// Reparse the type, then move to recovery. `unwrap` is
148+
// safe because we found `InvisibleSource::MetaVar` above.
149+
let ty = crate::reparse_metavar_seq!(
150+
$self,
151+
token::NonterminalKind::Ty,
152+
super::ParseNtResult::Ty(ty),
153+
ty
154+
);
150155
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
151156
}
152157
};
@@ -574,6 +579,24 @@ impl<'a> Parser<'a> {
574579
self.token == *tok
575580
}
576581

582+
// Check the first token after the delimiter that closes the current
583+
// delimited sequence. (Panics if used in the outermost token stream, which
584+
// has no delimiters.) It uses a clone of the relevant tree cursor to skip
585+
// past the entire `TokenTree::Delimited` in a single step, avoiding the
586+
// need for unbounded token lookahead.
587+
//
588+
// Primarily used when `self.token` matches
589+
// `OpenDelim(Delimiter::Invisible(_))`, to look ahead through the current
590+
// metavar expansion.
591+
fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
592+
let mut tree_cursor = self.token_cursor.stack.last().unwrap().0.clone();
593+
let tt = tree_cursor.next_ref();
594+
matches!(
595+
tt,
596+
Some(ast::tokenstream::TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
597+
)
598+
}
599+
577600
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
578601
///
579602
/// the main purpose of this function is to reduce the cluttering of the suggestions list
@@ -1558,6 +1581,7 @@ pub enum FlatToken {
15581581
#[derive(Clone, Debug)]
15591582
pub enum ParseNtResult {
15601583
Tt(TokenTree),
1584+
Ty(P<ast::Ty>),
15611585
Vis(P<ast::Visibility>),
15621586

15631587
/// This case will eventually be removed, along with `Token::Interpolate`.

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ impl<'a> Parser<'a> {
4848
NtStmt(_)
4949
| NtPat(_)
5050
| NtExpr(_)
51-
| NtTy(_)
5251
| NtIdent(..)
5352
| NtLiteral(_) // `true`, `false`
5453
| NtMeta(_)
@@ -83,7 +82,7 @@ impl<'a> Parser<'a> {
8382
token::OpenDelim(Delimiter::Brace) => true,
8483
token::Interpolated(nt) => match **nt {
8584
NtBlock(_) | NtLifetime(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
86-
NtItem(_) | NtPat(_) | NtTy(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
85+
NtItem(_) | NtPat(_) | NtIdent(..) | NtMeta(_) | NtPath(_) => false,
8786
},
8887
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(k))) => match k {
8988
NonterminalKind::Block
@@ -201,9 +200,9 @@ impl<'a> Parser<'a> {
201200
)
202201
}
203202

204-
NonterminalKind::Ty => NtTy(
205-
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
206-
),
203+
NonterminalKind::Ty => return Ok(ParseNtResult::Ty(
204+
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?
205+
)),
207206

208207
// this could be handled like a token, since it is one
209208
NonterminalKind::Ident

compiler/rustc_parse/src/parser/path.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
2-
use super::{Parser, Restrictions, TokenType};
2+
use super::{ParseNtResult, Parser, Restrictions, TokenType};
33
use crate::errors::PathSingleColon;
44
use crate::{errors, maybe_whole};
55
use rustc_ast::ptr::P;
6-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
6+
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
77
use rustc_ast::{
88
self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocConstraint,
99
AssocConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs,
@@ -184,14 +184,14 @@ impl<'a> Parser<'a> {
184184
path.into_inner()
185185
});
186186

187-
if let token::Interpolated(nt) = &self.token.kind {
188-
if let token::NtTy(ty) = &**nt {
189-
if let ast::TyKind::Path(None, path) = &ty.kind {
190-
let path = path.clone();
191-
self.bump();
192-
reject_generics_if_mod_style(self, &path);
193-
return Ok(path);
194-
}
187+
if let Some(NonterminalKind::Ty) = self.token.is_metavar_seq() {
188+
let mut self2 = self.clone();
189+
let ty =
190+
crate::reparse_metavar_seq!(self2, NonterminalKind::Ty, ParseNtResult::Ty(ty), ty);
191+
if let ast::TyKind::Path(None, path) = ty.into_inner().kind {
192+
*self = self2;
193+
reject_generics_if_mod_style(self, &path);
194+
return Ok(path);
195195
}
196196
}
197197

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use super::{Parser, PathStyle, TokenType};
1+
use super::{ParseNtResult, Parser, PathStyle, TokenType};
22

33
use crate::errors::{
44
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
55
FnPointerCannotBeAsync, FnPointerCannotBeConst, FnPtrWithGenerics, FnPtrWithGenericsSugg,
66
InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime, NestedCVariadicType,
77
ReturnTypesUseThinArrow,
88
};
9-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
9+
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_reparse_metavar_seq};
1010

1111
use ast::DUMMY_NODE_ID;
1212
use rustc_ast::ptr::P;
13-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
13+
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
1414
use rustc_ast::util::case::Case;
1515
use rustc_ast::{
1616
self as ast, BareFnTy, BoundPolarity, FnRetTy, GenericBound, GenericBounds, GenericParam,
@@ -191,7 +191,8 @@ impl<'a> Parser<'a> {
191191
)
192192
}
193193

194-
/// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
194+
/// Parse a type without recovering `:` as `->` to avoid breaking code such
195+
/// as `where fn() : for<'a>`.
195196
pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
196197
self.parse_ty_common(
197198
AllowPlus::Yes,
@@ -251,7 +252,16 @@ impl<'a> Parser<'a> {
251252
) -> PResult<'a, P<Ty>> {
252253
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
253254
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
254-
maybe_whole!(self, NtTy, |x| x);
255+
256+
if let Some(ty) = maybe_reparse_metavar_seq!(
257+
self,
258+
NonterminalKind::Ty,
259+
NonterminalKind::Ty,
260+
ParseNtResult::Ty(ty),
261+
ty
262+
) {
263+
return Ok(ty);
264+
}
255265

256266
let lo = self.token.span;
257267
let mut impl_dyn_multi = false;

0 commit comments

Comments
 (0)