Skip to content

Commit 973cedb

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 b53a0e4 commit 973cedb

File tree

18 files changed

+58
-50
lines changed

18 files changed

+58
-50
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ impl HasTokens for Nonterminal {
232232
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233233
match self {
234234
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens(),
235-
Nonterminal::NtMeta(attr_item) => attr_item.tokens(),
236235
Nonterminal::NtPath(path) => path.tokens(),
237236
Nonterminal::NtBlock(block) => block.tokens(),
238237
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
@@ -241,7 +240,6 @@ impl HasTokens for Nonterminal {
241240
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
242241
match self {
243242
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
244-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
245243
Nonterminal::NtPath(path) => path.tokens_mut(),
246244
Nonterminal::NtBlock(block) => block.tokens_mut(),
247245
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,

compiler/rustc_ast/src/attr/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
55
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
66
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
77
use crate::ptr::P;
8-
use crate::token::{self, CommentKind, Delimiter, Token};
8+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, NonterminalKind, Token};
99
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1010
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1111
use crate::util::comments;
@@ -343,10 +343,18 @@ impl MetaItem {
343343
Path { span, segments, tokens: None }
344344
}
345345
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &nt.0 {
346-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
347346
token::Nonterminal::NtPath(path) => (**path).clone(),
348347
_ => return None,
349348
},
349+
Some(TokenTree::Delimited(
350+
_span,
351+
_spacing,
352+
Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Meta)),
353+
_stream,
354+
)) => {
355+
// This path is currently unreachable in the test suite.
356+
unreachable!()
357+
}
350358
Some(TokenTree::Token(
351359
Token { kind: token::OpenDelim(Delimiter::Invisible(_)), .. },
352360
_,

compiler/rustc_ast/src/mut_visit.rs

-6
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
828828
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
829829
token::NtLifetime(ident) => vis.visit_ident(ident),
830830
token::NtLiteral(expr) => vis.visit_expr(expr),
831-
token::NtMeta(item) => {
832-
let AttrItem { path, args, tokens } = item.deref_mut();
833-
vis.visit_path(path);
834-
visit_attr_args(args, vis);
835-
visit_lazy_tts(tokens, vis);
836-
}
837831
token::NtPath(path) => vis.visit_path(path),
838832
}
839833
}

compiler/rustc_ast/src/token.rs

-5
Original file line numberDiff line numberDiff line change
@@ -929,8 +929,6 @@ pub enum Nonterminal {
929929
NtIdent(Ident, IdentIsRaw),
930930
NtLifetime(Ident),
931931
NtLiteral(P<ast::Expr>),
932-
/// Stuff inside brackets for attributes
933-
NtMeta(P<ast::AttrItem>),
934932
NtPath(P<ast::Path>),
935933
}
936934

@@ -1018,7 +1016,6 @@ impl Nonterminal {
10181016
NtBlock(block) => block.span,
10191017
NtExpr(expr) | NtLiteral(expr) => expr.span,
10201018
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
1021-
NtMeta(attr_item) => attr_item.span(),
10221019
NtPath(path) => path.span,
10231020
}
10241021
}
@@ -1030,7 +1027,6 @@ impl Nonterminal {
10301027
NtLiteral(..) => "literal",
10311028
NtIdent(..) => "identifier",
10321029
NtLifetime(..) => "lifetime",
1033-
NtMeta(..) => "attribute",
10341030
NtPath(..) => "path",
10351031
}
10361032
}
@@ -1059,7 +1055,6 @@ impl fmt::Debug for Nonterminal {
10591055
NtExpr(..) => f.pad("NtExpr(..)"),
10601056
NtIdent(..) => f.pad("NtIdent(..)"),
10611057
NtLiteral(..) => f.pad("NtLiteral(..)"),
1062-
NtMeta(..) => f.pad("NtMeta(..)"),
10631058
NtPath(..) => f.pad("NtPath(..)"),
10641059
NtLifetime(..) => f.pad("NtLifetime(..)"),
10651060
}

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ impl TokenStream {
484484
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
485485
}
486486
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
487-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
488487
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
489488
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
490489
}

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845845
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
846846
match nt {
847847
token::NtExpr(e) => self.expr_to_string(e),
848-
token::NtMeta(e) => self.attr_item_to_string(e),
849848
token::NtPath(e) => self.path_to_string(e),
850849
token::NtBlock(e) => self.block_to_string(e),
851850
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),

