Skip to content

Commit 2b2ed2c

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 f2eacb1 commit 2b2ed2c

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;
@@ -411,10 +411,18 @@ impl MetaItem {
411411
Path { span, segments, tokens: None }
412412
}
413413
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
414-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
415414
token::Nonterminal::NtPath(path) => (**path).clone(),
416415
_ => return None,
417416
},
417+
Some(TokenTree::Delimited(
418+
_span,
419+
_spacing,
420+
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta)),
421+
_stream,
422+
)) => {
423+
// This path is currently unreachable in the test suite.
424+
unreachable!()
425+
}
418426
Some(TokenTree::Token(
419427
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
420428
_,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
799799
token::NtBlock(block) => vis.visit_block(block),
800800
token::NtExpr(expr) => vis.visit_expr(expr),
801801
token::NtLiteral(expr) => vis.visit_expr(expr),
802-
token::NtMeta(item) => {
803-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
804-
vis.visit_path(path);
805-
visit_attr_args(vis, args);
806-
visit_lazy_tts(vis, tokens);
807-
}
808802
token::NtPath(path) => vis.visit_path(path),
809803
}
810804
}

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
@@ -461,7 +461,6 @@ impl TokenStream {
461461
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
462462
match nt {
463463
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
464-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
465464
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
466465
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
467466
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ pub(super) fn transcribe<'a>(
336336
MatchedSingle(ParseNtResult::Ty(ty)) => {
337337
mk_delimited(MetaVarKind::Ty, TokenStream::from_ast(ty))
338338
}
339+
MatchedSingle(ParseNtResult::Meta(meta)) => {
340+
mk_delimited(MetaVarKind::Meta, TokenStream::from_ast(meta))
341+
}
339342
MatchedSingle(ParseNtResult::Vis(vis)) => {
340343
mk_delimited(MetaVarKind::Vis, TokenStream::from_ast(vis))
341344
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
424424
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
425425
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
426426
427-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
427+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
428428
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
429429
430430
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| {
@@ -399,18 +403,18 @@ impl<'a> Parser<'a> {
399403
&mut self,
400404
unsafe_allowed: AllowLeadingUnsafe,
401405
) -> PResult<'a, ast::MetaItem> {
402-
// We can't use `maybe_whole` here because it would bump in the `None`
403-
// case, which we don't want.
404-
if let token::Interpolated(nt) = &self.token.kind
405-
&& let token::NtMeta(attr_item) = &**nt
406+
// Snapshot the parser so we can backtrack in the case where `attr_item.meta()` fails.
407+
let mut snapshot = self.create_snapshot_for_diagnostic();
408+
if let Some(attr_item) = snapshot
409+
.eat_metavar_seq(MetaVarKind::Meta, |this| this.parse_attr_item(ForceCollect::No))
406410
{
407-
match attr_item.meta(attr_item.path.span) {
411+
return match attr_item.meta(attr_item.path.span) {
408412
Some(meta) => {
409-
self.bump();
410-
return Ok(meta);
413+
self.restore_snapshot(snapshot);
414+
Ok(meta)
411415
}
412-
None => self.unexpected()?,
413-
}
416+
None => self.unexpected_any(),
417+
};
414418
}
415419

416420
let lo = self.token.span;
@@ -467,7 +471,7 @@ impl<'a> Parser<'a> {
467471

468472
let mut err = errors::InvalidMetaItem {
469473
span: self.token.span,
470-
token: self.token.clone(),
474+
descr: super::token_descr(&self.token),
471475
quote_ident_sugg: None,
472476
};
473477

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)