Skip to content

Commit 73618fc

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 70abe82 commit 73618fc

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
NormalAttr, Path, PathSegment, Safety,
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
@@ -657,7 +657,6 @@ impl Token {
657657
matches!(&**nt,
658658
| NtExpr(..)
659659
| NtLiteral(..)
660-
| NtMeta(..)
661660
| NtPath(..)
662661
),
663662
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -860,6 +859,7 @@ impl Token {
860859
/// Is this a pre-parsed expression dropped into the token stream
861860
/// (which happens while parsing the result of macro expansion)?
862861
pub fn is_whole_expr(&self) -> bool {
862+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
863863
if let Interpolated(nt) = &self.kind
864864
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
865865
{
@@ -1066,8 +1066,6 @@ pub enum Nonterminal {
10661066
NtBlock(P<ast::Block>),
10671067
NtExpr(P<ast::Expr>),
10681068
NtLiteral(P<ast::Expr>),
1069-
/// Stuff inside brackets for attributes
1070-
NtMeta(P<ast::AttrItem>),
10711069
NtPath(P<ast::Path>),
10721070
}
10731071

@@ -1159,7 +1157,6 @@ impl Nonterminal {
11591157
match self {
11601158
NtBlock(block) => block.span,
11611159
NtExpr(expr) | NtLiteral(expr) => expr.span,
1162-
NtMeta(attr_item) => attr_item.span(),
11631160
NtPath(path) => path.span,
11641161
}
11651162
}
@@ -1169,7 +1166,6 @@ impl Nonterminal {
11691166
NtBlock(..) => "block",
11701167
NtExpr(..) => "expression",
11711168
NtLiteral(..) => "literal",
1172-
NtMeta(..) => "attribute",
11731169
NtPath(..) => "path",
11741170
}
11751171
}
@@ -1191,7 +1187,6 @@ impl fmt::Debug for Nonterminal {
11911187
NtBlock(..) => f.pad("NtBlock(..)"),
11921188
NtExpr(..) => f.pad("NtExpr(..)"),
11931189
NtLiteral(..) => f.pad("NtLiteral(..)"),
1194-
NtMeta(..) => f.pad("NtMeta(..)"),
11951190
NtPath(..) => f.pad("NtPath(..)"),
11961191
}
11971192
}

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
@@ -335,6 +335,9 @@ pub(super) fn transcribe<'a>(
335335
MatchedSingle(ParseNtResult::Ty(ty)) => {
336336
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
337337
}
338+
MatchedSingle(ParseNtResult::Meta(meta)) => {
339+
mk_delimited(MetaVarKind::Meta, TokenStream::from_ast(meta))
340+
}
338341
MatchedSingle(ParseNtResult::Vis(vis)) => {
339342
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
340343
}

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

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
@@ -1399,7 +1399,6 @@ impl<'a> Parser<'a> {
13991399
self.bump();
14001400
return Ok(self.mk_expr(self.prev_token.span, ExprKind::Block(block, None)));
14011401
}
1402-
_ => {}
14031402
};
14041403
}
14051404

0 commit comments

Comments
 (0)