compiler/rustc_expand/src/mbe/transcribe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ pub(super) fn transcribe<'a>(
289289
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
290290
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
291291
}
292+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
293+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
294+
}
292295
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
293296
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
294297
}

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
402402
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
403403
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
404404
405-
parse_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`
405+
parse_invalid_meta_item = expected unsuffixed literal or identifier, found {$descr}
406406
407407
parse_invalid_meta_item_unquoted_ident = expected unsuffixed literal, found `{$token}`
408408
.suggestion = surround the identifier with quotation marks to parse it as a string

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
968968
pub(crate) struct InvalidMetaItem {
969969
#[primary_span]
970970
pub span: Span,
971-
pub token: Token,
971+
pub descr: String,
972972
}
973973

974974
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/attr.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use crate::errors::{
33
SuffixedLiteralInAttribute,
44
};
55
use crate::fluent_generated as fluent;
6-
use crate::maybe_whole;
6+
use crate::maybe_reparse_metavar_seq;
77

8-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
8+
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, ParseNtResult, Parser, PathStyle};
99
use rustc_ast as ast;
1010
use rustc_ast::attr;
11-
use rustc_ast::token::{self, Delimiter};
11+
use rustc_ast::token::{self, Delimiter, NonterminalKind};
1212
use rustc_errors::{codes::*, Diag, PResult};
1313
use rustc_span::{sym, BytePos, Span};
1414
use thin_vec::ThinVec;
@@ -252,7 +252,15 @@ impl<'a> Parser<'a> {
252252
/// PATH `=` UNSUFFIXED_LIT
253253
/// The delimiters or `=` are still put into the resulting token stream.
254254
pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
255-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
255+
if let Some(item) = maybe_reparse_metavar_seq!(
256+
self,
257+
NonterminalKind::Meta,
258+
NonterminalKind::Meta,
259+
ParseNtResult::Meta(item),
260+
item
261+
) {
262+
return Ok(item.into_inner());
263+
}
256264

257265
let do_parse = |this: &mut Self| {
258266
let path = this.parse_path(PathStyle::Mod)?;
@@ -362,21 +370,21 @@ impl<'a> Parser<'a> {
362370
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
363371
/// ```
364372
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
365-
// We can't use `maybe_whole` here because it would bump in the `None`
366-
// case, which we don't want.
367-
if let token::Interpolated(nt) = &self.token.kind
368-
&& let token::NtMeta(attr_item) = &nt.0
369-
{
370-
match attr_item.meta(attr_item.path.span) {
373+
let mut parser = self.clone(); // njn: ugh
374+
if let Some(attr_item) = maybe_reparse_metavar_seq!(
375+
parser,
376+
NonterminalKind::Meta,
377+
NonterminalKind::Meta,
378+
ParseNtResult::Meta(attr_item),
379+
attr_item
380+
) {
381+
return match attr_item.meta(attr_item.path.span) {
371382
Some(meta) => {
372-
self.bump();
373-
return Ok(meta);
383+
*self = parser;
384+
Ok(meta)
374385
}
375-
None => self.unexpected()?,
376-
}
377-
}
378-
if let token::OpenDelim(Delimiter::Invisible(_)) = &self.token.kind {
379-
panic!("njn: invis-delim?");
386+
None => self.unexpected_any(),
387+
};
380388
}
381389

382390
let lo = self.token.span;
@@ -429,7 +437,8 @@ impl<'a> Parser<'a> {
429437
}));
430438
}
431439

432-
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, token }))
440+
let descr = super::token_descr(&token);
441+
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, descr }))
433442
}
434443
}
435444

