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

Commit aa6bd27

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 296b392 commit aa6bd27

19 files changed

+92
-43
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(),
@@ -247,7 +246,6 @@ impl HasTokens for Nonterminal {
247246
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
248247
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
249248
Nonterminal::NtPat(pat) => pat.tokens_mut(),
250-
Nonterminal::NtTy(ty) => ty.tokens_mut(),
251249
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
252250
Nonterminal::NtPath(path) => path.tokens_mut(),
253251
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
@@ -834,7 +834,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
834834
}),
835835
token::NtPat(pat) => vis.visit_pat(pat),
836836
token::NtExpr(expr) => vis.visit_expr(expr),
837-
token::NtTy(ty) => vis.visit_ty(ty),
838837
token::NtLiteral(expr) => vis.visit_expr(expr),
839838
token::NtMeta(item) => {
840839
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ impl Token {
592592
Lifetime(..) | // lifetime bound in trait object
593593
Lt | BinOp(Shl) | // associated path
594594
PathSep => true, // global path
595-
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
595+
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
596596
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
597597
NonterminalKind::Ty |
598598
NonterminalKind::Path
@@ -964,7 +964,6 @@ pub enum Nonterminal {
964964
NtStmt(P<ast::Stmt>),
965965
NtPat(P<ast::Pat>),
966966
NtExpr(P<ast::Expr>),
967-
NtTy(P<ast::Ty>),
968967
NtLiteral(P<ast::Expr>),
969968
/// Stuff inside brackets for attributes
970969
NtMeta(P<ast::AttrItem>),
@@ -1062,7 +1061,6 @@ impl Nonterminal {
10621061
NtStmt(stmt) => stmt.span,
10631062
NtPat(pat) => pat.span,
10641063
NtExpr(expr) | NtLiteral(expr) => expr.span,
1065-
NtTy(ty) => ty.span,
10661064
NtMeta(attr_item) => attr_item.span(),
10671065
NtPath(path) => path.span,
10681066
}
@@ -1076,7 +1074,6 @@ impl Nonterminal {
10761074
NtPat(..) => "pattern",
10771075
NtExpr(..) => "expression",
10781076
NtLiteral(..) => "literal",
1079-
NtTy(..) => "type",
10801077
NtMeta(..) => "attribute",
10811078
NtPath(..) => "path",
10821079
}
@@ -1101,7 +1098,6 @@ impl fmt::Debug for Nonterminal {
11011098
NtStmt(..) => f.pad("NtStmt(..)"),
11021099
NtPat(..) => f.pad("NtPat(..)"),
11031100
NtExpr(..) => f.pad("NtExpr(..)"),
1104-
NtTy(..) => f.pad("NtTy(..)"),
11051101
NtLiteral(..) => f.pad("NtLiteral(..)"),
11061102
NtMeta(..) => f.pad("NtMeta(..)"),
11071103
NtPath(..) => f.pad("NtPath(..)"),

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ impl TokenStream {
474474
}
475475
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
476476
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
477-
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
478477
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
479478
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
480479
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
@@ -296,6 +296,9 @@ pub(super) fn transcribe<'a>(
296296
let kind = token::NtLifetime(*ident);
297297
TokenTree::token_alone(kind, sp)
298298
}
299+
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
300+
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
301+
}
299302
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
300303
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
301304
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,16 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
157157
($self: expr, $allow_qpath_recovery: expr) => {
158158
if $allow_qpath_recovery
159159
&& $self.may_recover()
160-
&& $self.look_ahead(1, |t| t == &token::PathSep)
161-
&& let token::Interpolated(nt) = &$self.token.kind
162-
&& let token::NtTy(ty) = &**nt
160+
&& let Some(token::NonterminalKind::Ty) = $self.token.is_metavar_seq()
161+
&& $self.check_noexpect_past_close_delim(&token::PathSep)
163162
{
164-
let ty = ty.clone();
165-
$self.bump();
163+
// Reparse the type, then move to recovery.
164+
let ty = crate::reparse_metavar_seq!(
165+
$self,
166+
token::NonterminalKind::Ty,
167+
super::ParseNtResult::Ty(ty),
168+
ty
169+
);
166170
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
167171
}
168172
};
@@ -600,6 +604,24 @@ impl<'a> Parser<'a> {
600604
self.token == *tok
601605
}
602606

607+
// Check the first token after the delimiter that closes the current
608+
// delimited sequence. (Panics if used in the outermost token stream, which
609+
// has no delimiters.) It uses a clone of the relevant tree cursor to skip
610+
// past the entire `TokenTree::Delimited` in a single step, avoiding the
611+
// need for unbounded token lookahead.
612+
//
613+
// Primarily used when `self.token` matches
614+
// `OpenDelim(Delimiter::Invisible(_))`, to look ahead through the current
615+
// metavar expansion.
616+
fn check_noexpect_past_close_delim(&self, tok: &TokenKind) -> bool {
617+
let mut tree_cursor = self.token_cursor.stack.last().unwrap().0.clone();
618+
let tt = tree_cursor.next_ref();
619+
matches!(
620+
tt,
621+
Some(ast::tokenstream::TokenTree::Token(token::Token { kind, .. }, _)) if kind == tok
622+
)
623+
}
624+
603625
/// Consumes a token 'tok' if it exists. Returns whether the given token was present.
604626
///
605627
/// the main purpose of this function is to reduce the cluttering of the suggestions list
@@ -1679,6 +1701,7 @@ pub enum ParseNtResult {
16791701
Tt(TokenTree),
16801702
Ident(Ident, IdentIsRaw),
16811703
Lifetime(Ident),
1704+
Ty(P<ast::Ty>),
16821705
Vis(P<ast::Visibility>),
16831706

16841707
/// 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
@@ -49,7 +49,6 @@ impl<'a> Parser<'a> {
4949
NtStmt(_)
5050
| NtPat(_)
5151
| NtExpr(_)
52-
| NtTy(_)
5352
| NtLiteral(_) // `true`, `false`
5453
| NtMeta(_)
5554
| NtPath(_) => true,
@@ -89,7 +88,7 @@ impl<'a> Parser<'a> {
8988
token::NtLifetime(..) => true,
9089
token::Interpolated(nt) => match &**nt {
9190
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
92-
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) => false,
91+
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
9392
},
9493
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9594
NonterminalKind::Block
@@ -197,7 +196,9 @@ impl<'a> Parser<'a> {
197196
NtLiteral(self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?)
198197
}
199198
NonterminalKind::Ty => {
200-
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
199+
return Ok(ParseNtResult::Ty(
200+
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
201+
));
201202
}
202203
// this could be handled like a token, since it is one
203204
NonterminalKind::Ident => {

compiler/rustc_parse/src/parser/path.rs

Lines changed: 9 additions & 9 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, AssocItemConstraint,
1111
AssocItemConstraintKind, BlockCheckMode, GenericArg, GenericArgs, Generics, ParenthesizedArgs,
@@ -195,13 +195,13 @@ impl<'a> Parser<'a> {
195195

196196
maybe_whole!(self, NtPath, |path| reject_generics_if_mod_style(self, path.into_inner()));
197197

198-
if let token::Interpolated(nt) = &self.token.kind {
199-
if let token::NtTy(ty) = &**nt {
200-
if let ast::TyKind::Path(None, path) = &ty.kind {
201-
let path = path.clone();
202-
self.bump();
203-
return Ok(reject_generics_if_mod_style(self, path));
204-
}
198+
if let Some(NonterminalKind::Ty) = self.token.is_metavar_seq() {
199+
let mut self2 = self.clone();
200+
let ty =
201+
crate::reparse_metavar_seq!(self2, NonterminalKind::Ty, ParseNtResult::Ty(ty), ty);
202+
if let ast::TyKind::Path(None, path) = ty.into_inner().kind {
203+
*self = self2;
204+
return Ok(reject_generics_if_mod_style(self, path));
205205
}
206206
}
207207

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, BinOpToken, Delimiter, Token, TokenKind};
12+
use rustc_ast::token::{self, BinOpToken, 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;

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)