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

File tree

21 files changed

+60
-50
lines changed

21 files changed

+60
-50
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
@@ -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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

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

0 commit comments

Comments
 (0)