compiler/rustc_parse/src/parser/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,7 @@ pub enum ParseNtResult<NtType> {
16851685
PatParam(P<ast::Pat>, /* inferred */ bool),
16861686
PatWithOr(P<ast::Pat>),
16871687
Ty(P<ast::Ty>),
1688+
Meta(P<ast::AttrItem>),
16881689
Vis(P<ast::Visibility>),
16891690

16901691
/// This variant will eventually be removed, along with `Token::Interpolate`.
@@ -1703,6 +1704,7 @@ impl<T> ParseNtResult<T> {
17031704
ParseNtResult::PatParam(x, inf) => ParseNtResult::PatParam(x, inf),
17041705
ParseNtResult::PatWithOr(x) => ParseNtResult::PatWithOr(x),
17051706
ParseNtResult::Ty(x) => ParseNtResult::Ty(x),
1707+
ParseNtResult::Meta(x) => ParseNtResult::Meta(x),
17061708
ParseNtResult::Vis(x) => ParseNtResult::Vis(x),
17071709
ParseNtResult::Nt(nt) => ParseNtResult::Nt(f(nt)),
17081710
}

compiler/rustc_parse/src/parser/nonterminal.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ impl<'a> Parser<'a> {
4848
NtExpr(_)
4949
| NtIdent(..)
5050
| NtLiteral(_) // `true`, `false`
51-
| NtMeta(_)
5251
| NtPath(_) => true,
5352

5453
NtBlock(_)
@@ -79,7 +78,7 @@ impl<'a> Parser<'a> {
7978
token::OpenDelim(Delimiter::Brace) => true,
8079
token::Interpolated(nt) => match &nt.0 {
8180
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
82-
NtIdent(..) | NtMeta(_) | NtPath(_) => false,
81+
NtIdent(..) | NtPath(_) => false,
8382
},
8483
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
8584
NonterminalKind::Block
@@ -221,7 +220,9 @@ impl<'a> Parser<'a> {
221220
NonterminalKind::Path => {
222221
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
223222
}
224-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
223+
NonterminalKind::Meta => {
224+
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?)));
225+
}
225226
NonterminalKind::Vis => {
226227
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
227228
this.parse_visibility(FollowedByType::Yes)

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 or identifier, found `n!()`
8+
//~^ ERROR expected unsuffixed literal or identifier, found expression `n!()`
99
struct S;
1010
};
1111
}

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 or identifier, found `n!()`
1+
error: expected unsuffixed literal or identifier, 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

+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 or identifier, found `concat!("nonexistent")`
32-
//~| ERROR expected unsuffixed literal or identifier, found `concat!("nonexistent")`
31+
//~^ ERROR expected unsuffixed literal or identifier, found expression `concat!("nonexistent")`
32+
//~| ERROR expected unsuffixed literal or identifier, found expression `concat!("nonexistent")`
3333
struct S10;
3434
}
3535
}

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 or identifier, found `concat!("nonexistent")`
57+
error: expected unsuffixed literal or identifier, 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 or identifier, found `concat!("nonexistent")`
68+
error: expected unsuffixed literal or identifier, 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

+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 or identifier, found `an(arbitrary token stream)`
5-
//~| ERROR expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
4+
//~^ ERROR expected unsuffixed literal or identifier, found `meta` metavariable
5+
//~| ERROR expected unsuffixed literal or identifier, found `meta` metavariable
66
struct S;
77
}
88
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
1+
error: expected unsuffixed literal or identifier, found `meta` metavariable
22
--> $DIR/attr-bad-meta-4.rs:3:15
33
|
44
LL | #[cfg($attr_item)]
@@ -9,7 +9,7 @@ LL | mac!(an(arbitrary token stream));
99
|
1010
= note: this error originates in the macro `mac` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

12-
error: expected unsuffixed literal or identifier, found `an(arbitrary token stream)`
12+
error: expected unsuffixed literal or identifier, found `meta` metavariable
1313
--> $DIR/attr-bad-meta-4.rs:3:15
1414
|
1515
LL | #[cfg($attr_item)]

0 commit comments

Comments
 (0)