Skip to content

Commit 23bb0bf

Browse files
nnethercotecompiler-errors
authored andcommitted
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 08af67f commit 23bb0bf

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
@@ -654,7 +654,6 @@ impl Token {
654654
| NtMeta(..)
655655
| NtPat(..)
656656
| NtPath(..)
657-
| NtTy(..)
658657
),
659658
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
660659
MetaVarKind::Expr { .. } |
@@ -683,7 +682,7 @@ impl Token {
683682
Lifetime(..) | // lifetime bound in trait object
684683
Lt | BinOp(Shl) | // associated path
685684
PathSep => true, // global path
686-
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
685+
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
687686
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
688687
MetaVarKind::Ty |
689688
MetaVarKind::Path
@@ -1064,7 +1063,6 @@ pub enum Nonterminal {
10641063
NtStmt(P<ast::Stmt>),
10651064
NtPat(P<ast::Pat>),
10661065
NtExpr(P<ast::Expr>),
1067-
NtTy(P<ast::Ty>),
10681066
NtLiteral(P<ast::Expr>),
10691067
/// Stuff inside brackets for attributes
10701068
NtMeta(P<ast::AttrItem>),
@@ -1162,7 +1160,6 @@ impl Nonterminal {
11621160
NtStmt(stmt) => stmt.span,
11631161
NtPat(pat) => pat.span,
11641162
NtExpr(expr) | NtLiteral(expr) => expr.span,
1165-
NtTy(ty) => ty.span,
11661163
NtMeta(attr_item) => attr_item.span(),
11671164
NtPath(path) => path.span,
11681165
}
@@ -1176,7 +1173,6 @@ impl Nonterminal {
11761173
NtPat(..) => "pattern",
11771174
NtExpr(..) => "expression",
11781175
NtLiteral(..) => "literal",
1179-
NtTy(..) => "type",
11801176
NtMeta(..) => "attribute",
11811177
NtPath(..) => "path",
11821178
}
@@ -1201,7 +1197,6 @@ impl fmt::Debug for Nonterminal {
12011197
NtStmt(..) => f.pad("NtStmt(..)"),
12021198
NtPat(..) => f.pad("NtPat(..)"),
12031199
NtExpr(..) => f.pad("NtExpr(..)"),
1204-
NtTy(..) => f.pad("NtTy(..)"),
12051200
NtLiteral(..) => f.pad("NtLiteral(..)"),
12061201
NtMeta(..) => f.pad("NtMeta(..)"),
12071202
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
@@ -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

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
};
@@ -609,6 +613,24 @@ impl<'a> Parser<'a> {
609613
self.token == *tok
610614
}
611615

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

17301753
/// 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,
@@ -197,13 +197,14 @@ impl<'a> Parser<'a> {
197197

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

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

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, FnRetTy, GenericBound,
@@ -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

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

Lines changed: 12 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 1 deletion
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)