Skip to content

Commit 6ea7de7

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 1d79660 commit 6ea7de7

21 files changed

+60
-50
lines changed

compiler/rustc_ast/src/ast_traits.rs

-2
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

+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;
@@ -347,10 +347,18 @@ impl MetaItem {
347347
Path { span, segments, tokens: None }
348348
}
349349
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
350-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
351350
token::Nonterminal::NtPath(path) => (**path).clone(),
352351
_ => return None,
353352
},
353+
Some(TokenTree::Delimited(
354+
_span,
355+
_spacing,
356+
Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Meta)),
357+
_stream,
358+
)) => {
359+
// This path is currently unreachable in the test suite.
360+
unreachable!()
361+
}
354362
Some(TokenTree::Token(
355363
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
356364
_,

compiler/rustc_ast/src/mut_visit.rs

-6
Original file line numberDiff line numberDiff line change
@@ -825,12 +825,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
825825
token::NtBlock(block) => vis.visit_block(block),
826826
token::NtExpr(expr) => vis.visit_expr(expr),
827827
token::NtLiteral(expr) => vis.visit_expr(expr),
828-
token::NtMeta(item) => {
829-
let AttrItem { path, args, tokens } = item.deref_mut();
830-
vis.visit_path(path);
831-
visit_attr_args(args, vis);
832-
visit_lazy_tts(tokens, vis);
833-
}
834828
token::NtPath(path) => vis.visit_path(path),
835829
}
836830
}

compiler/rustc_ast/src/token.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ impl Token {
736736
/// That is, is this a pre-parsed expression dropped into the token stream
737737
/// (which happens while parsing the result of macro expansion)?
738738
pub fn is_whole_expr(&self) -> bool {
739+
#[allow(irrefutable_let_patterns)] // njn: temp
739740
if let Interpolated(nt) = &self.kind
740741
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
741742
{
@@ -921,8 +922,6 @@ pub enum Nonterminal {
921922
NtBlock(P<ast::Block>),
922923
NtExpr(P<ast::Expr>),
923924
NtLiteral(P<ast::Expr>),
924-
/// Stuff inside brackets for attributes
925-
NtMeta(P<ast::AttrItem>),
926925
NtPath(P<ast::Path>),
927926
}
928927

@@ -1009,7 +1008,6 @@ impl Nonterminal {
10091008
match self {
10101009
NtBlock(block) => block.span,
10111010
NtExpr(expr) | NtLiteral(expr) => expr.span,
1012-
NtMeta(attr_item) => attr_item.span(),
10131011
NtPath(path) => path.span,
10141012
}
10151013
}
@@ -1019,7 +1017,6 @@ impl Nonterminal {
10191017
NtBlock(..) => "block",
10201018
NtExpr(..) => "expression",
10211019
NtLiteral(..) => "literal",
1022-
NtMeta(..) => "attribute",
10231020
NtPath(..) => "path",
10241021
}
10251022
}
@@ -1041,7 +1038,6 @@ impl fmt::Debug for Nonterminal {
10411038
NtBlock(..) => f.pad("NtBlock(..)"),
10421039
NtExpr(..) => f.pad("NtExpr(..)"),
10431040
NtLiteral(..) => f.pad("NtLiteral(..)"),
1044-
NtMeta(..) => f.pad("NtMeta(..)"),
10451041
NtPath(..) => f.pad("NtPath(..)"),
10461042
}
10471043
}

compiler/rustc_ast/src/tokenstream.rs

-1
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ impl TokenStream {
477477
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
478478
match nt {
479479
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
480-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
481480
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
482481
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
483482
}

compiler/rustc_ast_pretty/src/pprust/state.rs

-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
854854
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
855855
match nt {
856856
token::NtExpr(e) => self.expr_to_string(e),
857-
token::NtMeta(e) => self.attr_item_to_string(e),
858857
token::NtPath(e) => self.path_to_string(e),
859858
token::NtBlock(e) => self.block_to_string(e),
860859
token::NtLiteral(e) => self.expr_to_string(e),

compiler/rustc_expand/src/mbe/transcribe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ pub(super) fn transcribe<'a>(
306306
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
307307
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
308308
}
309+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
310+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
311+
}
309312
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
310313
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
311314
}

