Skip to content

Commit 5552feb

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 #132629 are now visible in error message for several tests.
1 parent 4c098db commit 5552feb

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
@@ -880,7 +880,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
880880
}),
881881
token::NtPat(pat) => vis.visit_pat(pat),
882882
token::NtExpr(expr) => vis.visit_expr(expr),
883-
token::NtTy(ty) => vis.visit_ty(ty),
884883
token::NtLiteral(expr) => vis.visit_expr(expr),
885884
token::NtMeta(item) => {
886885
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
@@ -468,7 +468,6 @@ impl TokenStream {
468468
}
469469
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
470470
Nonterminal::NtPat(pat) => TokenStream::from_ast(pat),
471-
Nonterminal::NtTy(ty) => TokenStream::from_ast(ty),
472471
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
473472
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
474473
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
@@ -318,6 +318,9 @@ pub(super) fn transcribe<'a>(
318318
let kind = token::NtLifetime(*ident, *is_raw);
319319
TokenTree::token_alone(kind, sp)
320320
}
321+
MatchedSingle(ParseNtResult::Ty(ty)) => {
322+
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
323+
}
321324
MatchedSingle(ParseNtResult::Vis(vis)) => {
322325
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
323326
}

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
};
@@ -611,6 +615,24 @@ impl<'a> Parser<'a> {
611615
self.token == *tok
612616
}
613617

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

17451768
/// 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
@@ -50,7 +50,6 @@ impl<'a> Parser<'a> {
5050
NtStmt(_)
5151
| NtPat(_)
5252
| NtExpr(_)
53-
| NtTy(_)
5453
| NtLiteral(_) // `true`, `false`
5554
| NtMeta(_)
5655
| NtPath(_) => true,
@@ -99,7 +98,7 @@ impl<'a> Parser<'a> {
9998
token::NtLifetime(..) => true,
10099
token::Interpolated(nt) => match &**nt {
101100
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
102-
NtItem(_) | NtPat(_) | NtTy(_) | NtMeta(_) | NtPath(_) => false,
101+
NtItem(_) | NtPat(_) | NtMeta(_) | NtPath(_) => false,
103102
},
104103
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
105104
MetaVarKind::Block
@@ -186,7 +185,9 @@ impl<'a> Parser<'a> {
186185
NtLiteral(self.collect_tokens_no_attrs(|this| this.parse_literal_maybe_minus())?)
187186
}
188187
NonterminalKind::Ty => {
189-
NtTy(self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?)
188+
return Ok(ParseNtResult::Ty(
189+
self.collect_tokens_no_attrs(|this| this.parse_ty_no_question_mark_recover())?,
190+
));
190191
}
191192
// this could be handled like a token, since it is one
192193
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,
@@ -19,7 +19,7 @@ use crate::errors::{
1919
HelpUseLatestEdition, InvalidDynKeyword, LifetimeAfterMut, NeedPlusAfterTraitObjectLifetime,
2020
NestedCVariadicType, ReturnTypesUseThinArrow,
2121
};
22-
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
22+
use crate::maybe_recover_from_interpolated_ty_qpath;
2323

2424
/// Signals whether parsing a type should allow `+`.
2525
///
@@ -184,7 +184,8 @@ impl<'a> Parser<'a> {
184184
)
185185
}
186186

187-
/// Parse a type without recovering `:` as `->` to avoid breaking code such as `where fn() : for<'a>`
187+
/// Parse a type without recovering `:` as `->` to avoid breaking code such
188+
/// as `where fn() : for<'a>`.
188189
pub(super) fn parse_ty_for_where_clause(&mut self) -> PResult<'a, P<Ty>> {
189190
self.parse_ty_common(
190191
AllowPlus::Yes,
@@ -248,7 +249,12 @@ impl<'a> Parser<'a> {
248249
) -> PResult<'a, P<Ty>> {
249250
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
250251
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
251-
maybe_whole!(self, NtTy, |ty| ty);
252+
253+
if let Some(ty) =
254+
self.eat_metavar_seq(MetaVarKind::Ty, |this| this.parse_ty_no_question_mark_recover())
255+
{
256+
return Ok(ty);
257+
}
252258

253259
let lo = self.token.span;
254260
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)