Skip to content

Commit e651a19

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 b5d4767 commit e651a19

21 files changed

+97
-58
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
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

-1
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

+1-5
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl Token {
580580
Lifetime(..) | // lifetime bound in trait object
581581
Lt | BinOp(Shl) | // associated path
582582
PathSep => true, // global path
583-
Interpolated(ref nt) => matches!(&nt.0, NtTy(..) | NtPath(..)),
583+
Interpolated(ref nt) => matches!(&nt.0, NtPath(..)),
584584
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
585585
NonterminalKind::Ty |
586586
NonterminalKind::Path
@@ -930,7 +930,6 @@ pub enum Nonterminal {
930930
NtStmt(P<ast::Stmt>),
931931
NtPat(P<ast::Pat>),
932932
NtExpr(P<ast::Expr>),
933-
NtTy(P<ast::Ty>),
934933
NtIdent(Ident, IdentIsRaw),
935934
NtLifetime(Ident),
936935
NtLiteral(P<ast::Expr>),
@@ -1025,7 +1024,6 @@ impl Nonterminal {
10251024
NtStmt(stmt) => stmt.span,
10261025
NtPat(pat) => pat.span,
10271026
NtExpr(expr) | NtLiteral(expr) => expr.span,
1028-
NtTy(ty) => ty.span,
10291027
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
10301028
NtMeta(attr_item) => attr_item.span(),
10311029
NtPath(path) => path.span,
@@ -1040,7 +1038,6 @@ impl Nonterminal {
10401038
NtPat(..) => "pattern",
10411039
NtExpr(..) => "expression",
10421040
NtLiteral(..) => "literal",
1043-
NtTy(..) => "type",
10441041
NtIdent(..) => "identifier",
10451042
NtLifetime(..) => "lifetime",
10461043
NtMeta(..) => "attribute",
@@ -1073,7 +1070,6 @@ impl fmt::Debug for Nonterminal {
10731070
NtStmt(..) => f.pad("NtStmt(..)"),
10741071
NtPat(..) => f.pad("NtPat(..)"),
10751072
NtExpr(..) => f.pad("NtExpr(..)"),
1076-
NtTy(..) => f.pad("NtTy(..)"),
10771073
NtIdent(..) => f.pad("NtIdent(..)"),
10781074
NtLiteral(..) => f.pad("NtLiteral(..)"),
10791075
NtMeta(..) => f.pad("NtMeta(..)"),

compiler/rustc_ast/src/tokenstream.rs

-1
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

-1
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

+3
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

+29-12
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,18 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
145145
($self: expr, $allow_qpath_recovery: expr) => {
146146
if $allow_qpath_recovery
147147
&& $self.may_recover()
148-
&& $self.look_ahead(1, |t| t == &token::PathSep)
149-
&& let token::Interpolated(nt) = &$self.token.kind
150-
&& let token::NtTy(ty) = &nt.0
148+
&& let Some(token::NonterminalKind::Ty) = $self.token.is_metavar_seq()
149+
&& $self.check_noexpect_past_close_delim(&token::PathSep)
151150
{
152-
let ty = ty.clone();
153-
$self.bump();
151+
// Reparse the type, then move to recovery.
152+
let ty = crate::reparse_metavar_seq!(
153+
$self,
154+
token::NonterminalKind::Ty,
155+
super::ParseNtResult::Ty(ty),
156+
ty
157+
);
154158
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
155159
}
156-
if $allow_qpath_recovery
157-
&& $self.may_recover()
158-
&& $self.look_ahead(1, |t| t == &token::PathSep)
159-
&& let token::OpenDelim(Delimiter::Invisible(_)) = &$self.token.kind
160-
{
161-
panic!("njn: invis-delim?");
162-
}
163160
};
164161
}
165162

@@ -619,6 +616,24 @@ impl<'a> Parser<'a> {
619616
self.token == *tok
620617
}
621618

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

16691685
/// This variant will eventually be removed, along with `Token::Interpolate`.
@@ -1677,6 +1693,7 @@ impl<T> ParseNtResult<T> {
16771693
{
16781694
match self {
16791695
ParseNtResult::Tt(tt) => ParseNtResult::Tt(tt),
1696+
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
16801697
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
16811698
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
16821699
}

compiler/rustc_parse/src/parser/nonterminal.rs

+4-3
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

+10-13
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,19 +186,16 @@ 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
}
199-
if let token::OpenDelim(Delimiter::Invisible(_)) = &self.token.kind {
200-
panic!("njn: invis-delim?");
201-
}
202199

203200
let lo = self.token.span;
204201
let mut segments = ThinVec::new();

compiler/rustc_parse/src/parser/ty.rs

+14-5
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;

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

+1
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

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ LL | type_ascribe!(p, a<p:p<e=6>>);
2626
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
2727
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2828

29-
error: aborting due to 4 previous errors
29+
error[E0658]: associated const equality is incomplete
30+
--> $DIR/issue-93835.rs:4:28
31+
|
32+
LL | type_ascribe!(p, a<p:p<e=6>>);
33+
| ^^^
34+
|
35+
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
36+
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
37+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
38+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
39+
40+
error: aborting due to 5 previous errors
3041

3142
Some errors have detailed explanations: E0405, E0412, E0425, E0658.
3243
For more information about an error, try `rustc --explain E0405`.

tests/ui/macros/macro-interpolation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ macro_rules! qpath {
1919

2020
(ty, <$type:ty as $trait:ty>::$name:ident) => {
2121
<$type as $trait>::$name
22-
//~^ ERROR expected identifier, found `!`
22+
//~^ ERROR expected identifier, found metavariable
2323
};
2424
}
2525

tests/ui/macros/macro-interpolation.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: expected identifier, found `!`
1+
error: expected identifier, found metavariable
22
--> $DIR/macro-interpolation.rs:21:19
33
|
44
LL | <$type as $trait>::$name
5-
| ^^^^^^ expected identifier
5+
| ^^^^^^ expected identifier, found metavariable
66
...
77
LL | let _: qpath!(ty, <str as !>::Owned);
88
| -----------------------------

tests/ui/macros/stringify.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,14 @@ fn test_ty() {
748748
c1!(ty, [ &'a T ], "&'a T");
749749
c1!(ty, [ &'a mut [T] ], "&'a mut [T]");
750750
c1!(ty, [ &A<B<C<D<E>>>> ], "&A<B<C<D<E>>>>");
751-
c2!(ty, [ &A<B<C<D<E> > > > ], "&A<B<C<D<E>>>>", "&A<B<C<D<E> > > >");
751+
c1!(ty, [ &A<B<C<D<E> > > > ], "&A<B<C<D<E> > > >");
752752

753753
// TyKind::BareFn
754754
c1!(ty, [ fn() ], "fn()");
755755
c1!(ty, [ fn() -> () ], "fn() -> ()");
756756
c1!(ty, [ fn(u8) ], "fn(u8)");
757757
c1!(ty, [ fn(x: u8) ], "fn(x: u8)");
758-
c2!(ty, [ for<> fn() ], "fn()", "for<> fn()");
758+
c1!(ty, [ for<> fn() ], "for<> fn()");
759759
c1!(ty, [ for<'a> fn() ], "for<'a> fn()");
760760

761761
// TyKind::Never
@@ -774,7 +774,7 @@ fn test_ty() {
774774
c1!(ty, [ T ], "T");
775775
c1!(ty, [ Ref<'a> ], "Ref<'a>");
776776
c1!(ty, [ PhantomData<T> ], "PhantomData<T>");
777-
c2!(ty, [ PhantomData::<T> ], "PhantomData<T>", "PhantomData::<T>");
777+
c1!(ty, [ PhantomData::<T> ], "PhantomData::<T>");
778778
c1!(ty, [ Fn() -> ! ], "Fn() -> !");
779779
c1!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !");
780780
c1!(ty, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type");

tests/ui/macros/syntax-error-recovery.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ macro_rules! values {
99
}
1010
};
1111
}
12-
//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found type `(String)`
13-
//~| ERROR macro expansion ignores type `(String)` and any tokens following
12+
//~^^^^^ ERROR expected one of `(`, `,`, `=`, `{`, or `}`, found `ty` metavariable
13+
//~| ERROR macro expansion ignores `ty` metavariable and any tokens following
1414

1515
values!(STRING(1) as (String) => cfg(test),);
1616
//~^ ERROR expected one of `!` or `::`, found `<eof>`

tests/ui/macros/syntax-error-recovery.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected one of `(`, `,`, `=`, `{`, or `}`, found type `(String)`
1+
error: expected one of `(`, `,`, `=`, `{`, or `}`, found `ty` metavariable
22
--> $DIR/syntax-error-recovery.rs:7:26
33
|
44
LL | $token $($inner)? = $value,
@@ -10,7 +10,7 @@ LL | values!(STRING(1) as (String) => cfg(test),);
1010
= help: enum variants can be `Variant`, `Variant = <integer>`, `Variant(Type, ..., TypeN)` or `Variant { fields: Types }`
1111
= note: this error originates in the macro `values` (in Nightly builds, run with -Z macro-backtrace for more info)
1212

13-
error: macro expansion ignores type `(String)` and any tokens following
13+
error: macro expansion ignores `ty` metavariable and any tokens following
1414
--> $DIR/syntax-error-recovery.rs:7:26
1515
|
1616
LL | $token $($inner)? = $value,

tests/ui/parser/macro/issue-37113.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
macro_rules! test_macro {
22
( $( $t:ty ),* $(),*) => {
33
enum SomeEnum {
4-
$( $t, )* //~ ERROR expected identifier, found `String`
4+
$( $t, )* //~ ERROR expected identifier, found metavariable
55
};
66
};
77
}

0 commit comments

Comments
 (0)