Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d4b8ef3

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 0d4f9e5 commit d4b8ef3

20 files changed

+60
-49
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
@@ -7,7 +7,7 @@ use crate::ast::{DelimArgs, Expr, ExprKind, LitKind, MetaItemLit};
77
use crate::ast::{MetaItem, MetaItemKind, NestedMetaItem, NormalAttr};
88
use crate::ast::{Path, PathSegment, DUMMY_NODE_ID};
99
use crate::ptr::P;
10-
use crate::token::{self, CommentKind, Delimiter, Token};
10+
use crate::token::{self, CommentKind, Delimiter, InvisibleOrigin, NonterminalKind, Token};
1111
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1212
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1313
use crate::util::comments;
@@ -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(NonterminalKind::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
@@ -822,12 +822,6 @@ fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T) {
822822
token::NtBlock(block) => vis.visit_block(block),
823823
token::NtExpr(expr) => vis.visit_expr(expr),
824824
token::NtLiteral(expr) => vis.visit_expr(expr),
825-
token::NtMeta(item) => {
826-
let AttrItem { unsafety: _, path, args, tokens } = item.deref_mut();
827-
vis.visit_path(path);
828-
visit_attr_args(args, vis);
829-
visit_lazy_tts(tokens, vis);
830-
}
831825
token::NtPath(path) => vis.visit_path(path),
832826
}
833827
}

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,7 @@ impl Token {
757757
/// That is, is this a pre-parsed expression dropped into the token stream
758758
/// (which happens while parsing the result of macro expansion)?
759759
pub fn is_whole_expr(&self) -> bool {
760+
#[allow(irrefutable_let_patterns)] // njn: temp
760761
if let Interpolated(nt) = &self.kind
761762
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = &**nt
762763
{
@@ -942,8 +943,6 @@ pub enum Nonterminal {
942943
NtBlock(P<ast::Block>),
943944
NtExpr(P<ast::Expr>),
944945
NtLiteral(P<ast::Expr>),
945-
/// Stuff inside brackets for attributes
946-
NtMeta(P<ast::AttrItem>),
947946
NtPath(P<ast::Path>),
948947
}
949948

@@ -1043,7 +1042,6 @@ impl Nonterminal {
10431042
match self {
10441043
NtBlock(block) => block.span,
10451044
NtExpr(expr) | NtLiteral(expr) => expr.span,
1046-
NtMeta(attr_item) => attr_item.span(),
10471045
NtPath(path) => path.span,
10481046
}
10491047
}
@@ -1053,7 +1051,6 @@ impl Nonterminal {
10531051
NtBlock(..) => "block",
10541052
NtExpr(..) => "expression",
10551053
NtLiteral(..) => "literal",
1056-
NtMeta(..) => "attribute",
10571054
NtPath(..) => "path",
10581055
}
10591056
}
@@ -1075,7 +1072,6 @@ impl fmt::Debug for Nonterminal {
10751072
NtBlock(..) => f.pad("NtBlock(..)"),
10761073
NtExpr(..) => f.pad("NtExpr(..)"),
10771074
NtLiteral(..) => f.pad("NtLiteral(..)"),
1078-
NtMeta(..) => f.pad("NtMeta(..)"),
10791075
NtPath(..) => f.pad("NtPath(..)"),
10801076
}
10811077
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ impl TokenStream {
467467
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
468468
match nt {
469469
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
470-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
471470
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
472471
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
473472
}

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ pub(super) fn transcribe<'a>(
319319
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
320320
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
321321
}
322+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
323+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
324+
}
322325
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
323326
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
324327
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
408408
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
409409
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
410410
411-
parse_invalid_meta_item = expected unsuffixed literal, found `{$token}`
411+
parse_invalid_meta_item = expected unsuffixed literal, found {$descr}
412412
.quote_ident_sugg = surround the identifier with quotation marks to make it into a string literal
413413
414414
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
@@ -968,7 +968,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
968968
pub(crate) struct InvalidMetaItem {
969969
#[primary_span]
970970
pub span: Span,
971-
pub token: Token,
971+
pub descr: String,
972972
#[subdiagnostic]
973973
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
974974
}

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, symbol::kw, 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 is_unsafe = this.eat_keyword(kw::Unsafe);
@@ -374,18 +382,22 @@ impl<'a> Parser<'a> {
374382
/// MetaSeq = MetaItemInner (',' MetaItemInner)* ','? ;
375383
/// ```
376384
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
377-
// We can't use `maybe_whole` here because it would bump in the `None`
378-
// case, which we don't want.
379-
if let token::Interpolated(nt) = &self.token.kind
380-
&& let token::NtMeta(attr_item) = &**nt
381-
{
382-
match attr_item.meta(attr_item.path.span) {
385+
// Clone the parser so we can backtrack in the case where `attr_item.meta()` fails.
386+
let mut parser = self.clone();
387+
if let Some(attr_item) = maybe_reparse_metavar_seq!(
388+
parser,
389+
NonterminalKind::Meta,
390+
NonterminalKind::Meta,
391+
ParseNtResult::Meta(attr_item),
392+
attr_item
393+
) {
394+
return match attr_item.meta(attr_item.path.span) {
383395
Some(meta) => {
384-
self.bump();
385-
return Ok(meta);
396+
*self = parser;
397+
Ok(meta)
386398
}
387-
None => self.unexpected()?,
388-
}
399+
None => self.unexpected_any(),
400+
};
389401
}
390402

391403
let lo = self.token.span;
@@ -439,7 +451,7 @@ impl<'a> Parser<'a> {
439451

440452
let mut err = errors::InvalidMetaItem {
441453
span: self.token.span,
442-
token: self.token.clone(),
454+
descr: super::token_descr(&self.token),
443455
quote_ident_sugg: None,
444456
};
445457

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ macro_rules! maybe_whole_expr {
6363
$p.bump();
6464
return Ok($p.mk_expr($p.prev_token.span, ExprKind::Block(block, None)));
6565
}
66-
_ => {}
6766
};
6867
}
6968
};

0 commit comments

Comments
 (0)