Skip to content

Commit ab342f7

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 487be59 commit ab342f7

File tree

20 files changed

+54
-48
lines changed

20 files changed

+54
-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
@@ -14,7 +14,7 @@ use crate::ast::{
1414
PathSegment, Safety,
1515
};
1616
use crate::ptr::P;
17-
use crate::token::{self, CommentKind, Delimiter, Token};
17+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, MetaVarKind, Token};
1818
use crate::tokenstream::{
1919
DelimSpan, LazyAttrTokenStream, Spacing, TokenStream, TokenStreamIter, TokenTree,
2020
};
@@ -406,10 +406,18 @@ impl MetaItem {
406406
Path { span, segments, tokens: None }
407407
}
408408
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
409-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
410409
token::Nonterminal::NtPath(path) => (**path).clone(),
411410
_ => return None,
412411
},
412+
Some(TokenTree::Delimited(
413+
_span,
414+
_spacing,
415+
Delimiter::Invisible(InvisibleOrigin::MetaVar(MetaVarKind::Meta)),
416+
_stream,
417+
)) => {
418+
// This path is currently unreachable in the test suite.
419+
unreachable!()
420+
}
413421
Some(TokenTree::Token(
414422
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
415423
_,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -895,12 +895,6 @@ fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
895895
token::NtBlock(block) => vis.visit_block(block),
896896
token::NtExpr(expr) => vis.visit_expr(expr),
897897
token::NtLiteral(expr) => vis.visit_expr(expr),
898-
token::NtMeta(item) => {
899-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
900-
vis.visit_path(path);
901-
visit_attr_args(vis, args);
902-
visit_lazy_tts(vis, tokens);
903-
}
904898
token::NtPath(path) => vis.visit_path(path),
905899
}
906900
}

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,6 @@ impl Token {
658658
matches!(&**nt,
659659
| NtExpr(..)
660660
| NtLiteral(..)
661-
| NtMeta(..)
662661
| NtPath(..)
663662
),
664663
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -861,6 +860,7 @@ impl Token {
861860
/// Is this a pre-parsed expression dropped into the token stream
862861
/// (which happens while parsing the result of macro expansion)?
863862
pub fn is_whole_expr(&self) -> bool {
863+
#[allow(irrefutable_let_patterns)] // FIXME: temporary
864864
if let Interpolated(nt) = &self.kind
865865
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
866866
{
@@ -1074,8 +1074,6 @@ pub enum Nonterminal {
10741074
NtBlock(P<ast::Block>),
10751075
NtExpr(P<ast::Expr>),
10761076
NtLiteral(P<ast::Expr>),
1077-
/// Stuff inside brackets for attributes
1078-
NtMeta(P<ast::AttrItem>),
10791077
NtPath(P<ast::Path>),
10801078
}
10811079

@@ -1167,7 +1165,6 @@ impl Nonterminal {
11671165
match self {
11681166
NtBlock(block) => block.span,
11691167
NtExpr(expr) | NtLiteral(expr) => expr.span,
1170-
NtMeta(attr_item) => attr_item.span(),
11711168
NtPath(path) => path.span,
11721169
}
11731170
}
@@ -1177,7 +1174,6 @@ impl Nonterminal {
11771174
NtBlock(..) => "block",
11781175
NtExpr(..) => "expression",
11791176
NtLiteral(..) => "literal",
1180-
NtMeta(..) => "attribute",
11811177
NtPath(..) => "path",
11821178
}
11831179
}
@@ -1199,7 +1195,6 @@ impl fmt::Debug for Nonterminal {
11991195
NtBlock(..) => f.pad("NtBlock(..)"),
12001196
NtExpr(..) => f.pad("NtExpr(..)"),
12011197
NtLiteral(..) => f.pad("NtLiteral(..)"),
1202-
NtMeta(..) => f.pad("NtMeta(..)"),
12031198
NtPath(..) => f.pad("NtPath(..)"),
12041199
}
12051200
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ pub(super) fn transcribe<'a>(
348348
TokenStream::from_ast(ty),
349349
)
350350
}
351+
MatchedSingle(ParseNtResult::Meta(meta)) => mk_delimited(
352+
meta.span(),
353+
MetaVarKind::Meta,
354+
TokenStream::from_ast(meta),
355+
),
351356
MatchedSingle(ParseNtResult::Vis(vis)) => {
352357
mk_delimited(vis.span, MetaVarKind::Vis, TokenStream::from_ast(vis))
353358
}

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
@@ -1025,7 +1025,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
10251025
pub(crate) struct InvalidMetaItem {
10261026
#[primary_span]
10271027
pub span: Span,
1028-
pub token: Token,
1028+
pub descr: String,
10291029
#[subdiagnostic]
10301030
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
10311031
}

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use rustc_ast::{self as ast, Attribute, attr, token};
1+
use rustc_ast as ast;
2+
use rustc_ast::token::{self, MetaVarKind};
3+
use rustc_ast::{Attribute, attr};
24
use rustc_errors::codes::*;
35
use rustc_errors::{Diag, PResult};
46
use rustc_span::{BytePos, Span};
@@ -9,7 +11,7 @@ use super::{
911
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
1012
UsePreAttrPos,
1113
};
12-
use crate::{errors, exp, fluent_generated as fluent, maybe_whole};
14+
use crate::{errors, exp, fluent_generated as fluent};
1315

1416
// Public for rustfmt usage
1517
#[derive(Debug)]
@@ -269,7 +271,11 @@ impl<'a> Parser<'a> {
269271
/// PATH `=` UNSUFFIXED_LIT
270272
/// The delimiters or `=` are still put into the resulting token stream.
271273
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
272-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
274+
if let Some(item) =
275+
self.eat_metavar_seq(MetaVarKind::Meta, |this| this.parse_attr_item(force_collect))
276+
{
277+
return Ok(item);
278+
}
273279

274280
// Attr items don't have attributes.
275281
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
@@ -396,18 +402,18 @@ impl<'a> Parser<'a> {
396402
&mut self,
397403
unsafe_allowed: AllowLeadingUnsafe,
398404
) -> PResult<'a, ast::MetaItem> {
399-
// We can't use `maybe_whole` here because it would bump in the `None`
400-
// case, which we don't want.
401-
if let token::Interpolated(nt) = &self.token.kind
402-
&& let token::NtMeta(attr_item) = &**nt
405+
// Snapshot the parser so we can backtrack in the case where `attr_item.meta()` fails.
406+
let mut snapshot = self.create_snapshot_for_diagnostic();
407+
if let Some(attr_item) = snapshot
408+
.eat_metavar_seq(MetaVarKind::Meta, |this| this.parse_attr_item(ForceCollect::No))
403409
{
404-
match attr_item.meta(attr_item.path.span) {
410+
return match attr_item.meta(attr_item.path.span) {
405411
Some(meta) => {
406-
self.bump();
407-
return Ok(meta);
412+
self.restore_snapshot(snapshot);
413+
Ok(meta)
408414
}
409-
None => self.unexpected()?,
410-
}
415+
None => self.unexpected_any(),
416+
};
411417
}
412418

413419
let lo = self.token.span;
@@ -464,7 +470,7 @@ impl<'a> Parser<'a> {
464470

465471
let mut err = errors::InvalidMetaItem {
466472
span: self.token.span,
467-
token: self.token.clone(),
473+
descr: super::token_descr(&self.token),
468474
quote_ident_sugg: None,
469475
};
470476

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)