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.
This commit is contained in:
Nicholas Nethercote 2024-04-18 16:22:02 +10:00
parent ef1114a964
commit 7ea59e053b
20 changed files with 67 additions and 64 deletions

View file

@ -1024,7 +1024,7 @@ pub(crate) struct SuffixedLiteralInAttribute {
pub(crate) struct InvalidMetaItem {
#[primary_span]
pub span: Span,
pub token: Token,
pub descr: String,
#[subdiagnostic]
pub quote_ident_sugg: Option<InvalidMetaItemQuoteIdentSugg>,
}

View file

@ -1,4 +1,6 @@
use rustc_ast::{self as ast, Attribute, attr, token};
use rustc_ast as ast;
use rustc_ast::token::{self, MetaVarKind};
use rustc_ast::{Attribute, attr};
use rustc_errors::codes::*;
use rustc_errors::{Diag, PResult};
use rustc_span::{BytePos, Span};
@ -9,7 +11,7 @@ use super::{
AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, ParserRange, PathStyle, Trailing,
UsePreAttrPos,
};
use crate::{errors, exp, fluent_generated as fluent, maybe_whole};
use crate::{errors, exp, fluent_generated as fluent};
// Public for rustfmt usage
#[derive(Debug)]
@ -269,7 +271,12 @@ impl<'a> Parser<'a> {
/// PATH `=` UNSUFFIXED_LIT
/// The delimiters or `=` are still put into the resulting token stream.
pub fn parse_attr_item(&mut self, force_collect: ForceCollect) -> PResult<'a, ast::AttrItem> {
maybe_whole!(self, NtMeta, |attr| attr.into_inner());
if let Some(item) = self.eat_metavar_seq_with_matcher(
|mv_kind| matches!(mv_kind, MetaVarKind::Meta { .. }),
|this| this.parse_attr_item(force_collect),
) {
return Ok(item);
}
// Attr items don't have attributes.
self.collect_tokens(None, AttrWrapper::empty(), force_collect, |this, _empty_attrs| {
@ -396,18 +403,17 @@ impl<'a> Parser<'a> {
&mut self,
unsafe_allowed: AllowLeadingUnsafe,
) -> PResult<'a, ast::MetaItem> {
// We can't use `maybe_whole` here because it would bump in the `None`
// case, which we don't want.
if let token::Interpolated(nt) = &self.token.kind
&& let token::NtMeta(attr_item) = &**nt
{
match attr_item.meta(attr_item.path.span) {
Some(meta) => {
self.bump();
return Ok(meta);
}
None => self.unexpected()?,
}
if let Some(MetaVarKind::Meta { has_meta_form }) = self.token.is_metavar_seq() {
return if has_meta_form {
let attr_item = self
.eat_metavar_seq(MetaVarKind::Meta { has_meta_form: true }, |this| {
this.parse_attr_item(ForceCollect::No)
})
.unwrap();
Ok(attr_item.meta(attr_item.path.span).unwrap())
} else {
self.unexpected_any()
};
}
let lo = self.token.span;
@ -464,7 +470,7 @@ impl<'a> Parser<'a> {
let mut err = errors::InvalidMetaItem {
span: self.token.span,
token: self.token.clone(),
descr: super::token_descr(&self.token),
quote_ident_sugg: None,
};

View file

@ -1748,6 +1748,7 @@ pub enum ParseNtResult {
Lifetime(Ident, IdentIsRaw),
Pat(P<ast::Pat>, NtPatKind),
Ty(P<ast::Ty>),
Meta(P<ast::AttrItem>),
Vis(P<ast::Visibility>),
/// This variant will eventually be removed, along with `Token::Interpolate`.

View file

@ -32,7 +32,7 @@ impl<'a> Parser<'a> {
| MetaVarKind::Expr { .. }
| MetaVarKind::Ty { .. }
| MetaVarKind::Literal // `true`, `false`
| MetaVarKind::Meta
| MetaVarKind::Meta { .. }
| MetaVarKind::Path => true,
MetaVarKind::Item
@ -51,7 +51,6 @@ impl<'a> Parser<'a> {
NtStmt(_)
| NtExpr(_)
| NtLiteral(_) // `true`, `false`
| NtMeta(_)
| NtPath(_) => true,
NtItem(_) | NtBlock(_) => false,
@ -98,7 +97,7 @@ impl<'a> Parser<'a> {
token::NtLifetime(..) => true,
token::Interpolated(nt) => match &**nt {
NtBlock(_) | NtStmt(_) | NtExpr(_) | NtLiteral(_) => true,
NtItem(_) | NtMeta(_) | NtPath(_) => false,
NtItem(_) | NtPath(_) => false,
},
token::OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(k))) => match k {
MetaVarKind::Block
@ -108,7 +107,7 @@ impl<'a> Parser<'a> {
MetaVarKind::Item
| MetaVarKind::Pat(_)
| MetaVarKind::Ty { .. }
| MetaVarKind::Meta
| MetaVarKind::Meta { .. }
| MetaVarKind::Path
| MetaVarKind::Vis => false,
MetaVarKind::Lifetime | MetaVarKind::Ident | MetaVarKind::TT => {
@ -207,7 +206,9 @@ impl<'a> Parser<'a> {
NonterminalKind::Path => {
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
}
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(ForceCollect::Yes)?)),
NonterminalKind::Meta => {
return Ok(ParseNtResult::Meta(P(self.parse_attr_item(ForceCollect::Yes)?)));
}
NonterminalKind::Vis => {
return Ok(ParseNtResult::Vis(P(self.collect_tokens_no_attrs(|this| {
this.parse_visibility(FollowedByType::Yes)