Skip to content

Commit 353ccf5

Browse files
nnethercotecompiler-errors
authored andcommitted
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 1317750 commit 353ccf5

20 files changed

+50
-48
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,15 +200,13 @@ impl HasTokens for Nonterminal {
200200
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
201201
match self {
202202
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
203-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
204203
Nonterminal::NtPath(path) => path.tokens(),
205204
Nonterminal::NtBlock(block) => block.tokens(),
206205
}
207206
}
208207
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
209208
match self {
210209
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
211-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
212210
Nonterminal::NtPath(path) => path.tokens_mut(),
213211
Nonterminal::NtBlock(block) => block.tokens_mut(),
214212
}

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::ast::{
1515
PathSegment, Safety, DUMMY_NODE_ID,
1616
};
1717
use crate::ptr::P;
18-
use crate::token::{self, CommentKind, Delimiter, Token};
18+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};
1919
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, Spacing, TokenStream, TokenTree};
2020
use crate::util::comments;
2121
use crate::util::literal::escape_string_symbol;
@@ -365,10 +365,18 @@ impl MetaItem {
365365
Path { span, segments, tokens: None }
366366
}
367367
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
368-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
369368
token::Nonterminal::NtPath(path) => (**path).clone(),
370369
_ => return None,
371370
},
371+
Some(TokenTree::Delimited(
372+
_span,
373+
_spacing,
374+
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta)),
375+
_stream,
376+
)) => {
377+
// This path is currently unreachable in the test suite.
378+
unreachable!()
379+
}
372380
Some(TokenTree::Token(
373381
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
374382
_,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,12 +803,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
803803
token::NtBlock(block) => vis.visit_block(block),
804804
token::NtExpr(expr) => vis.visit_expr(expr),
805805
token::NtLiteral(expr) => vis.visit_expr(expr),
806-
token::NtMeta(item) => {
807-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
808-
vis.visit_path(path);
809-
visit_attr_args(vis, args);
810-
visit_lazy_tts(vis, tokens);
811-
}
812806
token::NtPath(path) => vis.visit_path(path),
813807
}
814808
}

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ impl Token {
651651
matches!(&**nt,
652652
| NtExpr(..)
653653
| NtLiteral(..)
654-
| NtMeta(..)
655654
| NtPath(..)
656655
),
657656
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -854,6 +853,7 @@ impl Token {
854853
/// Is this a pre-parsed expression dropped into the token stream
855854
/// (which happens while parsing the result of macro expansion)?
856855
pub fn is_whole_expr(&self) -> bool {
856+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
857857
if let Interpolated(nt) = &self.kind
858858
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
859859
{
@@ -1060,8 +1060,6 @@ pub enum Nonterminal {
10601060
NtBlock(P<ast::Block>),
10611061
NtExpr(P<ast::Expr>),
10621062
NtLiteral(P<ast::Expr>),
1063-
/// Stuff inside brackets for attributes
1064-
NtMeta(P<ast::AttrItem>),
10651063
NtPath(P<ast::Path>),
10661064
}
10671065

@@ -1153,7 +1151,6 @@ impl Nonterminal {
11531151
match self {
11541152
NtBlock(block) => block.span,
11551153
NtExpr(expr) | NtLiteral(expr) => expr.span,
1156-
NtMeta(attr_item) => attr_item.span(),
11571154
NtPath(path) => path.span,
11581155
}
11591156
}
@@ -1163,7 +1160,6 @@ impl Nonterminal {
11631160
NtBlock(..) => "block",
11641161
NtExpr(..) => "expression",
11651162
NtLiteral(..) => "literal",
1166-
NtMeta(..) => "attribute",
11671163
NtPath(..) => "path",
11681164
}
11691165
}
@@ -1185,7 +1181,6 @@ impl fmt::Debug for Nonterminal {
11851181
NtBlock(..) => f.pad("NtBlock(..)"),
11861182
NtExpr(..) => f.pad("NtExpr(..)"),
11871183
NtLiteral(..) => f.pad("NtLiteral(..)"),
1188-
NtMeta(..) => f.pad("NtMeta(..)"),
11891184
NtPath(..) => f.pad("NtPath(..)"),
11901185
}
11911186
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,6 @@ impl TokenStream {
462462
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
463463
match nt {
464464
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
465-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
466465
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
467466
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
468467
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ pub(super) fn transcribe<'a>(
321321
MatchedSingle(ParseNtResult::Ty(ty)) => {
322322
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
323323
}
324+
MatchedSingle(ParseNtResult::Meta(meta)) => {
325+
mk_delimited(MetaVarKind::Meta, TokenStream::from_ast(meta))
326+
}
324327
MatchedSingle(ParseNtResult::Vis(vis)) => {
325328
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
326329
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
415415
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
416416
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
417417
418-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
418+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
419419
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
420420
421421
parse_invalid_offset_of = offset_of expects dot-separated field and variant names

compiler/rustc_parse/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
10221022
pub(crate) struct InvalidMetaItem {
10231023
#[primary_span]
10241024
pub span: Span,
1025-
pub token: Token,
1025+
pub descr: String,
10261026
#[subdiagnostic]
10271027
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
10281028
}

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast as ast;
22
use rustc_ast::attr;
3-
use rustc_ast::token::{self, Delimiter};
3+
use rustc_ast::token::{self, Delimiter, MetaVarKind};
44
use rustc_errors::codes::*;
55
use rustc_errors::{Diag, PResult};
66
use rustc_span::symbol::kw;
@@ -12,7 +12,7 @@ use super::{
1212
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
1313
UsePreAttrPos,
1414
};
15-
use crate::{errors, fluent_generated as fluent, maybe_whole};
15+
use crate::{errors, fluent_generated as fluent};
1616

1717
// Public for rustfmt usage
1818
#[derive(Debug)]
@@ -272,7 +272,11 @@ impl<'a> Parser<'a> {
272272
/// PATH `=` UNSUFFIXED_LIT
273273
/// The delimiters or `=` are still put into the resulting token stream.
274274
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
275-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
275+
if let Some(item) =
276+
self.eat_metavar_seq(MetaVarKind::Meta, |this| this.parse_attr_item(force_collect))
277+
{
278+
return Ok(item);
279+
}
276280

277281
// Attr items don't have attributes.
278282
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
@@ -397,18 +401,18 @@ impl<'a> Parser<'a> {
397401
&mut self,
398402
unsafe_allowed: AllowLeadingUnsafe,
399403
) -> PResult<'a, ast::MetaItem> {
400-
// We can't use `maybe_whole` here because it would bump in the `None`
401-
// case, which we don't want.
402-
if let token::Interpolated(nt) = &self.token.kind
403-
&& let token::NtMeta(attr_item) = &**nt
404+
// Snapshot the parser so we can backtrack in the case where `attr_item.meta()` fails.
405+
let mut snapshot = self.create_snapshot_for_diagnostic();
406+
if let Some(attr_item) = snapshot
407+
.eat_metavar_seq(MetaVarKind::Meta, |this| this.parse_attr_item(ForceCollect::No))
404408
{
405-
match attr_item.meta(attr_item.path.span) {
409+
return match attr_item.meta(attr_item.path.span) {
406410
Some(meta) => {
407-
self.bump();
408-
return Ok(meta);
411+
self.restore_snapshot(snapshot);
412+
Ok(meta)
409413
}
410-
None => self.unexpected()?,
411-
}
414+
None => self.unexpected_any(),
415+
};
412416
}
413417

414418
let lo = self.token.span;
@@ -465,7 +469,7 @@ impl<'a> Parser<'a> {
465469

466470
let mut err = errors::InvalidMetaItem {
467471
span: self.token.span,
468-
token: self.token.clone(),
472+
descr: super::token_descr(&self.token),
469473
quote_ident_sugg: None,
470474
};
471475

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,6 @@ impl<'a> Parser<'a> {
14021402
self.bump();
14031403
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
14041404
}
1405-
_ => {}
14061405
};
14071406
}
14081407

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,6 +1754,7 @@ pub enum ParseNtResult {
17541754
Stmt(P<ast::Stmt>),
17551755
Pat(P<ast::Pat>, NtPatKind),
17561756
Ty(P<ast::Ty>),
1757+
Meta(P<ast::AttrItem>),
17571758
Vis(P<ast::Visibility>),
17581759

17591760
/// 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
@@ -50,7 +50,6 @@ impl<'a> Parser<'a> {
5050
match nt {
5151
NtExpr(_)
5252
| NtLiteral(_) // `true`, `false`
53-
| NtMeta(_)
5453
| NtPath(_) => true,
5554

5655
NtBlock(_) => false,
@@ -97,7 +96,7 @@ impl<'a> Parser<'a> {
9796
token::NtLifetime(..) => true,
9897
token::Interpolated(nt) => match &**nt {
9998
NtBlock(_) | NtExpr(_) | NtLiteral(_) => true,
100-
NtMeta(_) | NtPath(_) => false,
99+
NtPath(_) => false,
101100
},
102101
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
103102
MetaVarKind::Block
@@ -206,7 +205,9 @@ impl<'a> Parser<'a> {
206205
NonterminalKind::Path => {
207206
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
208207
}
209-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(ForceCollect::Yes)?)),
208+
NonterminalKind::Meta => {
209+
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(ForceCollect::Yes)?)));
210+
}
210211
NonterminalKind::Vis => {
211212
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
212213
this.parse_visibility(FollowedByType::Yes)

tests/ui/attributes/nonterminal-expansion.rs

Lines changed: 1 addition & 1 deletion
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
struct S;
1010
};
1111
}

tests/ui/attributes/nonterminal-expansion.stderr

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

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

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

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

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

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

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

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

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

tests/ui/parser/attribute/attr-unquoted-ident.rs

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

tests/ui/parser/attribute/attr-unquoted-ident.stderr

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