Skip to content

Commit b0bc56a

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 98ef9f7 commit b0bc56a

File tree

17 files changed

+104
-366
lines changed

17 files changed

+104
-366
lines changed

compiler/rustc_ast/src/ast_traits.rs

-14
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

+3-37
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,9 @@ fn visit_lazy_tts<T: MutVisitor>(vis: &mut T, lazy_tts: &mut Option<LazyAttrToke
745745
visit_lazy_tts_opt_mut(vis, lazy_tts.as_mut());
746746
}
747747

748-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
749-
/// In practice the ident part is not actually used by specific visitors right now,
750-
/// but there's a test below checking that it works.
748+
/// Applies ident visitor if it's an ident. In practice this is not actually
749+
/// used by specific visitors right now, but there's a test below checking that
750+
/// it works.
751751
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
752752
pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
753753
let Token { kind, span } = t;
@@ -765,45 +765,11 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
765765
token::NtLifetime(ident, _is_raw) => {
766766
vis.visit_ident(ident);
767767
}
768-
token::Interpolated(nt) => {
769-
let nt = Lrc::make_mut(nt);
770-
visit_nonterminal(vis, nt);
771-
}
772768
_ => {}
773769
}
774770
vis.visit_span(span);
775771
}
776772

777-
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
778-
/// Applies the visitor to elements of interpolated nodes.
779-
//
780-
// N.B., this can occur only when applying a visitor to partially expanded
781-
// code, where parsed pieces have gotten implanted ito *other* macro
782-
// invocations. This is relevant for macro hygiene, but possibly not elsewhere.
783-
//
784-
// One problem here occurs because the types for flat_map_item, flat_map_stmt,
785-
// etc., allow the visitor to return *multiple* items; this is a problem for the
786-
// nodes here, because they insist on having exactly one piece. One solution
787-
// would be to mangle the MutVisitor trait to include one-to-many and
788-
// one-to-one versions of these entry points, but that would probably confuse a
789-
// lot of people and help very few. Instead, I'm just going to put in dynamic
790-
// checks. I think the performance impact of this will be pretty much
791-
// nonexistent. The danger is that someone will apply a `MutVisitor` to a
792-
// partially expanded node, and will be confused by the fact that their
793-
// `flat_map_item` or `flat_map_stmt` isn't getting called on `NtItem` or `NtStmt`
794-
// nodes. Hopefully they'll wind up reading this comment, and doing something
795-
// appropriate.
796-
//
797-
// BTW, design choice: I considered just changing the type of, e.g., `NtItem` to
798-
// contain multiple items, but decided against it when I looked at
799-
// `parse_item_or_view_item` and tried to figure out what I would do with
800-
// multiple items there....
801-
fn visit_nonterminal<T: MutVisitor>(vis: &mut T, nt: &mut token::Nonterminal) {
802-
match nt {
803-
token::NtBlock(block) => vis.visit_block(block),
804-
}
805-
}
806-
807773
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
808774
fn visit_defaultness<T: MutVisitor>(vis: &mut T, defaultness: &mut Defaultness) {
809775
match defaultness {

compiler/rustc_ast/src/token.rs

+18-113
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ use std::fmt;
33

44
pub use BinOpToken::*;
55
pub use LitKind::*;
6-
pub use Nonterminal::*;
76
pub use NtExprKind::*;
87
pub use NtPatKind::*;
98
pub use TokenKind::*;
10-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
11-
use rustc_data_structures::sync::Lrc;
129
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1310
use rustc_span::edition::Edition;
1411
#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
@@ -18,7 +15,6 @@ use rustc_span::symbol::{kw, sym};
1815
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
1916

2017
use crate::ast;
21-
use crate::ptr::P;
2218
use crate::util::case::Case;
2319

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

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

@@ -349,9 +345,7 @@ impl From<IdentIsRaw> for bool {
349345
}
350346
}
351347

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

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

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

591556
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
592-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
557+
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
593558
}
594559
}
595560

@@ -620,7 +585,6 @@ impl Token {
620585
PathSep | // global path
621586
Lifetime(..) | // labeled loop
622587
Pound => true, // expression attributes
623-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
624588
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
625589
MetaVarKind::Block |
626590
MetaVarKind::Expr { .. } |
@@ -693,7 +657,6 @@ impl Token {
693657
match self.kind {
694658
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
695659
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
696-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
697660
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
698661
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
699662
))) => true,
@@ -821,31 +784,20 @@ impl Token {
821784
/// Is this a pre-parsed expression dropped into the token stream
822785
/// (which happens while parsing the result of macro expansion)?
823786
pub fn is_metavar_expr(&self) -> bool {
824-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
825-
if let Interpolated(nt) = &self.kind
826-
&& let NtBlock(_) = &**nt
827-
{
828-
true
829-
} else if matches!(
787+
matches!(
830788
self.is_metavar_seq(),
831-
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
832-
) {
833-
true
834-
} else {
835-
false
836-
}
789+
Some(
790+
MetaVarKind::Expr { .. }
791+
| MetaVarKind::Literal
792+
| MetaVarKind::Path
793+
| MetaVarKind::Block
794+
)
795+
)
837796
}
838797

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

851803
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -870,7 +822,8 @@ impl Token {
870822
self.is_non_raw_ident_where(|id| id.name == kw)
871823
}
872824

873-
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this token is an identifier equal to `kw` ignoring the case.
825+
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
826+
/// token is an identifier equal to `kw` ignoring the case.
874827
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
875828
self.is_keyword(kw)
876829
|| (case == Case::Insensitive
@@ -991,7 +944,7 @@ impl Token {
991944
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
992945
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
993946
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
994-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
947+
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof => {
995948
return None;
996949
}
997950
};
@@ -1028,12 +981,6 @@ pub enum NtExprKind {
1028981
Expr2021 { inferred: bool },
1029982
}
1030983

1031-
#[derive(Clone, Encodable, Decodable)]
1032-
/// For interpolation during macro expansion.
1033-
pub enum Nonterminal {
1034-
NtBlock(P<ast::Block>),
1035-
}
1036-
1037984
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
1038985
pub enum NonterminalKind {
1039986
Item,
@@ -1117,47 +1064,6 @@ impl fmt::Display for NonterminalKind {
11171064
}
11181065
}
11191066

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

compiler/rustc_ast/src/tokenstream.rs

+4-22
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::{DUMMY_SP, Span, SpanDecoder, SpanEncoder, Symbol, sym};
2424

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

3030
/// 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) Lrc<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.trees().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)