Skip to content

Commit 43b0940

Browse files
committed
Remove NtBlock, Nonterminal, and TokenKind::Interpolated.
`NtBlock` is the last remaining variant of `Nonterminal`, so once it is gone then `Nonterminal` can be removed as well.
1 parent cd60cb2 commit 43b0940

File tree

17 files changed

+105
-369
lines changed

17 files changed

+105
-369
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use std::fmt;
66
use std::marker::PhantomData;
77

88
use crate::ptr::P;
9-
use crate::token::Nonterminal;
109
use crate::tokenstream::LazyAttrTokenStream;
1110
use crate::{
1211
Arm, AssocItem, AttrItem, AttrKind, AttrVec, Attribute, Block, Crate, Expr, ExprField,
@@ -196,19 +195,6 @@ impl HasTokens for Attribute {
196195
}
197196
}
198197

199-
impl HasTokens for Nonterminal {
200-
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
201-
match self {
202-
Nonterminal::NtBlock(block) => block.tokens(),
203-
}
204-
}
205-
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
206-
match self {
207-
Nonterminal::NtBlock(block) => block.tokens_mut(),
208-
}
209-
}
210-
}
211-
212198
/// A trait for AST nodes having (or not having) attributes.
213199
pub trait HasAttrs {
214200
/// This is `true` if this `HasAttrs` might support 'custom' (proc-macro) inner

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -837,9 +837,9 @@ fn visit_lazy_tts<T: MutVisitor>(vis: &mut T, lazy_tts: &mut Option<LazyAttrToke
837837
visit_lazy_tts_opt_mut(vis, lazy_tts.as_mut());
838838
}
839839

840-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
841-
/// In practice the ident part is not actually used by specific visitors right now,
842-
/// but there's a test below checking that it works.
840+
/// Applies ident visitor if it's an ident. In practice this is not actually
841+
/// used by specific visitors right now, but there's a test below checking that
842+
/// it works.
843843
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
844844
pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
845845
let Token { kind, span } = t;
@@ -857,45 +857,11 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
857857
token::NtLifetime(ident, _is_raw) => {
858858
vis.visit_ident(ident);
859859
}
860-
token::Interpolated(nt) => {
861-
let nt = Arc::make_mut(nt);
862-
visit_nonterminal(vis, nt);
863-
}
864860
_ => {}
865861
}
866862
vis.visit_span(span);
867863
}
868864

