Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit ddf4827

Browse files
committed
Remove NtBlock.
XXX: also Nonterminal and TokenKind::Interpolated XXX: grep for interpolated/Interpolated, see if anything else can be removed?
1 parent 7413663 commit ddf4827

File tree

16 files changed

+106
-362
lines changed

16 files changed

+106
-362
lines changed

compiler/rustc_ast/src/ast_traits.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! The traits are not implemented exhaustively, only when actually necessary.
44
55
use crate::ptr::P;
6-
use crate::token::Nonterminal;
76
use crate::tokenstream::LazyAttrTokenStream;
87
use crate::{Arm, Crate, ExprField, FieldDef, GenericParam, Param, PatField, Variant};
98
use crate::{AssocItem, Expr, ForeignItem, Item, NodeId};
@@ -228,19 +227,6 @@ impl HasTokens for Attribute {
228227
}
229228
}
230229

231-
impl HasTokens for Nonterminal {
232-
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
233-
match self {
234-
Nonterminal::NtBlock(block) => block.tokens(),
235-
}
236-
}
237-
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
238-
match self {
239-
Nonterminal::NtBlock(block) => block.tokens_mut(),
240-
}
241-
}
242-
}
243-
244230
/// A trait for AST nodes having (or not having) attributes.
245231
pub trait HasAttrs {
246232
/// 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
@@ -764,9 +764,9 @@ fn visit_lazy_tts<T: MutVisitor>(lazy_tts: &mut Option<LazyAttrTokenStream>, vis
764764
visit_lazy_tts_opt_mut(lazy_tts.as_mut(), vis);
765765
}
766766

767-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
768-
/// In practice the ident part is not actually used by specific visitors right now,
769-
/// but there's a test below checking that it works.
767+
/// Applies ident visitor if it's an ident. In practice this is not actually
768+
/// used by specific visitors right now, but there's a test below checking that
769+
/// it works.
770770
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
771771
pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
772772
let Token { kind, span } = t;
@@ -784,45 +784,11 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
784784
token::NtLifetime(ident) => {
785785
vis.visit_ident(ident);
786786
}
787-
token::Interpolated(nt) => {
788-
let nt = Lrc::make_mut(nt);
789-
visit_nonterminal(nt, vis);
790-
}
791787
_ => {}
792788
}
793789
vis.visit_span(span);
794790
}
795791

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

compiler/rustc_ast/src/token.rs

Lines changed: 16 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
pub use BinOpToken::*;
22
pub use LitKind::*;
3-
pub use Nonterminal::*;
43
pub use NtExprKind::*;
54
pub use NtPatKind::*;
65
pub use TokenKind::*;
76

87
use crate::ast;
9-
use crate::ptr::P;
108
use crate::util::case::Case;
119

12-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
13-
use rustc_data_structures::sync::Lrc;
1410
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1511
use rustc_span::symbol::{kw, sym};
1612
#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
@@ -285,9 +281,7 @@ impl From<IdentIsRaw> for bool {
285281
}
286282
}
287283

