Skip to content

Commit 7ea59e0

Browse files
committed
Remove NtMeta.
Note: there was an existing code path involving `Interpolated` in `MetaItem::from_tokens` that was dead. This commit transfers that to the new form, but puts an `unreachable!` call inside it.
1 parent ef1114a commit 7ea59e0

20 files changed

+67
-64
lines changed

Diff for: compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ impl HasTokens for Nonterminal {
202202
Nonterminal::NtItem(item) => item.tokens(),
203203
Nonterminal::NtStmt(stmt) => stmt.tokens(),
204204
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
205-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
206205
Nonterminal::NtPath(path) => path.tokens(),
207206
Nonterminal::NtBlock(block) => block.tokens(),
208207
}
@@ -212,7 +211,6 @@ impl HasTokens for Nonterminal {
212211
Nonterminal::NtItem(item) => item.tokens_mut(),
213212
Nonterminal::NtStmt(stmt) => stmt.tokens_mut(),
214213
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
215-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
216214
Nonterminal::NtPath(path) => path.tokens_mut(),
217215
Nonterminal::NtBlock(block) => block.tokens_mut(),
218216
}

Diff for: compiler/rustc_ast/src/attr/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::ast::{
1414
PathSegment, Safety,
1515
};
1616
use crate::ptr::P;
17-
use crate::token::{self, CommentKind, Delimiter, Token};
17+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};
1818
use crate::tokenstream::{
1919
DelimSpan, LazyAttrTokenStream, Spacing, TokenStream, TokenStreamIter, TokenTree,
2020
};
@@ -406,10 +406,18 @@ impl MetaItem {
406406
Path { span, segments, tokens: None }
407407
}
408408
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
409-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
410409
token::Nonterminal::NtPath(path) => (**path).clone(),
411410
_ => return None,
412411
},
412+
Some(TokenTree::Delimited(
413+
_span,
414+
_spacing,
415+
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta { .. })),
416+
_stream,
417+
)) => {
418+
// This path is currently unreachable in the test suite.
419+
unreachable!()
420+
}
413421
Some(TokenTree::Token(
414422
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
415423
_,

Diff for: compiler/rustc_ast/src/mut_visit.rs

-6
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
907907
}),
908908
token::NtExpr(expr) => vis.visit_expr(expr),
909909
token::NtLiteral(expr) => vis.visit_expr(expr),
910-
token::NtMeta(item) => {
911-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
912-
vis.visit_path(path);
913-
visit_attr_args(vis, args);
914-
visit_lazy_tts(vis, tokens);
915-
}
916910
token::NtPath(path) => vis.visit_path(path),
917911
}
918912
}

Diff for: compiler/rustc_ast/src/token.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ pub enum MetaVarKind {
9090
Ident,
9191
Lifetime,
9292
Literal,
93-
Meta,
93+
Meta {
94+
/// Will `AttrItem::meta` succeed on this, if reparsed?
95+
has_meta_form: bool,
96+
},
9497
Path,
9598
Vis,
9699
TT,
@@ -110,7 +113,7 @@ impl fmt::Display for MetaVarKind {
110113
MetaVarKind::Ident => sym::ident,
111114
MetaVarKind::Lifetime => sym::lifetime,
112115
MetaVarKind::Literal => sym::literal,
113-
MetaVarKind::Meta => sym::meta,
116+
MetaVarKind::Meta { .. } => sym::meta,
114117
MetaVarKind::Path => sym::path,
115118
MetaVarKind::Vis => sym::vis,
116119
MetaVarKind::TT => sym::tt,
@@ -658,13 +661,12 @@ impl Token {
658661
matches!(&**nt,
659662
| NtExpr(..)
660663
| NtLiteral(..)
661-
| NtMeta(..)
662664
| NtPath(..)
663665
),
664666
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
665667
MetaVarKind::Expr { .. } |
666668
MetaVarKind::Literal |
667-
MetaVarKind::Meta |
669+
MetaVarKind::Meta { .. } |
668670
MetaVarKind::Pat(_) |
669671
MetaVarKind::Path |
670672
MetaVarKind::Ty { .. }
@@ -1076,8 +1078,6 @@ pub enum Nonterminal {
10761078
NtStmt(P<ast::Stmt>),
10771079
NtExpr(P<ast::Expr>),
10781080
NtLiteral(P<ast::Expr>),
1079-
/// Stuff inside brackets for attributes
1080-
NtMeta(P<ast::AttrItem>),
10811081
NtPath(P<ast::Path>),
10821082
}
10831083

@@ -1171,7 +1171,6 @@ impl Nonterminal {
11711171
NtBlock(block) => block.span,
11721172
NtStmt(stmt) => stmt.span,
11731173
NtExpr(expr) | NtLiteral(expr) => expr.span,
1174-
NtMeta(attr_item) => attr_item.span(),
11751174
NtPath(path) => path.span,
11761175
}
11771176
}
@@ -1183,7 +1182,6 @@ impl Nonterminal {
11831182
NtStmt(..) => "statement",
11841183
NtExpr(..) => "expression",
11851184
NtLiteral(..) => "literal",
1186-
NtMeta(..) => "attribute",
11871185
NtPath(..) => "path",
11881186
}
11891187
}
@@ -1207,7 +1205,6 @@ impl fmt::Debug for Nonterminal {
12071205
NtStmt(..) => f.pad("NtStmt(..)"),
12081206
NtExpr(..) => f.pad("NtExpr(..)"),
12091207
NtLiteral(..) => f.pad("NtLiteral(..)"),
1210-
NtMeta(..) => f.pad("NtMeta(..)"),
12111208
NtPath(..) => f.pad("NtPath(..)"),
12121209
}
12131210
}

