Skip to content

Commit 92d4c9f

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 2c45836 commit 92d4c9f

File tree

11 files changed

+43
-53
lines changed

11 files changed

+43
-53
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: 9 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, InvisibleSource, NonterminalKind, Token};
99
use crate::tokenstream::{DelimSpan, Spacing, TokenTree};
1010
use crate::tokenstream::{LazyAttrTokenStream, TokenStream};
1111
use crate::util::comments;
@@ -327,10 +327,17 @@ impl MetaItem {
327327
Path { span, segments, tokens: None }
328328
}
329329
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &**nt {
330-
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
331330
token::Nonterminal::NtPath(path) => (**path).clone(),
332331
_ => return None,
333332
},
333+
Some(TokenTree::Delimited(
334+
_span,
335+
Delimiter::Invisible(InvisibleSource::MetaVar(NonterminalKind::Meta)),
336+
_stream,
337+
)) => {
338+
// njn: this pre-existing (equivalent) path is unreachable in the test suite
339+
unreachable!()
340+
}
334341
_ => return None,
335342
};
336343
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -794,12 +794,6 @@ pub fn visit_nonterminal<T: MutVisitor>(nt: &mut token::Nonterminal, vis: &mut T
794794
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
795795
token::NtLifetime(ident) => vis.visit_ident(ident),
796796
token::NtLiteral(expr) => vis.visit_expr(expr),
797-
token::NtMeta(item) => {
798-
let AttrItem { path, args, tokens } = item.deref_mut();
799-
vis.visit_path(path);
800-
visit_attr_args(args, vis);
801-
visit_lazy_tts(tokens, vis);
802-
}
803797
token::NtPath(path) => vis.visit_path(path),
804798
}
805799
}

compiler/rustc_ast/src/token.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,8 +847,6 @@ pub enum Nonterminal {
847847
NtIdent(Ident, /* is_raw */ bool),
848848
NtLifetime(Ident),
849849
NtLiteral(P<ast::Expr>),
850-
/// Stuff inside brackets for attributes
851-
NtMeta(P<ast::AttrItem>),
852850
NtPath(P<ast::Path>),
853851
}
854852

@@ -936,7 +934,6 @@ impl Nonterminal {
936934
NtBlock(block) => block.span,
937935
NtExpr(expr) | NtLiteral(expr) => expr.span,
938936
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
939-
NtMeta(attr_item) => attr_item.span(),
940937
NtPath(path) => path.span,
941938
}
942939
}
@@ -965,7 +962,6 @@ impl fmt::Debug for Nonterminal {
965962
NtExpr(..) => f.pad("NtExpr(..)"),
966963
NtIdent(..) => f.pad("NtIdent(..)"),
967964
NtLiteral(..) => f.pad("NtLiteral(..)"),
968-
NtMeta(..) => f.pad("NtMeta(..)"),
969965
NtPath(..) => f.pad("NtPath(..)"),
970966
NtLifetime(..) => f.pad("NtLifetime(..)"),
971967
}

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ impl TokenStream {
464464
TokenStream::token_alone(token::Lifetime(ident.name), ident.span)
465465
}
466466
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
467-
Nonterminal::NtMeta(attr) => TokenStream::from_ast(attr),
468467
Nonterminal::NtPath(path) => TokenStream::from_ast(path),
469468
Nonterminal::NtExpr(expr) | Nonterminal::NtLiteral(expr) => TokenStream::from_ast(expr),
470469
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,6 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
727727
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
728728
match nt {
729729
token::NtExpr(e) => self.expr_to_string(e),
730-
token::NtMeta(e) => self.attr_item_to_string(e),
731730
token::NtPath(e) => self.path_to_string(e),
732731
token::NtBlock(e) => self.block_to_string(e),
733732
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),

compiler/rustc_expand/src/mbe/transcribe.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ pub(super) fn transcribe<'a>(
255255
MatchedSingle(ParseNtResult::Ty(ref ty)) => {
256256
mk_delimited(NonterminalKind::Ty, TokenStream::from_ast(ty))
257257
}
258+
MatchedSingle(ParseNtResult::Meta(ref meta)) => {
259+
mk_delimited(NonterminalKind::Meta, TokenStream::from_ast(meta))
260+
}
258261
MatchedSingle(ParseNtResult::Vis(ref vis)) => {
259262
mk_delimited(NonterminalKind::Vis, TokenStream::from_ast(vis))
260263
}

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::errors::{InvalidMetaItem, SuffixedLiteralInAttribute};
22
use crate::fluent_generated as fluent;
3+
use crate::maybe_reparse_metavar_seq;
34