869-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
870-
/// Applies the visitor to elements of interpolated nodes.
871-
//
872-
// N.B., this can occur only when applying a visitor to partially expanded
873-
// code, where parsed pieces have gotten implanted ito *other* macro
874-
// invocations. This is relevant for macro hygiene, but possibly not elsewhere.
875-
//
876-
// One problem here occurs because the types for flat_map_item, flat_map_stmt,
877-
// etc., allow the visitor to return *multiple* items; this is a problem for the
878-
// nodes here, because they insist on having exactly one piece. One solution
879-
// would be to mangle the MutVisitor trait to include one-to-many and
880-
// one-to-one versions of these entry points, but that would probably confuse a
881-
// lot of people and help very few. Instead, I'm just going to put in dynamic
882-
// checks. I think the performance impact of this will be pretty much
883-
// nonexistent. The danger is that someone will apply a `MutVisitor` to a
884-
// partially expanded node, and will be confused by the fact that their
885-
// `flat_map_item` or `flat_map_stmt` isn't getting called on `NtItem` or `NtStmt`
886-
// nodes. Hopefully they'll wind up reading this comment, and doing something
887-
// appropriate.
888-
//
889-
// BTW, design choice: I considered just changing the type of, e.g., `NtItem` to
890-
// contain multiple items, but decided against it when I looked at
891-
// `parse_item_or_view_item` and tried to figure out what I would do with
892-
// multiple items there....
893-
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
894-
match nt {
895-
token::NtBlock(block) => vis.visit_block(block),
896-
}
897-
}
898-
899865
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
900866
fn visit_defaultness<T: MutVisitor>(vis: &mut T, defaultness: &mut Defaultness) {
901867
match defaultness {

compiler/rustc_ast/src/token.rs

Lines changed: 16 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::borrow::Cow;
22
use std::fmt;
3-
use std::sync::Arc;
43

54
pub use BinOpToken::*;
65
pub use LitKind::*;
7-
pub use Nonterminal::*;
86
pub use NtExprKind::*;
97
pub use NtPatKind::*;
108
pub use TokenKind::*;
11-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
129
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1310
use rustc_span::edition::Edition;
1411
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
@@ -17,7 +14,6 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
1714
use rustc_span::{Ident, Symbol};
1815

1916
use crate::ast;
20-
use crate::ptr::P;
2117
use crate::util::case::Case;
2218

2319
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
@@ -51,8 +47,8 @@ pub enum InvisibleOrigin {
5147
// `proc_macro::Delimiter::to_internal`, i.e. returned by a proc macro.
5248
ProcMacro,
5349

54-
// Converted from `TokenKind::Interpolated` in
55-
// `TokenStream::flatten_token`. Treated similarly to `ProcMacro`.
50+
// Converted from `TokenKind::NtLifetime` in `TokenStream::flatten_token`.
51+
// Treated similarly to `ProcMacro`.
5652
FlattenToken,
5753
}
5854

@@ -350,9 +346,7 @@ impl From<IdentIsRaw> for bool {
350346
}
351347
}
352348

353-
// SAFETY: due to the `Clone` impl below, all fields of all variants other than
354-
// `Interpolated` must impl `Copy`.
355-
#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
349+
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
356350
pub enum TokenKind {
357351
/* Expression-operator symbols. */
358352
/// `=`
@@ -441,21 +435,6 @@ pub enum TokenKind {
441435
/// the `lifetime` metavariable in the macro's RHS.
442436
NtLifetime(Ident, IdentIsRaw),
443437

444-
/// An embedded AST node, as produced by a macro. This only exists for
445-
/// historical reasons. We'd like to get rid of it, for multiple reasons.
446-
/// - It's conceptually very strange. Saying a token can contain an AST
447-
/// node is like saying, in natural language, that a word can contain a
448-
/// sentence.
449-
/// - It requires special handling in a bunch of places in the parser.
450-
/// - It prevents `Token` from implementing `Copy`.
451-
/// It adds complexity and likely slows things down. Please don't add new
452-
/// occurrences of this token kind!
453-
///
454-
/// The span in the surrounding `Token` is that of the metavariable in the
455-
/// macro's RHS. The span within the Nonterminal is that of the fragment
456-
/// passed to the macro at the call site.
457-
Interpolated(Arc<Nonterminal>),
458-
459438
/// A doc comment token.
460439
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
461440
/// similarly to symbols in string literal tokens.
@@ -465,19 +444,6 @@ pub enum TokenKind {
465444
Eof,
466445
}
467446

468-
impl Clone for TokenKind {
469-
fn clone(&self) -> Self {
470-
// `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
471-
// for all other variants, this implementation of `clone` is just like
472-
// a copy. This is faster than the `derive(Clone)` version which has a
473-
// separate path for every variant.
474-
match self {
475-
Interpolated(nt) => Interpolated(Arc::clone(nt)),
476-
_ => unsafe { std::ptr::read(self) },
477-
}
478-
}
479-
}
480-
481447
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
482448
pub struct Token {
483449
pub kind: TokenKind,
@@ -572,7 +538,6 @@ impl Token {
572538
pub fn uninterpolated_span(&self) -> Span {
573539
match self.kind {
574540
NtIdent(ident, _) | NtLifetime(ident, _) => ident.span,
575-
Interpolated(ref nt) => nt.use_span(),
576541
_ => self.span,
577542
}
578543
}
@@ -590,7 +555,7 @@ impl Token {
590555
}
591556

592557
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
593-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
558+
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
594559
}
595560
}
596561

@@ -621,7 +586,6 @@ impl Token {
621586
PathSep | // global path
622587
Lifetime(..) | // labeled loop
623588
Pound => true, // expression attributes
624-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
625589
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
626590
MetaVarKind::Block |
627591
MetaVarKind::Expr { .. } |
@@ -694,7 +658,6 @@ impl Token {
694658
match self.kind {
695659
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
696660
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
697-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
698661
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
699662
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
700663
))) => true,
@@ -822,31 +785,20 @@ impl Token {
822785
/// Is this a pre-parsed expression dropped into the token stream
823786
/// (which happens while parsing the result of macro expansion)?
824787
pub fn is_metavar_expr(&self) -> bool {
825-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
826-
if let Interpolated(nt) = &self.kind
827-
&& let NtBlock(_) = &**nt
828-
{
829-
true
830-
} else if matches!(
788+
matches!(
831789
self.is_metavar_seq(),
832-
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
833-
) {
834-
true
835-
} else {
836-
false
837-
}
790+
Some(
791+
MetaVarKind::Expr { .. }
792+
| MetaVarKind::Literal
793+
| MetaVarKind::Path
794+
| MetaVarKind::Block
795+
)
796+
)
838797
}
839798

840-
/// Is the token an interpolated block (`$b:block`)?
841-
pub fn is_whole_block(&self) -> bool {
842-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
843-
if let Interpolated(nt) = &self.kind
844-
&& let NtBlock(..) = &**nt
845-
{
846-
return true;
847-
}
848-
849-
false
799+
/// Are we at a block from a metavar (`$b:block`)?
800+
pub fn is_metavar_block(&self) -> bool {
801+
matches!(self.is_metavar_seq(), Some(MetaVarKind::Block))
850802
}
851803

852804
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -999,7 +951,7 @@ impl Token {
999951
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
1000952
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1001953
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
1002-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
954+
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof => {
1003955
return None;
1004956
}
1005957
};
@@ -1036,12 +988,6 @@ pub enum NtExprKind {
1036988
Expr2021 { inferred: bool },
1037989
}
1038990

1039-
#[derive(Clone, Encodable, Decodable)]
1040-
/// For interpolation during macro expansion.
1041-
pub enum Nonterminal {
1042-
NtBlock(P<ast::Block>),
1043-
}
1044-
1045991
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
1046992
pub enum NonterminalKind {
1047993
Item,
@@ -1125,47 +1071,6 @@ impl fmt::Display for NonterminalKind {
11251071
}
11261072
}
11271073

1128-
impl Nonterminal {
1129-
pub fn use_span(&self) -> Span {
1130-
match self {
1131-
NtBlock(block) => block.span,
1132-
}
1133-
}
1134-
1135-
pub fn descr(&self) -> &'static str {
1136-
match self {
1137-
NtBlock(..) => "block",
1138-
}
1139-
}
1140-
}
1141-
1142-
impl PartialEq for Nonterminal {
1143-
fn eq(&self, _rhs: &Self) -> bool {
1144-
// FIXME: Assume that all nonterminals are not equal, we can't compare them
1145-
// correctly based on data from AST. This will prevent them from matching each other
1146-
// in macros. The comparison will become possible only when each nonterminal has an
1147-
// attached token stream from which it was parsed.
1148-
false
1149-
}
1150-
}
1151-
1152-
impl fmt::Debug for Nonterminal {
1153-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1154-
match *self {
1155-
NtBlock(..) => f.pad("NtBlock(..)"),
1156-
}
1157-
}
1158-
}
1159-
1160-
impl<CTX> HashStable<CTX> for Nonterminal
1161-
where
1162-
CTX: crate::HashStableContext,
1163-
{
1164-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
1165-
panic!("interpolated tokens should not be present in the HIR")
1166-
}
1167-
}
1168-
11691074
// Some types are used a lot. Make sure they don't unintentionally get bigger.
11701075
#[cfg(target_pointer_width = "64")]
11711076
mod size_asserts {
@@ -1175,7 +1080,6 @@ mod size_asserts {
11751080
// tidy-alphabetical-start
11761081
static_assert_size!(Lit, 12);
11771082
static_assert_size!(LitKind, 2);
1178-
static_assert_size!(Nonterminal, 8);
11791083
static_assert_size!(Token, 24);
11801084
static_assert_size!(TokenKind, 16);
11811085
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
2525

2626
use crate::ast::AttrStyle;
2727
use crate::ast_traits::{HasAttrs, HasTokens};
28-
use crate::token::{self, Delimiter, InvisibleOrigin, Nonterminal, Token, TokenKind};
28+
use crate::token::{self, Delimiter, InvisibleOrigin, Token, TokenKind};
2929
use crate::{AttrVec, Attribute};
3030

3131
/// Part of a `TokenStream`.
@@ -305,11 +305,6 @@ pub struct AttrsTarget {
305305
}
306306

307307
/// A `TokenStream` is an abstract sequence of tokens, organized into [`TokenTree`]s.
308-
///
309-
/// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s
310-
/// instead of a representation of the abstract syntax tree.
311-
/// Today's `TokenTree`s can still contain AST via `token::Interpolated` for
312-
/// backwards compatibility.
313308
#[derive(Clone, Debug, Default, Encodable, Decodable)]
314309
pub struct TokenStream(pub(crate) Arc<Vec<TokenTree>>);
315310

@@ -476,12 +471,6 @@ impl TokenStream {
476471
TokenStream::new(tts)
477472
}
478473

479-
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
480-
match nt {
481-
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
482-
}
483-
}
484-
485474
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
486475
match token.kind {
487476
token::NtIdent(ident, is_raw) => {
@@ -493,12 +482,6 @@ impl TokenStream {
493482
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
494483
TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span),
495484
),
496-
token::Interpolated(ref nt) => TokenTree::Delimited(
497-
DelimSpan::from_single(token.span),
498-
DelimSpacing::new(Spacing::JointHidden, spacing),
499-
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
500-
TokenStream::from_nonterminal_ast(&nt).flattened(),
501-
),
502485
_ => TokenTree::Token(token.clone(), spacing),
503486
}
504487
}
@@ -516,10 +499,9 @@ impl TokenStream {
516499
pub fn flattened(&self) -> TokenStream {
517500
fn can_skip(stream: &TokenStream) -> bool {
518501
stream.iter().all(|tree| match tree {
519-
TokenTree::Token(token, _) => !matches!(
520-
token.kind,
521-
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
522-
),
502+
TokenTree::Token(token, _) => {
503+
!matches!(token.kind, token::NtIdent(..) | token::NtLifetime(..))
504+
}
523505
TokenTree::Delimited(.., inner) => can_skip(inner),
524506
})
525507
}

0 commit comments

Comments
 (0)