Skip to content

Commit 6bb707a

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. - tests/ui/associated-consts/issue-93835.rs: ditto.
1 parent 31943c1 commit 6bb707a

21 files changed

+97
-48
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
@@ -838,7 +838,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
838838
}),
839839
token::NtPat(pat) => vis.visit_pat(pat),
840840
token::NtExpr(expr) => vis.visit_expr(expr),
841-
token::NtTy(ty) => vis.visit_ty(ty),
842841
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
843842
token::NtLifetime(ident) => vis.visit_ident(ident),
844843
token::NtLiteral(expr) => vis.visit_expr(expr),

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ impl Token {
576576
Lifetime(..) | // lifetime bound in trait object
577577
Lt | BinOp(Shl) | // associated path
578578
PathSep => true, // global path
579-
Interpolated(ref nt) => matches!(&nt.0, NtTy(..) | NtPath(..)),
579+
Interpolated(ref nt) => matches!(&nt.0, NtPath(..)),
580580
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
581581
NonterminalKind::Ty |
582582
NonterminalKind::Path
@@ -919,7 +919,6 @@ pub enum Nonterminal {
919919
NtStmt(P<ast::Stmt>),
920920
NtPat(P<ast::Pat>),
921921
NtExpr(P<ast::Expr>),
922-
NtTy(P<ast::Ty>),
923922
NtIdent(Ident, IdentIsRaw),
924923
NtLifetime(Ident),
925924
NtLiteral(P<ast::Expr>),
@@ -1014,7 +1013,6 @@ impl Nonterminal {
10141013
NtStmt(stmt) => stmt.span,
10151014
NtPat(pat) => pat.span,
10161015
NtExpr(expr) | NtLiteral(expr) => expr.span,
1017-
NtTy(ty) => ty.span,
10181016
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
10191017
NtMeta(attr_item) => attr_item.span(),
10201018
NtPath(path) => path.span,
@@ -1029,7 +1027,6 @@ impl Nonterminal {
10291027
NtPat(..) => "pattern",
10301028
NtExpr(..) => "expression",
10311029
NtLiteral(..) => "literal",
1032-
NtTy(..) => "type",
10331030
NtIdent(..) => "identifier",
10341031
NtLifetime(..) => "lifetime",
10351032
NtMeta(..) => "attribute",
@@ -1062,7 +1059,6 @@ impl fmt::Debug for Nonterminal {
10621059
NtStmt(..) => f.pad("NtStmt(..)"),
10631060
NtPat(..) => f.pad("NtPat(..)"),
10641061
NtExpr(..) => f.pad("NtExpr(..)"),
1065-
NtTy(..) => f.pad("NtTy(..)"),
10661062
NtIdent(..) => f.pad("NtIdent(..)"),
10671063
NtLiteral(..) => f.pad("NtLiteral(..)"),
10681064
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,6 @@ impl TokenStream {
481481
}
482482
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
483483
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
484-
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
485484
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
486485
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
487486
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
@@ -846,7 +846,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
846846
match nt {
847847
token::NtExpr(e) => self.expr_to_string(e),
848848
token::NtMeta(e) => self.attr_item_to_string(e),
849-
token::NtTy(e) => self.ty_to_string(e),
850849
token::NtPath(e) => self.path_to_string(e),
851850
token::NtItem(e) => self.item_to_string(e),
852851
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
@@ -266,6 +266,9 @@ pub(super) fn transcribe<'a>(
266266
// without wrapping them into groups.
267267
maybe_use_metavar_location(cx, &stack, sp, tt, &mut marker)
268268
}
269+
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
270+
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
271+
}
269272
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
270273
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
271274
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,16 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
144144
($self: expr, $allow_qpath_recovery: expr) => {
145145
if $allow_qpath_recovery
146146
&& $self.may_recover()
147-
&& $self.look_ahead(1, |t| t == &token::PathSep)
148-
&& let token::Interpolated(nt) = &$self.token.kind
149-
&& let token::NtTy(ty) = &nt.0
147+
&& let Some(token::NonterminalKind::Ty) = $self.token.is_metavar_seq()
148+
&& $self.check_noexpect_past_close_delim(&token::PathSep)
150149
{
151-
let ty = ty.clone();
152-
$self.bump();
150+
// Reparse the type, then move to recovery.
151+
let ty = crate::reparse_metavar_seq!(
152+
$self,
153+
token::NonterminalKind::Ty,
154+
super::ParseNtResult::Ty(ty),
155+
ty
156+
);
153157
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
154158
}
155159
};
@@ -610,6 +614,24 @@ impl<'a> Parser<'a> {
610614
self.token == *tok
611615
}
612616

617+
// Check the first token after the delimiter that closes the current
618+
// delimited sequence. (Panics if used in the outermost token stream, which
619+
// has no delimiters.) It uses a clone of the relevant tree cursor to skip
620+
// past the entire `TokenTree::Delimited` in a single step, avoiding the
621+
// need for unbounded token lookahead.
622+
//
623+
// Primarily used when `self.token` matches
624+
// `OpenDelim(Delimiter::Invisible(_))`, to look ahead through the current
625+
// metavar expansion.
626+
fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
627+
let mut tree_cursor = self.token_cursor.stack.last().unwrap().0.clone();
628+
let tt = tree_cursor.next_ref();
629+
matches!(
630+
tt,
631+
Some(ast::tokenstream::TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
632+
)
633+
}
634+
613635
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
614636
///
615637
/// the main purpose of this function is to reduce the cluttering of the suggestions list
@@ -1647,6 +1669,7 @@ pub enum FlatToken {
16471669
#[derive(Clone, Debug)]
16481670
pub enum ParseNtResult<NtType> {
16491671
Tt(TokenTree),
1672+
Ty(P<ast::Ty>),
16501673
Vis(P<ast::Visibility>),
16511674

16521675
/// This variant will eventually be removed, along with `Token::Interpolate`.
@@ -1660,6 +1683,7 @@ impl<T> ParseNtResult<T> {
16601683
{
16611684
match self {
16621685
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
1686+
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
16631687
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16641688
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
16651689
}

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 4 additions & 3 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.0 {
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(InvisibleOrigin::MetaVar(k))) => match k {
8988
NonterminalKind::Block
@@ -203,7 +202,9 @@ impl<'a> Parser<'a> {
203202
}
204203

205204
NonterminalKind::Ty => {
206-
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
205+
return Ok(ParseNtResult::Ty(
206+
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
207+
));
207208
}
208209

209210
// this could be handled like a token, since it is one

compiler/rustc_parse/src/parser/path.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
55
use crate::{errors, maybe_whole};
66
use ast::token::IdentIsRaw;
77
use rustc_ast::ptr::P;
8-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
8+
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
99
use rustc_ast::{
1010
self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocConstraint,
1111
AssocConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs,
@@ -186,14 +186,14 @@ impl<'a> Parser<'a> {
186186
path.into_inner()
187187
});
188188

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

compiler/rustc_parse/src/parser/ty.rs

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

33
use crate::errors::{
44
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
55
FnPointerCannotBeAsync, FnPointerCannotBeConst, FnPtrWithGenerics, FnPtrWithGenericsSugg,
66
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
77
NestedCVariadicType, 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 rustc_ast::ptr::P;
12-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
12+
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
1313
use rustc_ast::util::case::Case;
1414
use rustc_ast::{
1515
self as ast, BareFnTy, BoundAsyncness, BoundConstness, BoundPolarity, FnRetTy, GenericBound,
@@ -190,7 +190,8 @@ impl<'a> Parser<'a> {
190190
)
191191
}
192192

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

255264
let lo = self.token.span;
256265
let mut impl_dyn_multi = false;

0 commit comments

Comments
 (0)