288-
// SAFETY: due to the `Clone` impl below, all fields of all variants other than
289-
// `Interpolated` must impl `Copy`.
290-
#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
284+
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
291285
pub enum TokenKind {
292286
/* Expression-operator symbols. */
293287
/// `=`
@@ -376,21 +370,6 @@ pub enum TokenKind {
376370
/// the `lifetime` metavariable in the macro's RHS.
377371
NtLifetime(Ident),
378372

379-
/// An embedded AST node, as produced by a macro. This only exists for
380-
/// historical reasons. We'd like to get rid of it, for multiple reasons.
381-
/// - It's conceptually very strange. Saying a token can contain an AST
382-
/// node is like saying, in natural language, that a word can contain a
383-
/// sentence.
384-
/// - It requires special handling in a bunch of places in the parser.
385-
/// - It prevents `Token` from implementing `Copy`.
386-
/// It adds complexity and likely slows things down. Please don't add new
387-
/// occurrences of this token kind!
388-
///
389-
/// The span in the surrounding `Token` is that of the metavariable in the
390-
/// macro's RHS. The span within the Nonterminal is that of the fragment
391-
/// passed to the macro at the call site.
392-
Interpolated(Lrc<Nonterminal>),
393-
394373
/// A doc comment token.
395374
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
396375
/// similarly to symbols in string literal tokens.
@@ -400,19 +379,6 @@ pub enum TokenKind {
400379
Eof,
401380
}
402381

403-
impl Clone for TokenKind {
404-
fn clone(&self) -> Self {
405-
// `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
406-
// for all other variants, this implementation of `clone` is just like
407-
// a copy. This is faster than the `derive(Clone)` version which has a
408-
// separate path for every variant.
409-
match self {
410-
Interpolated(nt) => Interpolated(nt.clone()),
411-
_ => unsafe { std::ptr::read(self) },
412-
}
413-
}
414-
}
415-
416382
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
417383
pub struct Token {
418384
pub kind: TokenKind,
@@ -499,7 +465,6 @@ impl Token {
499465
pub fn uninterpolated_span(&self) -> Span {
500466
match self.kind {
501467
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
502-
Interpolated(ref nt) => nt.use_span(),
503468
_ => self.span,
504469
}
505470
}
@@ -517,7 +482,7 @@ impl Token {
517482
}
518483

519484
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
520-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
485+
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
521486
}
522487
}
523488

@@ -545,7 +510,6 @@ impl Token {
545510
PathSep | // global path
546511
Lifetime(..) | // labeled loop
547512
Pound => true, // expression attributes
548-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
549513
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
550514
NonterminalKind::Block |
551515
NonterminalKind::Expr(_) |
@@ -572,7 +536,6 @@ impl Token {
572536
| DotDot | DotDotDot | DotDotEq // ranges
573537
| Lt | BinOp(Shl) // associated path
574538
| PathSep => true, // global path
575-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
576539
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
577540
NonterminalKind::Block |
578541
NonterminalKind::Pat(_) |
@@ -613,7 +576,6 @@ impl Token {
613576
match self.kind {
614577
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
615578
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
616-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
617579
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
618580
NonterminalKind::Expr(_) | NonterminalKind::Block | NonterminalKind::Literal,
619581
))) => true,
@@ -757,31 +719,20 @@ impl Token {
757719
/// That is, is this a pre-parsed expression dropped into the token stream
758720
/// (which happens while parsing the result of macro expansion)?
759721
pub fn is_metavar_expr(&self) -> bool {
760-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
761-
if let Interpolated(nt) = &self.kind
762-
&& let NtBlock(_) = &**nt
763-
{
764-
true
765-
} else if matches!(
722+
matches!(
766723
self.is_metavar_seq(),
767-
Some(NonterminalKind::Expr(_) | NonterminalKind::Literal | NonterminalKind::Path)
768-
) {
769-
true
770-
} else {
771-
false
772-
}
724+
Some(
725+
NonterminalKind::Expr(_)
726+
| NonterminalKind::Literal
727+
| NonterminalKind::Path
728+
| NonterminalKind::Block
729+
)
730+
)
773731
}
774732

775-
/// Is the token an interpolated block (`$b:block`)?
776-
pub fn is_whole_block(&self) -> bool {
777-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
778-
if let Interpolated(nt) = &self.kind
779-
&& let NtBlock(..) = &**nt
780-
{
781-
return true;
782-
}
783-
784-
false
733+
/// Are we at a block from a metavar (`$b:block`)?
734+
pub fn is_metavar_block(&self) -> bool {
735+
matches!(self.is_metavar_seq(), Some(NonterminalKind::Block))
785736
}
786737

787738
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -806,7 +757,8 @@ impl Token {
806757
self.is_non_raw_ident_where(|id| id.name == kw)
807758
}
808759

809-
/// 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.
760+
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
761+
/// token is an identifier equal to `kw` ignoring the case.
810762
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
811763
self.is_keyword(kw)
812764
|| (case == Case::Insensitive
@@ -927,7 +879,7 @@ impl Token {
927879
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
928880
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
929881
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
930-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
882+
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof => {
931883
return None;
932884
}
933885
};
@@ -964,12 +916,6 @@ pub enum NtExprKind {
964916
Expr2021 { inferred: bool },
965917
}
966918

967-
#[derive(Clone, Encodable, Decodable)]
968-
/// For interpolation during macro expansion.
969-
pub enum Nonterminal {
970-
NtBlock(P<ast::Block>),
971-
}
972-
973919
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
974920
pub enum NonterminalKind {
975921
Item,
@@ -1053,47 +999,6 @@ impl fmt::Display for NonterminalKind {
1053999
}
10541000
}
10551001

1056-
impl Nonterminal {
1057-
pub fn use_span(&self) -> Span {
1058-
match self {
1059-
NtBlock(block) => block.span,
1060-
}
1061-
}
1062-
1063-
pub fn descr(&self) -> &'static str {
1064-
match self {
1065-
NtBlock(..) => "block",
1066-
}
1067-
}
1068-
}
1069-
1070-
impl PartialEq for Nonterminal {
1071-
fn eq(&self, _rhs: &Self) -> bool {
1072-
// FIXME: Assume that all nonterminals are not equal, we can't compare them
1073-
// correctly based on data from AST. This will prevent them from matching each other
1074-
// in macros. The comparison will become possible only when each nonterminal has an
1075-
// attached token stream from which it was parsed.
1076-
false
1077-
}
1078-
}
1079-
1080-
impl fmt::Debug for Nonterminal {
1081-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1082-
match *self {
1083-
NtBlock(..) => f.pad("NtBlock(..)"),
1084-
}
1085-
}
1086-
}
1087-
1088-
impl<CTX> HashStable<CTX> for Nonterminal
1089-
where
1090-
CTX: crate::HashStableContext,
1091-
{
1092-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
1093-
panic!("interpolated tokens should not be present in the HIR")
1094-
}
1095-
}
1096-
10971002
// Some types are used a lot. Make sure they don't unintentionally get bigger.
10981003
#[cfg(target_pointer_width = "64")]
10991004
mod size_asserts {
@@ -1102,7 +1007,6 @@ mod size_asserts {
11021007
// tidy-alphabetical-start
11031008
static_assert_size!(Lit, 12);
11041009
static_assert_size!(LitKind, 2);
1105-
static_assert_size!(Nonterminal, 8);
11061010
static_assert_size!(Token, 24);
11071011
static_assert_size!(TokenKind, 16);
11081012
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use crate::ast::AttrStyle;
1717
use crate::ast_traits::{HasAttrs, HasSpan, HasTokens};
18-
use crate::token::{self, Delimiter, InvisibleOrigin, Nonterminal, Token, TokenKind};
18+
use crate::token::{self, Delimiter, InvisibleOrigin, Token, TokenKind};
1919
use crate::AttrVec;
2020

2121
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -464,12 +464,6 @@ impl TokenStream {
464464
attr_stream.to_tokenstream()
465465
}
466466

467-
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
468-
match nt {
469-
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
470-
}
471-
}
472-
473467
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
474468
match token.kind {
475469
token::NtIdent(ident, is_raw) => {
@@ -481,12 +475,6 @@ impl TokenStream {
481475
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
482476
TokenStream::token_alone(token::Lifetime(ident.name), ident.span),
483477
),
484-
token::Interpolated(ref nt) => TokenTree::Delimited(
485-
DelimSpan::from_single(token.span),
486-
DelimSpacing::new(Spacing::JointHidden, spacing),
487-
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
488-
TokenStream::from_nonterminal_ast(&nt).flattened(),
489-
),
490478
_ => TokenTree::Token(token.clone(), spacing),
491479
}
492480
}
@@ -504,10 +492,9 @@ impl TokenStream {
504492
pub fn flattened(&self) -> TokenStream {
505493
fn can_skip(stream: &TokenStream) -> bool {
506494
stream.trees().all(|tree| match tree {
507-
TokenTree::Token(token, _) => !matches!(
508-
token.kind,
509-
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
510-
),
495+
TokenTree::Token(token, _) => {
496+
!matches!(token.kind, token::NtIdent(..) | token::NtLifetime(..))
497+
}
511498
TokenTree::Delimited(.., inner) => can_skip(inner),
512499
})
513500
}

compiler/rustc_ast_pretty/src/pprust/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,11 @@ pub mod state;
55
pub use state::{print_crate, AnnNode, Comments, PpAnn, PrintState, State};
66

77
use rustc_ast as ast;
8-
use rustc_ast::token::{Nonterminal, Token, TokenKind};
8+
use rustc_ast::token::{Token, TokenKind};
99
use rustc_ast::tokenstream::{TokenStream, TokenTree};
1010

1111
use std::borrow::Cow;
1212

13-
pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
14-
State::new().nonterminal_to_string(nt)
15-
}
16-
1713
/// Print the token kind precisely, without converting `$crate` into its respective crate name.
1814
pub fn token_kind_to_string(tok: &TokenKind) -> Cow<'static, str> {
1915
State::new().token_kind_to_string(tok)

0 commit comments

Comments
 (0)