Diff for: compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,6 @@ impl TokenStream {
468468
TokenStream::token_alone(token::Semi, stmt.span)
469469
}
470470
Nonterminal::NtStmt(stmt) => TokenStream::from_ast(stmt),
471-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
472471
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
473472
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
474473
}

Diff for: compiler/rustc_attr_parsing/src/parser.rs

-9
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,6 @@ impl<'a> MetaItemListParserContext<'a> {
481481
self.inside_delimiters.peek()
482482
{
483483
match &**nt {
484-
// or maybe a full nt meta including the path but we return immediately
485-
token::Nonterminal::NtMeta(item) => {
486-
self.inside_delimiters.next();
487-
488-
return Some(MetaItemOrLitParser::MetaItemParser(MetaItemParser {
489-
path: PathParser::Ast(&item.path),
490-
args: ArgParser::from_attr_args(&item.args, self.dcx),
491-
}));
492-
}
493484
// an already interpolated path from a macro expansion is a path, no need to parse
494485
// one from tokens
495486
token::Nonterminal::NtPath(path) => {

Diff for: compiler/rustc_expand/src/mbe/transcribe.rs

+8
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,14 @@ pub(super) fn transcribe<'a>(
336336
TokenStream::from_ast(ty),
337337
)
338338
}
339+
MatchedSingle(ParseNtResult::Meta(attr_item)) => {
340+
let has_meta_form = attr_item.meta_kind().is_some();
341+
mk_delimited(
342+
attr_item.span(),
343+
MetaVarKind::Meta { has_meta_form },
344+
TokenStream::from_ast(attr_item),
345+
)
346+
}
339347
MatchedSingle(ParseNtResult::Vis(vis)) => {
340348
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
341349
}

Diff for: compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
424424
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
425425
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
426426
427-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
427+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
428428
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
429429
430430
parse_invalid_offset_of = offset_of expects dot-separated field and variant names

Diff for: compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
10241024
pub(crate) struct InvalidMetaItem {
10251025
#[primary_span]
10261026
pub span: Span,
1027-
pub token: Token,
1027+
pub descr: String,
10281028
#[subdiagnostic]
10291029
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
10301030
}

Diff for: compiler/rustc_parse/src/parser/attr.rs

+22-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use rustc_ast::{self as ast, Attribute, attr, token};
1+
use rustc_ast as ast;
2+
use rustc_ast::token::{self, MetaVarKind};
3+
use rustc_ast::{Attribute, attr};
24
use rustc_errors::codes::*;
35
use rustc_errors::{Diag, PResult};
46
use rustc_span::{BytePos, Span};
@@ -9,7 +11,7 @@ use super::{
911
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
1012
UsePreAttrPos,
1113
};
12-
use crate::{errors, exp, fluent_generated as fluent, maybe_whole};
14+
use crate::{errors, exp, fluent_generated as fluent};
1315

1416
// Public for rustfmt usage
1517
#[derive(Debug)]
@@ -269,7 +271,12 @@ impl<'a> Parser<'a> {
269271
/// PATH `=` UNSUFFIXED_LIT
270272
/// The delimiters or `=` are still put into the resulting token stream.
271273
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
272-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
274+
if let Some(item) = self.eat_metavar_seq_with_matcher(
275+
|mv_kind| matches!(mv_kind, MetaVarKind::Meta { .. }),
276+
|this| this.parse_attr_item(force_collect),
277+
) {
278+
return Ok(item);
279+
}
273280

274281
// Attr items don't have attributes.
275282
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
@@ -396,18 +403,17 @@ impl<'a> Parser<'a> {
396403
&mut self,
397404
unsafe_allowed: AllowLeadingUnsafe,
398405
) -> PResult<'a, ast::MetaItem> {
399-
// We can't use `maybe_whole` here because it would bump in the `None`
400-
// case, which we don't want.
401-
if let token::Interpolated(nt) = &self.token.kind
402-
&& let token::NtMeta(attr_item) = &**nt
403-
{
404-
match attr_item.meta(attr_item.path.span) {
405-
Some(meta) => {
406-
self.bump();
407-
return Ok(meta);
408-
}
409-
None => self.unexpected()?,
410-
}
406+
if let Some(MetaVarKind::Meta { has_meta_form }) = self.token.is_metavar_seq() {
407+
return if has_meta_form {
408+
let attr_item = self
409+
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
410+
this.parse_attr_item(ForceCollect::No)
411+
})
412+
.unwrap();
413+
Ok(attr_item.meta(attr_item.path.span).unwrap())
414+
} else {
415+
self.unexpected_any()
416+
};
411417
}
412418

413419
let lo = self.token.span;
@@ -464,7 +470,7 @@ impl<'a> Parser<'a> {
464470

465471
let mut err = errors::InvalidMetaItem {
466472
span: self.token.span,
467-
token: self.token.clone(),
473+
descr: super::token_descr(&self.token),
468474
quote_ident_sugg: None,
469475
};
470476

Diff for: compiler/rustc_parse/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,7 @@ pub enum ParseNtResult {
17481748
Lifetime(Ident, IdentIsRaw),
17491749
Pat(P<ast::Pat>, NtPatKind),
17501750
Ty(P<ast::Ty>),
1751+
Meta(P<ast::AttrItem>),
17511752
Vis(P<ast::Visibility>),
17521753

17531754
/// This variant will eventually be removed, along with `Token::Interpolate`.

Diff for: compiler/rustc_parse/src/parser/nonterminal.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<'a> Parser<'a> {
3232
| MetaVarKind::Expr { .. }
3333
| MetaVarKind::Ty { .. }
3434
| MetaVarKind::Literal // `true`, `false`
35-
| MetaVarKind::Meta
35+
| MetaVarKind::Meta { .. }
3636
| MetaVarKind::Path => true,
3737

3838
MetaVarKind::Item
@@ -51,7 +51,6 @@ impl<'a> Parser<'a> {
5151
NtStmt(_)
5252
| NtExpr(_)
5353
| NtLiteral(_) // `true`, `false`
54-
| NtMeta(_)
5554
| NtPath(_) => true,
5655

5756
NtItem(_) | NtBlock(_) => false,
@@ -98,7 +97,7 @@ impl<'a> Parser<'a> {
9897
token::NtLifetime(..) => true,
9998
token::Interpolated(nt) => match &**nt {
10099
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
101-
NtItem(_) | NtMeta(_) | NtPath(_) => false,
100+
NtItem(_) | NtPath(_) => false,
102101
},
103102
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
104103
MetaVarKind::Block
@@ -108,7 +107,7 @@ impl<'a> Parser<'a> {
108107
MetaVarKind::Item
109108
| MetaVarKind::Pat(_)
110109
| MetaVarKind::Ty { .. }
111-
| MetaVarKind::Meta
110+
| MetaVarKind::Meta { .. }
112111
| MetaVarKind::Path
113112
| MetaVarKind::Vis => false,
114113
MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
@@ -207,7 +206,9 @@ impl<'a> Parser<'a> {
207206
NonterminalKind::Path => {
208207
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
209208
}
210-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(ForceCollect::Yes)?)),
209+
NonterminalKind::Meta => {
210+
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(ForceCollect::Yes)?)));
211+
}
211212
NonterminalKind::Vis => {
212213
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
213214
this.parse_visibility(FollowedByType::Yes)

Diff for: tests/ui/attributes/nonterminal-expansion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
macro_rules! pass_nonterminal {
66
($n:expr) => {
77
#[repr(align($n))]
8-
//~^ ERROR expected unsuffixed literal, found `n!()`
8+
//~^ ERROR expected unsuffixed literal, found expression `n!()`
99
//~^^ ERROR incorrect `repr(align)` attribute format: `align` expects a literal integer as argument [E0693]
1010
struct S;
1111
};

Diff for: tests/ui/attributes/nonterminal-expansion.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected unsuffixed literal, found `n!()`
1+
error: expected unsuffixed literal, found expression `n!()`
22
--> $DIR/nonterminal-expansion.rs:7:22
33
|
44
LL | #[repr(align($n))]

Diff for: tests/ui/conditional-compilation/cfg-attr-syntax-validation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ struct S9;
2828
macro_rules! generate_s10 {
2929
($expr: expr) => {
3030
#[cfg(feature = $expr)]
31-
//~^ ERROR expected unsuffixed literal, found `concat!("nonexistent")`
32-
//~| ERROR expected unsuffixed literal, found `concat!("nonexistent")`
31+
//~^ ERROR expected unsuffixed literal, found expression `concat!("nonexistent")`
32+
//~| ERROR expected unsuffixed literal, found expression `concat!("nonexistent")`
3333
struct S10;
3434
}
3535
}

Diff for: tests/ui/conditional-compilation/cfg-attr-syntax-validation.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ LL | #[cfg(a = b"hi")]
5454
| |
5555
| help: consider removing the prefix
5656

57-
error: expected unsuffixed literal, found `concat!("nonexistent")`
57+
error: expected unsuffixed literal, found expression `concat!("nonexistent")`
5858
--> $DIR/cfg-attr-syntax-validation.rs:30:25
5959
|
6060
LL | #[cfg(feature = $expr)]
@@ -65,7 +65,7 @@ LL | generate_s10!(concat!("nonexistent"));
6565
|
6666
= note: this error originates in the macro `generate_s10` (in Nightly builds, run with -Z macro-backtrace for more info)
6767

68-
error: expected unsuffixed literal, found `concat!("nonexistent")`
68+
error: expected unsuffixed literal, found expression `concat!("nonexistent")`
6969
--> $DIR/cfg-attr-syntax-validation.rs:30:25
7070
|
7171
LL | #[cfg(feature = $expr)]

Diff for: tests/ui/parser/attribute/attr-bad-meta-4.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
macro_rules! mac {
22
($attr_item: meta) => {
33
#[cfg($attr_item)]
4-
//~^ ERROR expected unsuffixed literal, found `an(arbitrary token stream)`
5-
//~| ERROR expected unsuffixed literal, found `an(arbitrary token stream)`
4+
//~^ ERROR expected unsuffixed literal, found `meta` metavariable
5+
//~| ERROR expected unsuffixed literal, found `meta` metavariable
66
struct S;
77
}
88
}

Diff for: tests/ui/parser/attribute/attr-bad-meta-4.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: expected unsuffixed literal, found `-`
44
LL | #[cfg(feature = -1)]
55
| ^
66

7-
error: expected unsuffixed literal, found `an(arbitrary token stream)`
7+
error: expected unsuffixed literal, found `meta` metavariable
88
--> $DIR/attr-bad-meta-4.rs:3:15
99
|
1010
LL | #[cfg($attr_item)]
@@ -15,7 +15,7 @@ LL | mac!(an(arbitrary token stream));
1515
|
1616
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

18-
error: expected unsuffixed literal, found `an(arbitrary token stream)`
18+
error: expected unsuffixed literal, found `meta` metavariable
1919
--> $DIR/attr-bad-meta-4.rs:3:15
2020
|
2121
LL | #[cfg($attr_item)]

Diff for: tests/ui/parser/attribute/attr-unquoted-ident.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn main() {
1919

2020
macro_rules! make {
2121
($name:ident) => { #[doc(alias = $name)] pub struct S; }
22-
//~^ ERROR expected unsuffixed literal, found `nickname`
22+
//~^ ERROR expected unsuffixed literal, found identifier `nickname`
2323
}
2424

2525
make!(nickname); //~ NOTE in this expansion

Diff for: tests/ui/parser/attribute/attr-unquoted-ident.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ help: surround the identifier with quotation marks to make it into a string lite
2020
LL | #[cfg(key="foo bar baz")]
2121
| + +
2222

23-
error: expected unsuffixed literal, found `nickname`
23+
error: expected unsuffixed literal, found identifier `nickname`
2424
--> $DIR/attr-unquoted-ident.rs:21:38
2525
|
2626
LL | ($name:ident) => { #[doc(alias = $name)] pub struct S; }

0 commit comments

Comments
 (0)