Skip to content

Commit 65b8dfd

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 a574946 commit 65b8dfd

File tree

18 files changed

+58
-47
lines changed

18 files changed

+58
-47
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ 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
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,
@@ -241,7 +240,6 @@ impl HasTokens for Nonterminal {
241240
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
242241
match self {
243242
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => expr.tokens_mut(),
244-
Nonterminal::NtMeta(attr_item) => attr_item.tokens_mut(),
245243
Nonterminal::NtPath(path) => path.tokens_mut(),
246244
Nonterminal::NtBlock(block) => block.tokens_mut(),
247245
Nonterminal::NtIdent(..) | Nonterminal::NtLifetime(..) => None,

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;
@@ -343,10 +343,18 @@ impl MetaItem {
343343
Path { span, segments, tokens: None }
344344
}
345345
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &nt.0 {
346-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
347346
token::Nonterminal::NtPath(path) => (**path).clone(),
348347
_ => return None,
349348
},
349+
Some(TokenTree::Delimited(
350+
_span,
351+
_spacing,
352+
Delimiter::Invisible(InvisibleOrigin::MetaVar(NonterminalKind::Meta)),
353+
_stream,
354+
)) => {
355+
// This path is currently unreachable in the test suite.
356+
unreachable!()
357+
}
350358
Some(TokenTree::Token(
351359
Token { kind: token::OpenDelim(Delimiter::Invisible(_)), .. },
352360
_,

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
828828
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
829829
token::NtLifetime(ident) => vis.visit_ident(ident),
830830
token::NtLiteral(expr) => vis.visit_expr(expr),
831-
token::NtMeta(item) => {
832-
let AttrItem { path, args, tokens } = item.deref_mut();
833-
vis.visit_path(path);
834-
visit_attr_args(args, vis);
835-
visit_lazy_tts(tokens, vis);
836-
}
837831
token::NtPath(path) => vis.visit_path(path),
838832
}
839833
}

compiler/rustc_ast/src/token.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -918,8 +918,6 @@ pub enum Nonterminal {
918918
NtIdent(Ident, IdentIsRaw),
919919
NtLifetime(Ident),
920920
NtLiteral(P<ast::Expr>),
921-
/// Stuff inside brackets for attributes
922-
NtMeta(P<ast::AttrItem>),
923921
NtPath(P<ast::Path>),
924922
}
925923

@@ -1007,7 +1005,6 @@ impl Nonterminal {
10071005
NtBlock(block) => block.span,
10081006
NtExpr(expr) | NtLiteral(expr) => expr.span,
10091007
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
1010-
NtMeta(attr_item) => attr_item.span(),
10111008
NtPath(path) => path.span,
10121009
}
10131010
}
@@ -1019,7 +1016,6 @@ impl Nonterminal {
10191016
NtLiteral(..) => "literal",
10201017
NtIdent(..) => "identifier",
10211018
NtLifetime(..) => "lifetime",
1022-
NtMeta(..) => "attribute",
10231019
NtPath(..) => "path",
10241020
}
10251021
}
@@ -1048,7 +1044,6 @@ impl fmt::Debug for Nonterminal {
10481044
NtExpr(..) => f.pad("NtExpr(..)"),
10491045
NtIdent(..) => f.pad("NtIdent(..)"),
10501046
NtLiteral(..) => f.pad("NtLiteral(..)"),
1051-
NtMeta(..) => f.pad("NtMeta(..)"),
10521047
NtPath(..) => f.pad("NtPath(..)"),
10531048
NtLifetime(..) => f.pad("NtLifetime(..)"),
10541049
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ impl TokenStream {
484484
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
485485
}
486486
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
487-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
488487
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
489488
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
490489
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845845
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
846846
match nt {
847847
token::NtExpr(e) => self.expr_to_string(e),
848-
token::NtMeta(e) => self.attr_item_to_string(e),
849848
token::NtPath(e) => self.path_to_string(e),
850849
token::NtBlock(e) => self.block_to_string(e),
851850
&token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw.into()).to_string(),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ pub(super) fn transcribe<'a>(
289289
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
290290
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
291291
}
292+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
293+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
294+
}
292295
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
293296
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
294297
}

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
402402
.use_amp_amp_for_conjunction = use `&&` to perform logical conjunction
403403
.use_pipe_pipe_for_disjunction = use `||` to perform logical disjunction
404404
405-
parse_invalid_meta_item = expected unsuffixed literal or identifier, found `{$token}`
405+
parse_invalid_meta_item = expected unsuffixed literal or identifier, found {$descr}
406406
407407
parse_invalid_meta_item_unquoted_ident = expected unsuffixed literal, found `{$token}`
408408
.suggestion = surround the identifier with quotation marks to parse it as a string

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

974974
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use crate::errors::{
33
SuffixedLiteralInAttribute,
44
};
55
use crate::fluent_generated as fluent;
6-
use crate::maybe_whole;
6+
use crate::maybe_reparse_metavar_seq;
77

8-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
8+
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, ParseNtResult, Parser, PathStyle};
99
use rustc_ast as ast;
1010
use rustc_ast::attr;
11-
use rustc_ast::token::{self, Delimiter};
11+
use rustc_ast::token::{self, Delimiter, NonterminalKind};
1212
use rustc_errors::{codes::*, Diag, PResult};
1313
use rustc_span::{sym, BytePos, Span};
1414
use thin_vec::ThinVec;
@@ -252,7 +252,15 @@ impl<'a> Parser<'a> {
252252
/// PATH `=` UNSUFFIXED_LIT
253253
/// The delimiters or `=` are still put into the resulting token stream.
254254
pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
255-
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
255+
if let Some(item) = maybe_reparse_metavar_seq!(
256+
self,
257+
NonterminalKind::Meta,
258+
NonterminalKind::Meta,
259+
ParseNtResult::Meta(item),
260+
item
261+
) {
262+
return Ok(item.into_inner());
263+
}
256264

257265
let do_parse = |this: &mut Self| {
258266
let path = this.parse_path(PathStyle::Mod)?;
@@ -362,18 +370,21 @@ impl<'a> Parser<'a> {
362370
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
363371
/// ```
364372
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
365-
// We can't use `maybe_whole` here because it would bump in the `None`
366-
// case, which we don't want.
367-
if let token::Interpolated(nt) = &self.token.kind
368-
&& let token::NtMeta(attr_item) = &nt.0
369-
{
370-
match attr_item.meta(attr_item.path.span) {
373+
let mut parser = self.clone(); // njn: ugh
374+
if let Some(attr_item) = maybe_reparse_metavar_seq!(
375+
parser,
376+
NonterminalKind::Meta,
377+
NonterminalKind::Meta,
378+
ParseNtResult::Meta(attr_item),
379+
attr_item
380+
) {
381+
return match attr_item.meta(attr_item.path.span) {
371382
Some(meta) => {
372-
self.bump();
373-
return Ok(meta);
383+
*self = parser;
384+
Ok(meta)
374385
}
375-
None => self.unexpected()?,
376-
}
386+
None => self.unexpected_any(),
387+
};
377388
}
378389

379390
let lo = self.token.span;
@@ -426,7 +437,8 @@ impl<'a> Parser<'a> {
426437
}));
427438
}
428439

429-
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, token }))
440+
let descr = super::token_descr(&token);
441+
Err(self.dcx().create_err(InvalidMetaItem { span: token.span, descr }))
430442
}
431443
}
432444

0 commit comments

Comments
 (0)