4-
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, Parser, PathStyle};
5+
use super::{AttrWrapper, Capturing, FnParseMode, ForceCollect, ParseNtResult, Parser, PathStyle};
56
use rustc_ast as ast;
67
use rustc_ast::attr;
7-
use rustc_ast::token::{self, Delimiter, Nonterminal};
8+
use rustc_ast::token::{self, Delimiter, NonterminalKind};
89
use rustc_errors::{error_code, Diagnostic, IntoDiagnostic, PResult};
910
use rustc_span::{sym, BytePos, Span};
1011
use std::convert::TryInto;
@@ -248,25 +249,23 @@ impl<'a> Parser<'a> {
248249
/// PATH `=` UNSUFFIXED_LIT
249250
/// The delimiters or `=` are still put into the resulting token stream.
250251
pub fn parse_attr_item(&mut self, capture_tokens: bool) -> PResult<'a, ast::AttrItem> {
251-
let item = match &self.token.kind {
252-
token::Interpolated(nt) => match &**nt {
253-
Nonterminal::NtMeta(item) => Some(item.clone().into_inner()),
254-
_ => None,
255-
},
256-
_ => None,
257-
};
258-
Ok(if let Some(item) = item {
259-
self.bump();
252+
if let Some(item) = maybe_reparse_metavar_seq!(
253+
self,
254+
NonterminalKind::Meta,
255+
NonterminalKind::Meta,
256+
ParseNtResult::Meta(item),
260257
item
261-
} else {
262-
let do_parse = |this: &mut Self| {
263-
let path = this.parse_path(PathStyle::Mod)?;
264-
let args = this.parse_attr_args()?;
265-
Ok(ast::AttrItem { path, args, tokens: None })
266-
};
267-
// Attr items don't have attributes
268-
if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }?
269-
})
258+
) {
259+
return Ok(item.into_inner());
260+
}
261+
262+
let do_parse = |this: &mut Self| {
263+
let path = this.parse_path(PathStyle::Mod)?;
264+
let args = this.parse_attr_args()?;
265+
Ok(ast::AttrItem { path, args, tokens: None })
266+
};
267+
// Attr items don't have attributes
268+
if capture_tokens { self.collect_tokens_no_attrs(do_parse) } else { do_parse(self) }
270269
}
271270

272271
/// Parses attributes that appear after the opening of an item. These should
@@ -368,20 +367,15 @@ impl<'a> Parser<'a> {
368367
/// meta_item_inner : (meta_item | UNSUFFIXED_LIT) (',' meta_item_inner)? ;
369368
/// ```
370369
pub fn parse_meta_item(&mut self) -> PResult<'a, ast::MetaItem> {
371-
let nt_meta = match &self.token.kind {
372-
token::Interpolated(nt) => match &**nt {
373-
token::NtMeta(e) => Some(e.clone()),
374-
_ => None,
375-
},
376-
_ => None,
377-
};
378-
379-
if let Some(item) = nt_meta {
370+
if let Some(item) = maybe_reparse_metavar_seq!(
371+
self,
372+
NonterminalKind::Meta,
373+
NonterminalKind::Meta,
374+
ParseNtResult::Meta(item),
375+
item
376+
) {
380377
return match item.meta(item.path.span) {
381-
Some(meta) => {
382-
self.bump();
383-
Ok(meta)
384-
}
378+
Some(meta) => Ok(meta),
385379
None => self.unexpected(),
386380
};
387381
}

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ pub enum ParseNtResult {
15871587
PatParam(P<ast::Pat>, /* inferred */ bool),
15881588
PatWithOr(P<ast::Pat>),
15891589
Ty(P<ast::Ty>),
1590+
Meta(P<ast::AttrItem>),
15901591
Vis(P<ast::Visibility>),
15911592

15921593
/// This case will eventually be removed, along with `Token::Interpolate`.

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ impl<'a> Parser<'a> {
4848
NtExpr(_)
4949
| NtIdent(..)
5050
| NtLiteral(_) // `true`, `false`
51-
| NtMeta(_)
5251
| NtPath(_) => true,
5352

5453
NtBlock(_)
@@ -79,7 +78,7 @@ impl<'a> Parser<'a> {
7978
token::OpenDelim(Delimiter::Brace) => true,
8079
token::Interpolated(nt) => match **nt {
8180
NtBlock(_) | NtLifetime(_) | NtExpr(_) | NtLiteral(_) => true,
82-
NtIdent(..) | NtMeta(_) | NtPath(_) => false,
81+
NtIdent(..) | NtPath(_) => false,
8382
},
8483
token::OpenDelim(Delimiter::Invisible(InvisibleSource::MetaVar(k))) => match k {
8584
NonterminalKind::Block
@@ -217,7 +216,7 @@ impl<'a> Parser<'a> {
217216
NonterminalKind::Path => NtPath(
218217
P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?),
219218
),
220-
NonterminalKind::Meta => NtMeta(P(self.parse_attr_item(true)?)),
219+
NonterminalKind::Meta => return Ok(ParseNtResult::Meta(P(self.parse_attr_item(true)?))),
221220
NonterminalKind::Vis => return Ok(ParseNtResult::Vis(
222221
P(self.collect_tokens_no_attrs(|this| this.parse_visibility(FollowedByType::Yes))?),
223222
)),

tests/ui/macros/stringify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ fn test_meta() {
641641
assert_eq!(stringify_meta!(k), "k");
642642
assert_eq!(stringify_meta!(k = "v"), "k = \"v\"");
643643
assert_eq!(stringify_meta!(list(k1, k2 = "v")), "list(k1, k2 = \"v\")");
644-
assert_eq!(stringify_meta!(serde::k), "serde::k");
644+
assert_eq!(stringify_meta!(serde::k), "serde :: k");
645645
}
646646

647647
#[test]

0 commit comments

Comments
 (0)