Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2868023

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. - The changes to metavariable descriptions in this PR's earlier commits are now visible in error message for several tests.
1 parent fcb7d6f commit 2868023

19 files changed

+88
-42
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl HasTokens for Nonterminal {
203203
Nonterminal::NtStmt(stmt) => stmt.tokens(),
204204
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
205205
Nonterminal::NtPat(pat) => pat.tokens(),
206-
Nonterminal::NtTy(ty) => ty.tokens(),
207206
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
208207
Nonterminal::NtPath(path) => path.tokens(),
209208
Nonterminal::NtBlock(block) => block.tokens(),
@@ -215,7 +214,6 @@ impl HasTokens for Nonterminal {
215214
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
216215
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
217216
Nonterminal::NtPat(pat) => pat.tokens_mut(),
218-
Nonterminal::NtTy(ty) => ty.tokens_mut(),
219217
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
220218
Nonterminal::NtPath(path) => path.tokens_mut(),
221219
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
@@ -815,7 +815,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
815815
}),
816816
token::NtPat(pat) => vis.visit_pat(pat),
817817
token::NtExpr(expr) => vis.visit_expr(expr),
818-
token::NtTy(ty) => vis.visit_ty(ty),
819818
token::NtLiteral(expr) => vis.visit_expr(expr),
820819
token::NtMeta(item) => {
821820
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,6 @@ impl Token {
660660
| NtMeta(..)
661661
| NtPat(..)
662662
| NtPath(..)
663-
| NtTy(..)
664663
),
665664
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
666665
MetaVarKind::Expr { .. } |
@@ -689,7 +688,7 @@ impl Token {
689688
Lifetime(..) | // lifetime bound in trait object
690689
Lt | BinOp(Shl) | // associated path
691690
PathSep => true, // global path
692-
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
691+
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
693692
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
694693
MetaVarKind::Ty |
695694
MetaVarKind::Path
@@ -1070,7 +1069,6 @@ pub enum Nonterminal {
10701069
NtStmt(P<ast::Stmt>),
10711070
NtPat(P<ast::Pat>),
10721071
NtExpr(P<ast::Expr>),
1073-
NtTy(P<ast::Ty>),
10741072
NtLiteral(P<ast::Expr>),
10751073
/// Stuff inside brackets for attributes
10761074
NtMeta(P<ast::AttrItem>),
@@ -1168,7 +1166,6 @@ impl Nonterminal {
11681166
NtStmt(stmt) => stmt.span,
11691167
NtPat(pat) => pat.span,
11701168
NtExpr(expr) | NtLiteral(expr) => expr.span,
1171-
NtTy(ty) => ty.span,
11721169
NtMeta(attr_item) => attr_item.span(),
11731170
NtPath(path) => path.span,
11741171
}
@@ -1182,7 +1179,6 @@ impl Nonterminal {
11821179
NtPat(..) => "pattern",
11831180
NtExpr(..) => "expression",
11841181
NtLiteral(..) => "literal",
1185-
NtTy(..) => "type",
11861182
NtMeta(..) => "attribute",
11871183
NtPath(..) => "path",
11881184
}
@@ -1207,7 +1203,6 @@ impl fmt::Debug for Nonterminal {
12071203
NtStmt(..) => f.pad("NtStmt(..)"),
12081204
NtPat(..) => f.pad("NtPat(..)"),
12091205
NtExpr(..) => f.pad("NtExpr(..)"),
1210-
NtTy(..) => f.pad("NtTy(..)"),
12111206
NtLiteral(..) => f.pad("NtLiteral(..)"),
12121207
NtMeta(..) => f.pad("NtMeta(..)"),
12131208
NtPath(..) => f.pad("NtPath(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ impl TokenStream {
469469
}
470470
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
471471
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
472-
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
473472
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
474473
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
475474
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ pub(super) fn transcribe<'a>(
317317
let kind = token::NtLifetime(*ident, *is_raw);
318318
TokenTree::token_alone(kind, sp)
319319
}
320+
MatchedSingle(ParseNtResult::Ty(ty)) => {
321+
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
322+
}
320323
MatchedSingle(ParseNtResult::Vis(vis)) => {
321324
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
322325
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,16 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
116116
($self: expr, $allow_qpath_recovery: expr) => {
117117
if $allow_qpath_recovery
118118
&& $self.may_recover()
119-
&& $self.look_ahead(1, |t| t == &token::PathSep)
120-
&& let token::Interpolated(nt) = &$self.token.kind
121-
&& let token::NtTy(ty) = &**nt
119+
&& let Some(token::MetaVarKind::Ty) = $self.token.is_metavar_seq()
120+
&& $self.check_noexpect_past_close_delim(&token::PathSep)
122121
{
123-
let ty = ty.clone();
124-
$self.bump();
122+
// Reparse the type, then move to recovery.
123+
let ty = $self
124+
.eat_metavar_seq(token::MetaVarKind::Ty, |this| {
125+
this.parse_ty_no_question_mark_recover()
126+
})
127+
.expect("metavar seq ty");
128+
125129
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
126130
}
127131
};
@@ -613,6 +617,24 @@ impl<'a> Parser<'a> {
613617
self.token == *tok
614618
}
615619

620+
// Check the first token after the delimiter that closes the current
621+
// delimited sequence. (Panics if used in the outermost token stream, which
622+
// has no delimiters.) It uses a clone of the relevant tree cursor to skip
623+
// past the entire `TokenTree::Delimited` in a single step, avoiding the
624+
// need for unbounded token lookahead.
625+
//
626+
// Primarily used when `self.token` matches
627+
// `OpenDelim(Delimiter::Invisible(_))`, to look ahead through the current
628+
// metavar expansion.
629+
fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
630+
let mut tree_cursor = self.token_cursor.stack.last().unwrap().0.clone();
631+
let tt = tree_cursor.next_ref();
632+
matches!(
633+
tt,
634+
Some(ast::tokenstream::TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
635+
)
636+
}
637+
616638
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
617639
///
618640
/// the main purpose of this function is to reduce the cluttering of the suggestions list
@@ -1741,6 +1763,7 @@ pub enum ParseNtResult {
17411763
Tt(TokenTree),
17421764
Ident(Ident, IdentIsRaw),
17431765
Lifetime(Ident, IdentIsRaw),
1766+
Ty(P<ast::Ty>),
17441767
Vis(P<ast::Visibility>),
17451768

17461769
/// This variant will eventually be removed, along with `Token::Interpolate`.

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ impl<'a> Parser<'a> {
5151
NtStmt(_)
5252
| NtPat(_)
5353
| NtExpr(_)
54-
| NtTy(_)
5554
| NtLiteral(_) // `true`, `false`
5655
| NtMeta(_)
5756
| NtPath(_) => true,
@@ -100,7 +99,7 @@ impl<'a> Parser<'a> {
10099
token::NtLifetime(..) => true,
101100
token::Interpolated(nt) => match &**nt {
102101
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
103-
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) => false,
102+
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
104103
},
105104
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
106105
MetaVarKind::Block
@@ -187,7 +186,9 @@ impl<'a> Parser<'a> {
187186
NtLiteral(self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?)
188187
}
189188
NonterminalKind::Ty => {
190-
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
189+
return Ok(ParseNtResult::Ty(
190+
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
191+
));
191192
}
192193
// this could be handled like a token, since it is one
193194
NonterminalKind::Ident => {

compiler/rustc_parse/src/parser/path.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::mem;
22

33
use ast::token::IdentIsRaw;
44
use rustc_ast::ptr::P;
5-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
5+
use rustc_ast::token::{self, Delimiter, MetaVarKind, Token, TokenKind};
66
use rustc_ast::{
77
self as ast, AngleBracketedArg, AngleBracketedArgs, AnonConst, AssocItemConstraint,
88
AssocItemConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs,
@@ -198,13 +198,14 @@ impl<'a> Parser<'a> {
198198

199199
maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));
200200

201-
if let token::Interpolated(nt) = &self.token.kind {
202-
if let token::NtTy(ty) = &**nt {
203-
if let ast::TyKind::Path(None, path) = &ty.kind {
204-
let path = path.clone();
205-
self.bump();
206-
return Ok(reject_generics_if_mod_style(self, path));
207-
}
201+
if let Some(MetaVarKind::Ty) = self.token.is_metavar_seq() {
202+
let mut snapshot = self.create_snapshot_for_diagnostic();
203+
let ty = snapshot
204+
.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
205+
.expect("metavar seq ty");
206+
if let ast::TyKind::Path(None, path) = ty.into_inner().kind {
207+
self.restore_snapshot(snapshot);
208+
return Ok(reject_generics_if_mod_style(self, path));
208209
}
209210
}
210211

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::ptr::P;
2-
use rustc_ast::token::{self, BinOpToken, Delimiter, IdentIsRaw, Token, TokenKind};
2+
use rustc_ast::token::{self, BinOpToken, Delimiter, IdentIsRaw, MetaVarKind, Token, TokenKind};
33
use rustc_ast::util::case::Case;
44
use rustc_ast::{
55
self as ast, BareFnTy, BoundAsyncness, BoundConstness, BoundPolarity, DUMMY_NODE_ID, FnRetTy,
@@ -18,7 +18,7 @@ use crate::errors::{
1818
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
1919
NestedCVariadicType, ReturnTypesUseThinArrow,
2020
};
21-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
21+
use crate::maybe_recover_from_interpolated_ty_qpath;
2222

2323
/// Signals whether parsing a type should allow `+`.
2424
///
@@ -194,7 +194,8 @@ impl<'a> Parser<'a> {
194194
)
195195
}
196196

197-
/// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
197+
/// Parse a type without recovering `:` as `->` to avoid breaking code such
198+
/// as `where fn() : for<'a>`.
198199
pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
199200
self.parse_ty_common(
200201
AllowPlus::Yes,
@@ -258,7 +259,12 @@ impl<'a> Parser<'a> {
258259
) -> PResult<'a, P<Ty>> {
259260
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
260261
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
261-
maybe_whole!(self, NtTy, |ty| ty);
262+
263+
if let Some(ty) =
264+
self.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
265+
{
266+
return Ok(ty);
267+
}
262268

263269
let lo = self.token.span;
264270
let mut impl_dyn_multi = false;

tests/ui/associated-consts/issue-93835.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ fn e() {
55
//~^ ERROR cannot find type `a` in this scope
66
//~| ERROR cannot find value
77
//~| ERROR associated const equality
8+
//~| ERROR associated const equality
89
//~| ERROR cannot find trait `p` in this scope
910
}
1011

0 commit comments

Comments
 (0)