Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d4b8ef3

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 0d4f9e5 commit d4b8ef3

20 files changed

+60
-49
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,13 @@ 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
}
239238
}
240239
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
241240
match self {
242241
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
243-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
244242
Nonterminal::NtPath(path) => path.tokens_mut(),
245243
Nonterminal::NtBlock(block) => block.tokens_mut(),
246244
}

compiler/rustc_ast/src/attr/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
77
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
88
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
99
use crate::ptr::P;
10-
use crate::token::{self, CommentKind, Delimiter, Token};
10+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, NonterminalKind, Token};
1111
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1212
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1313
use crate::util::comments;
@@ -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(NonterminalKind::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
@@ -822,12 +822,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
822822
token::NtBlock(block) => vis.visit_block(block),
823823
token::NtExpr(expr) => vis.visit_expr(expr),
824824
token::NtLiteral(expr) => vis.visit_expr(expr),
825-
token::NtMeta(item) => {
826-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
827-
vis.visit_path(path);
828-
visit_attr_args(args, vis);
829-
visit_lazy_tts(tokens, vis);
830-
}
831825
token::NtPath(path) => vis.visit_path(path),
832826
}
833827
}

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ impl Token {
757757
/// That is, is this a pre-parsed expression dropped into the token stream
758758
/// (which happens while parsing the result of macro expansion)?
759759
pub fn is_whole_expr(&self) -> bool {
760+
#[allow(irrefutable_let_patterns)] // njn: temp
760761
if let Interpolated(nt) = &self.kind
761762
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
762763
{
@@ -942,8 +943,6 @@ pub enum Nonterminal {
942943
NtBlock(P<ast::Block>),
943944
NtExpr(P<ast::Expr>),
944945
NtLiteral(P<ast::Expr>),
945-
/// Stuff inside brackets for attributes
946-
NtMeta(P<ast::AttrItem>),
947946
NtPath(P<ast::Path>),
948947
}
949948

@@ -1043,7 +1042,6 @@ impl Nonterminal {
10431042
match self {
10441043
NtBlock(block) => block.span,
10451044
NtExpr(expr) | NtLiteral(expr) => expr.span,
1046-
NtMeta(attr_item) => attr_item.span(),
10471045
NtPath(path) => path.span,
10481046
}
10491047
}
@@ -1053,7 +1051,6 @@ impl Nonterminal {
10531051
NtBlock(..) => "block",
10541052
NtExpr(..) => "expression",
10551053
NtLiteral(..) => "literal",
1056-
NtMeta(..) => "attribute",
10571054
NtPath(..) => "path",
10581055
}
10591056
}
@@ -1075,7 +1072,6 @@ impl fmt::Debug for Nonterminal {
10751072
NtBlock(..) => f.pad("NtBlock(..)"),
10761073
NtExpr(..) => f.pad("NtExpr(..)"),
10771074
NtLiteral(..) => f.pad("NtLiteral(..)"),
1078-
NtMeta(..) => f.pad("NtMeta(..)"),
10791075
NtPath(..) => f.pad("NtPath(..)"),
10801076
}
10811077
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ impl TokenStream {
467467
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
468468
match nt {
469469
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
470-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
471470
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
472471
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
473472
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ pub(super) fn transcribe<'a>(
319319
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
320320
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
321321
}
322+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
323+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
324+
}
322325
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
323326
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
324327
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
408408
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
409409
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
410410
411-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
411+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
412412
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
413413
414414
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
@@ -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
#[subdiagnostic]
973973
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
974974
}

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::errors;
22
use crate::fluent_generated as fluent;
3-
use crate::maybe_whole;
3+
use crate::maybe_reparse_metavar_seq;
44

5-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
5+
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, ParseNtResult, Parser, PathStyle};
66
use rustc_ast as ast;
77
use rustc_ast::attr;
8-
use rustc_ast::token::{self, Delimiter};
8+
use rustc_ast::token::{self, Delimiter, NonterminalKind};
99
use rustc_errors::{codes::*, Diag, PResult};
1010
use rustc_span::{sym, symbol::kw, BytePos, Span};
1111
use thin_vec::ThinVec;
@@ -249,7 +249,15 @@ impl<'a> Parser<'a> {
249249
/// PATH `=` UNSUFFIXED_LIT
250250
/// The delimiters or `=` are still put into the resulting token stream.
251251
pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
252-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
252+
if let Some(item) = maybe_reparse_metavar_seq!(
253+
self,
254+
NonterminalKind::Meta,
255+
NonterminalKind::Meta,
256+
ParseNtResult::Meta(item),
257+
item
258+
) {
259+
return Ok(item.into_inner());
260+
}
253261

254262
let do_parse = |this: &mut Self| {
255263
let is_unsafe = this.eat_keyword(kw::Unsafe);
@@ -374,18 +382,22 @@ impl<'a> Parser<'a> {
374382
/// MetaSeq = MetaItemInner (',' MetaItemInner)* ','? ;
375383
/// ```
376384
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
377-
// We can't use `maybe_whole` here because it would bump in the `None`
378-
// case, which we don't want.
379-
if let token::Interpolated(nt) = &self.token.kind
380-
&& let token::NtMeta(attr_item) = &**nt
381-
{
382-
match attr_item.meta(attr_item.path.span) {
385+
// Clone the parser so we can backtrack in the case where `attr_item.meta()` fails.
386+
let mut parser = self.clone();
387+
if let Some(attr_item) = maybe_reparse_metavar_seq!(
388+
parser,
389+
NonterminalKind::Meta,
390+
NonterminalKind::Meta,
391+
ParseNtResult::Meta(attr_item),
392+
attr_item
393+
) {
394+
return match attr_item.meta(attr_item.path.span) {
383395
Some(meta) => {
384-
self.bump();
385-
return Ok(meta);
396+
*self = parser;
397+
Ok(meta)
386398
}
387-
None => self.unexpected()?,
388-
}
399+
None => self.unexpected_any(),
400+
};
389401
}
390402

391403
let lo = self.token.span;
@@ -439,7 +451,7 @@ impl<'a> Parser<'a> {
439451

440452
let mut err = errors::InvalidMetaItem {
441453
span: self.token.span,
442-
token: self.token.clone(),
454+
descr: super::token_descr(&self.token),
443455
quote_ident_sugg: None,
444456
};
445457

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ macro_rules! maybe_whole_expr {
6363
$p.bump();
6464
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6565
}
66-
_ => {}
6766
};
6867
}
6968
};

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,7 @@ pub enum ParseNtResult {
17061706
PatParam(P<ast::Pat>, /* inferred */ bool),
17071707
PatWithOr(P<ast::Pat>),
17081708
Ty(P<ast::Ty>),
1709+
Meta(P<ast::AttrItem>),
17091710
Vis(P<ast::Visibility>),
17101711

17111712
/// 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
@@ -49,7 +49,6 @@ impl<'a> Parser<'a> {
4949
match nt {
5050
NtExpr(_)
5151
| NtLiteral(_) // `true`, `false`
52-
| NtMeta(_)
5352
| NtPath(_) => true,
5453

5554
NtBlock(_) => false,
@@ -87,7 +86,7 @@ impl<'a> Parser<'a> {
8786
token::NtLifetime(..) => true,
8887
token::Interpolated(nt) => match &**nt {
8988
NtBlock(_) | NtExpr(_) | NtLiteral(_) => true,
90-
NtMeta(_) | NtPath(_) => false,
89+
NtPath(_) => false,
9190
},
9291
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
9392
NonterminalKind::Block
@@ -226,7 +225,9 @@ impl<'a> Parser<'a> {
226225
NonterminalKind::Path => {
227226
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
228227
}
229-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
228+
NonterminalKind::Meta => {
229+
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?)));
230+
}
230231
NonterminalKind::Vis => {
231232
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
232233
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)