Skip to content

Commit 76b0443

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: similar, plus there are some additional errors about this very broken code. - The changes to metavariable descriptions in rust-lang#132629 are now visible in error message for several tests.
1 parent c7981d6 commit 76b0443

19 files changed

+109
-48
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
@@ -907,7 +907,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
907907
}),
908908
token::NtPat(pat) => vis.visit_pat(pat),
909909
token::NtExpr(expr) => vis.visit_expr(expr),
910-
token::NtTy(ty) => vis.visit_ty(ty),
911910
token::NtLiteral(expr) => vis.visit_expr(expr),
912911
token::NtMeta(item) => {
913912
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
@@ -659,7 +659,6 @@ impl Token {
659659
| NtMeta(..)
660660
| NtPat(..)
661661
| NtPath(..)
662-
| NtTy(..)
663662
),
664663
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
665664
MetaVarKind::Expr { .. } |
@@ -688,7 +687,7 @@ impl Token {
688687
Lifetime(..) | // lifetime bound in trait object
689688
Lt | BinOp(Shl) | // associated path
690689
PathSep => true, // global path
691-
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
690+
Interpolated(ref nt) => matches!(&**nt, NtPath(..)),
692691
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
693692
MetaVarKind::Ty |
694693
MetaVarKind::Path
@@ -1076,7 +1075,6 @@ pub enum Nonterminal {
10761075
NtStmt(P<ast::Stmt>),
10771076
NtPat(P<ast::Pat>),
10781077
NtExpr(P<ast::Expr>),
1079-
NtTy(P<ast::Ty>),
10801078
NtLiteral(P<ast::Expr>),
10811079
/// Stuff inside brackets for attributes
10821080
NtMeta(P<ast::AttrItem>),
@@ -1174,7 +1172,6 @@ impl Nonterminal {
11741172
NtStmt(stmt) => stmt.span,
11751173
NtPat(pat) => pat.span,
11761174
NtExpr(expr) | NtLiteral(expr) => expr.span,
1177-
NtTy(ty) => ty.span,
11781175
NtMeta(attr_item) => attr_item.span(),
11791176
NtPath(path) => path.span,
11801177
}
@@ -1188,7 +1185,6 @@ impl Nonterminal {
11881185
NtPat(..) => "pattern",
11891186
NtExpr(..) => "expression",
11901187
NtLiteral(..) => "literal",
1191-
NtTy(..) => "type",
11921188
NtMeta(..) => "attribute",
11931189
NtPath(..) => "path",
11941190
}
@@ -1213,7 +1209,6 @@ impl fmt::Debug for Nonterminal {
12131209
NtStmt(..) => f.pad("NtStmt(..)"),
12141210
NtPat(..) => f.pad("NtPat(..)"),
12151211
NtExpr(..) => f.pad("NtExpr(..)"),
1216-
NtTy(..) => f.pad("NtTy(..)"),
12171212
NtLiteral(..) => f.pad("NtLiteral(..)"),
12181213
NtMeta(..) => f.pad("NtMeta(..)"),
12191214
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
@@ -322,6 +322,9 @@ pub(super) fn transcribe<'a>(
322322
let kind = token::NtLifetime(*ident, *is_raw);
323323
TokenTree::token_alone(kind, sp)
324324
}
325+
MatchedSingle(ParseNtResult::Ty(ty)) => {
326+
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
327+
}
325328
MatchedSingle(ParseNtResult::Vis(vis)) => {
326329
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
327330
}

compiler/rustc_parse/src/parser/mod.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ macro_rules! maybe_recover_from_interpolated_ty_qpath {
117117
($self: expr, $allow_qpath_recovery: expr) => {
118118
if $allow_qpath_recovery
119119
&& $self.may_recover()
120-
&& $self.look_ahead(1, |t| t == &token::PathSep)
121-
&& let token::Interpolated(nt) = &$self.token.kind
122-
&& let token::NtTy(ty) = &**nt
120+
&& let Some(token::MetaVarKind::Ty) = $self.token.is_metavar_seq()
121+
&& $self.check_noexpect_past_close_delim(&token::PathSep)
123122
{
124-
let ty = ty.clone();
125-
$self.bump();
123+
// Reparse the type, then move to recovery.
124+
let ty = $self
125+
.eat_metavar_seq(token::MetaVarKind::Ty, |this| {
126+
this.parse_ty_no_question_mark_recover()
127+
})
128+
.expect("metavar seq ty");
129+
126130
return $self.maybe_recover_from_bad_qpath_stage_2($self.prev_token.span, ty);
127131
}
128132
};
@@ -614,6 +618,24 @@ impl<'a> Parser<'a> {
614618
self.token == *tok
615619
}
616620

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

17291752
/// 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,
@@ -196,13 +196,14 @@ impl<'a> Parser<'a> {
196196

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

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

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::{exp, maybe_recover_from_interpolated_ty_qpath, maybe_whole};
21+
use crate::{exp, maybe_recover_from_interpolated_ty_qpath};
2222

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

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

252258
let lo = self.token.span;
253259
let mut impl_dyn_multi = false;

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
fn e() {
44
type_ascribe!(p, a<p:p<e=6>>);
55
//~^ ERROR cannot find type `a` in this scope
6+
//~| ERROR path separator must be a double colon
67
//~| ERROR cannot find value
78
//~| ERROR associated const equality
8-
//~| ERROR cannot find trait `p` in this scope
9+
//~| ERROR associated const equality
10+
//~| ERROR failed to resolve: use of unresolved module or unlinked crate `p`
911
}
1012

1113
fn main() {}

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

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
error: path separator must be a double colon
2+
--> $DIR/issue-93835.rs:4:25
3+
|
4+
LL | type_ascribe!(p, a<p:p<e=6>>);
5+
| ^
6+
|
7+
= note: if you meant to annotate an expression with a type, the type ascription syntax has been removed, see issue #101728 <https://github.com/rust-lang/rust/issues/101728>
8+
help: use a double colon instead
9+
|
10+
LL | type_ascribe!(p, a<p::p<e=6>>);
11+
| +
12+
113
error[E0425]: cannot find value `p` in this scope
214
--> $DIR/issue-93835.rs:4:19
315
|
@@ -10,11 +22,15 @@ error[E0412]: cannot find type `a` in this scope
1022
LL | type_ascribe!(p, a<p:p<e=6>>);
1123
| ^ not found in this scope
1224

13-
error[E0405]: cannot find trait `p` in this scope
14-
--> $DIR/issue-93835.rs:4:26
25+
error[E0658]: associated const equality is incomplete
26+
--> $DIR/issue-93835.rs:4:28
1527
|
1628
LL | type_ascribe!(p, a<p:p<e=6>>);
17-
| ^ not found in this scope
29+
| ^^^
30+
|
31+
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
32+
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
33+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1834

1935
error[E0658]: associated const equality is incomplete
2036
--> $DIR/issue-93835.rs:4:28
@@ -25,8 +41,17 @@ LL | type_ascribe!(p, a<p:p<e=6>>);
2541
= note: see issue #92827 <https://github.com/rust-lang/rust/issues/92827> for more information
2642
= help: add `#![feature(associated_const_equality)]` to the crate attributes to enable
2743
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
44+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
45+
46+
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `p`
47+
--> $DIR/issue-93835.rs:4:24
48+
|
49+
LL | type_ascribe!(p, a<p:p<e=6>>);
50+
| ^ use of unresolved module or unlinked crate `p`
51+
|
52+
= help: you might be missing a crate named `p`
2853

29-
error: aborting due to 4 previous errors
54+
error: aborting due to 6 previous errors
3055

31-
Some errors have detailed explanations: E0405, E0412, E0425, E0658.
32-
For more information about an error, try `rustc --explain E0405`.
56+
Some errors have detailed explanations: E0412, E0425, E0433, E0658.
57+
For more information about an error, try `rustc --explain E0412`.

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
}

0 commit comments

Comments
 (0)