Skip to content

Commit 20f940c

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 41eb0dc commit 20f940c

19 files changed

+88
-42
lines changed

compiler/rustc_ast/src/ast_traits.rs

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

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

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

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

+3
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ pub(super) fn transcribe<'a>(
303303
let kind = token::NtLifetime(*ident, *is_raw);
304304
TokenTree::token_alone(kind, sp)
305305
}
306+
MatchedSingle(ParseNtResult::Ty(ty)) => {
307+
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
308+
}
306309
MatchedSingle(ParseNtResult::Vis(vis)) => {
307310
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
308311
}

compiler/rustc_parse/src/parser/mod.rs

+28-5
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

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

+9-8
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

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

+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/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
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error: expected identifier, found `String`
1+
error: expected identifier, found metavariable
22
--> $DIR/issue-37113.rs:4:16
33
|
44
LL | enum SomeEnum {
55
| -------- while parsing this enum
66
LL | $( $t, )*
7-
| ^^ expected identifier
7+
| ^^ expected identifier, found metavariable
88
...
99
LL | test_macro!(String,);
1010
| -------------------- in this macro invocation

tests/ui/parser/macro/trait-object-macro-matcher.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ macro_rules! m {
1010
fn main() {
1111
m!('static);
1212
//~^ ERROR lifetime in trait object type must be followed by `+`
13+
//~| ERROR lifetime in trait object type must be followed by `+`
1314
//~| ERROR at least one trait is required for an object type
1415
}

tests/ui/parser/macro/trait-object-macro-matcher.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,20 @@ error: lifetime in trait object type must be followed by `+`
44
LL | m!('static);
55
| ^^^^^^^
66

7+
error: lifetime in trait object type must be followed by `+`
8+
--> $DIR/trait-object-macro-matcher.rs:11:8
9+
|
10+
LL | m!('static);
11+
| ^^^^^^^
12+
|
13+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
14+
715
error[E0224]: at least one trait is required for an object type
816
--> $DIR/trait-object-macro-matcher.rs:11:8
917
|
1018
LL | m!('static);
1119
| ^^^^^^^
1220

13-
error: aborting due to 2 previous errors
21+
error: aborting due to 3 previous errors
1422

1523
For more information about this error, try `rustc --explain E0224`.

0 commit comments

Comments
 (0)