compiler/rustc_parse/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
406406
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
407407
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
408408
409-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
409+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
410410
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
411411
412412
parse_invalid_offset_of = offset_of expects dot-separated field and variant names

compiler/rustc_parse/src/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
977977
pub(crate) struct InvalidMetaItem {
978978
#[primary_span]
979979
pub span: Span,
980-
pub token: Token,
980+
pub descr: String,
981981
#[subdiagnostic]
982982
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
983983
}

compiler/rustc_parse/src/parser/attr.rs

+27-15
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, 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 path = this.parse_path(PathStyle::Mod)?;
@@ -360,18 +368,22 @@ impl<'a> Parser<'a> {
360368
/// MetaSeq = MetaItemInner (',' MetaItemInner)* ','? ;
361369
/// ```
362370
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
363-
// We can't use `maybe_whole` here because it would bump in the `None`
364-
// case, which we don't want.
365-
if let token::Interpolated(nt) = &self.token.kind
366-
&& let token::NtMeta(attr_item) = &**nt
367-
{
368-
match attr_item.meta(attr_item.path.span) {
371+
// Clone the parser so we can backtrack in the case where `attr_item.meta()` fails.
372+
let mut parser = self.clone();
373+
if let Some(attr_item) = maybe_reparse_metavar_seq!(
374+
parser,
375+
NonterminalKind::Meta,
376+
NonterminalKind::Meta,
377+
ParseNtResult::Meta(attr_item),
378+
attr_item
379+
) {
380+
return match attr_item.meta(attr_item.path.span) {
369381
Some(meta) => {
370-
self.bump();
371-
return Ok(meta);
382+
*self = parser;
383+
Ok(meta)
372384
}
373-
None => self.unexpected()?,
374-
}
385+
None => self.unexpected_any(),
386+
};
375387
}
376388

377389
let lo = self.token.span;
@@ -410,7 +422,7 @@ impl<'a> Parser<'a> {
410422

411423
let mut err = errors::InvalidMetaItem {
412424
span: self.token.span,
413-
token: self.token.clone(),
425+
descr: super::token_descr(&self.token),
414426
quote_ident_sugg: None,
415427
};
416428

compiler/rustc_parse/src/parser/expr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ macro_rules! maybe_whole_expr {
6161
$p.bump();
6262
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6363
}
64-
_ => {}
6564
};
6665
}
6766
};

compiler/rustc_parse/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,7 @@ pub enum ParseNtResult {
17181718
PatParam(P<ast::Pat>, /* inferred */ bool),
17191719
PatWithOr(P<ast::Pat>),
17201720
Ty(P<ast::Ty>),
1721+
Meta(P<ast::AttrItem>),
17211722
Vis(P<ast::Visibility>),
17221723

17231724
/// 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
@@ -48,7 +48,6 @@ impl<'a> Parser<'a> {
4848
match nt {
4949
NtExpr(_)
5050
| NtLiteral(_) // `true`, `false`
51-
| NtMeta(_)
5251
| NtPath(_) => true,
5352

5453
NtBlock(_) => false,
@@ -81,7 +80,7 @@ impl<'a> Parser<'a> {
8180
token::NtLifetime(..) => true,
8281
token::Interpolated(nt) => match &**nt {
8382
NtBlock(_) | NtExpr(_) | NtLiteral(_) => true,
84-
NtMeta(_) | NtPath(_) => false,
83+
NtPath(_) => false,
8584
},
8685
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
8786
NonterminalKind::Block
@@ -216,7 +215,9 @@ impl<'a> Parser<'a> {
216215
NonterminalKind::Path => {
217216
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
218217
}
219-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
218+
NonterminalKind::Meta => {
219+
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?)));
220+
}
220221
NonterminalKind::Vis => {
221222
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
222223
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, found `n!()`
8+
//~^ ERROR expected unsuffixed literal, 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, 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

+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
}

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)]

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
}

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)]

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

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)