Skip to content

Commit 13fdc3d

Browse files
committed
Remove NtBlock.
XXX: also Nonterminal and TokenKind::Interpolated
1 parent df017a6 commit 13fdc3d

File tree

16 files changed

+98
-356
lines changed

16 files changed

+98
-356
lines changed

compiler/rustc_ast/src/ast_traits.rs

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

+3-37
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,9 @@ fn visit_lazy_tts<T: MutVisitor>(lazy_tts: &mut Option<LazyAttrTokenStream>, vis
767767
visit_lazy_tts_opt_mut(lazy_tts.as_mut(), vis);
768768
}
769769

770-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
771-
/// In practice the ident part is not actually used by specific visitors right now,
772-
/// but there's a test below checking that it works.
770+
/// Applies ident visitor if it's an ident. In practice this is not actually
771+
/// used by specific visitors right now, but there's a test below checking that
772+
/// it works.
773773
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
774774
pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
775775
let Token { kind, span } = t;
@@ -787,45 +787,11 @@ pub fn visit_token<T: MutVisitor>(t: &mut Token, vis: &mut T) {
787787
token::NtLifetime(ident) => {
788788
vis.visit_ident(ident);
789789
}
790-
token::Interpolated(nt) => {
791-
let nt = Lrc::make_mut(nt);
792-
visit_nonterminal(nt, vis);
793-
}
794790
_ => {}
795791
}
796792
vis.visit_span(span);
797793
}
798794

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

compiler/rustc_ast/src/token.rs

+16-112
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
pub use BinOpToken::*;
22
pub use LitKind::*;
3-
pub use Nonterminal::*;
43
pub use TokenKind::*;
54

65
use crate::ast;
7-
use crate::ptr::P;
86
use crate::util::case::Case;
97

10-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
11-
use rustc_data_structures::sync::Lrc;
128
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
139
use rustc_span::symbol::{kw, sym};
1410
#[allow(clippy::useless_attribute)] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
@@ -282,9 +278,7 @@ impl From<IdentIsRaw> for bool {
282278
}
283279
}
284280

285-
// SAFETY: due to the `Clone` impl below, all fields of all variants other than
286-
// `Interpolated` must impl `Copy`.
287-
#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
281+
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
288282
pub enum TokenKind {
289283
/* Expression-operator symbols. */
290284
/// `=`
@@ -373,21 +367,6 @@ pub enum TokenKind {
373367
/// the `lifetime` metavariable in the macro's RHS.
374368
NtLifetime(Ident),
375369

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

400-
impl Clone for TokenKind {
401-
fn clone(&self) -> Self {
402-
// `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
403-
// for all other variants, this implementation of `clone` is just like
404-
// a copy. This is faster than the `derive(Clone)` version which has a
405-
// separate path for every variant.
406-
match self {
407-
Interpolated(nt) => Interpolated(nt.clone()),
408-
_ => unsafe { std::ptr::read(self) },
409-
}
410-
}
411-
}
412-
413379
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
414380
pub struct Token {
415381
pub kind: TokenKind,
@@ -496,7 +462,6 @@ impl Token {
496462
pub fn uninterpolated_span(&self) -> Span {
497463
match self.kind {
498464
NtIdent(ident, _) | NtLifetime(ident) => ident.span,
499-
Interpolated(ref nt) => nt.use_span(),
500465
_ => self.span,
501466
}
502467
}
@@ -514,7 +479,7 @@ impl Token {
514479
}
515480

516481
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
517-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
482+
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
518483
}
519484
}
520485

@@ -542,7 +507,6 @@ impl Token {
542507
PathSep | // global path
543508
Lifetime(..) | // labeled loop
544509
Pound => true, // expression attributes
545-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
546510
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
547511
NonterminalKind::Block |
548512
NonterminalKind::Expr |
@@ -569,7 +533,6 @@ impl Token {
569533
| DotDot | DotDotDot | DotDotEq // ranges
570534
| Lt | BinOp(Shl) // associated path
571535
| PathSep => true, // global path
572-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
573536
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
574537
NonterminalKind::Block |
575538
NonterminalKind::PatParam { .. } |
@@ -610,7 +573,6 @@ impl Token {
610573
pub fn can_begin_const_arg(&self) -> bool {
611574
match self.kind {
612575
OpenDelim(Delimiter::Brace) => true,
613-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
614576
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
615577
NonterminalKind::Expr | NonterminalKind::Block | NonterminalKind::Literal,
616578
))) => true,
@@ -733,31 +695,20 @@ impl Token {
733695
/// That is, is this a pre-parsed expression dropped into the token stream
734696
/// (which happens while parsing the result of macro expansion)?
735697
pub fn is_metavar_expr(&self) -> bool {
736-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
737-
if let Interpolated(nt) = &self.kind
738-
&& let NtBlock(_) = &**nt
739-
{
740-
true
741-
} else if matches!(
698+
matches!(
742699
self.is_metavar_seq(),
743-
Some(NonterminalKind::Expr | NonterminalKind::Literal | NonterminalKind::Path)
744-
) {
745-
true
746-
} else {
747-
false
748-
}
700+
Some(
701+
NonterminalKind::Expr
702+
| NonterminalKind::Literal
703+
| NonterminalKind::Path
704+
| NonterminalKind::Block
705+
)
706+
)
749707
}
750708

751-
/// Is the token an interpolated block (`$b:block`)?
752-
pub fn is_whole_block(&self) -> bool {
753-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
754-
if let Interpolated(nt) = &self.kind
755-
&& let NtBlock(..) = &**nt
756-
{
757-
return true;
758-
}
759-
760-
false
709+
/// Are we at a block from a metavar (`$b:block`)?
710+
pub fn is_metavar_block(&self) -> bool {
711+
matches!(self.is_metavar_seq(), Some(NonterminalKind::Block))
761712
}
762713

763714
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -782,7 +733,8 @@ impl Token {
782733
self.is_non_raw_ident_where(|id| id.name == kw)
783734
}
784735

785-
/// 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.
736+
/// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
737+
/// token is an identifier equal to `kw` ignoring the case.
786738
pub fn is_keyword_case(&self, kw: Symbol, case: Case) -> bool {
787739
self.is_keyword(kw)
788740
|| (case == Case::Insensitive
@@ -903,7 +855,7 @@ impl Token {
903855
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
904856
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
905857
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
906-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
858+
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof => {
907859
return None;
908860
}
909861
};
@@ -919,12 +871,6 @@ impl PartialEq<TokenKind> for Token {
919871
}
920872
}
921873

922-
#[derive(Clone, Encodable, Decodable)]
923-
/// For interpolation during macro expansion.
924-
pub enum Nonterminal {
925-
NtBlock(P<ast::Block>),
926-
}
927-
928874
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
929875
pub enum NonterminalKind {
930876
Item,
@@ -1003,47 +949,6 @@ impl fmt::Display for NonterminalKind {
1003949
}
1004950
}
1005951

1006-
impl Nonterminal {
1007-
pub fn use_span(&self) -> Span {
1008-
match self {
1009-
NtBlock(block) => block.span,
1010-
}
1011-
}
1012-
1013-
pub fn descr(&self) -> &'static str {
1014-
match self {
1015-
NtBlock(..) => "block",
1016-
}
1017-
}
1018-
}
1019-
1020-
impl PartialEq for Nonterminal {
1021-
fn eq(&self, _rhs: &Self) -> bool {
1022-
// FIXME: Assume that all nonterminals are not equal, we can't compare them
1023-
// correctly based on data from AST. This will prevent them from matching each other
1024-
// in macros. The comparison will become possible only when each nonterminal has an
1025-
// attached token stream from which it was parsed.
1026-
false
1027-
}
1028-
}
1029-
1030-
impl fmt::Debug for Nonterminal {
1031-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1032-
match *self {
1033-
NtBlock(..) => f.pad("NtBlock(..)"),
1034-
}
1035-
}
1036-
}
1037-
1038-
impl<CTX> HashStable<CTX> for Nonterminal
1039-
where
1040-
CTX: crate::HashStableContext,
1041-
{
1042-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
1043-
panic!("interpolated tokens should not be present in the HIR")
1044-
}
1045-
}
1046-
1047952
// Some types are used a lot. Make sure they don't unintentionally get bigger.
1048953
#[cfg(target_pointer_width = "64")]
1049954
mod size_asserts {
@@ -1052,7 +957,6 @@ mod size_asserts {
1052957
// tidy-alphabetical-start
1053958
static_assert_size!(Lit, 12);
1054959
static_assert_size!(LitKind, 2);
1055-
static_assert_size!(Nonterminal, 8);
1056960
static_assert_size!(Token, 24);
1057961
static_assert_size!(TokenKind, 16);
1058962
// tidy-alphabetical-end

compiler/rustc_ast/src/tokenstream.rs

+4-17
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};
@@ -474,12 +474,6 @@ impl TokenStream {
474474
attr_stream.to_tokenstream()
475475
}
476476

477-
pub fn from_nonterminal_ast(nt: &Nonterminal) -> TokenStream {
478-
match nt {
479-
Nonterminal::NtBlock(block) => TokenStream::from_ast(block),
480-
}
481-
}
482-
483477
fn flatten_token(token: &Token, spacing: Spacing) -> TokenTree {
484478
match token.kind {
485479
token::NtIdent(ident, is_raw) => {
@@ -491,12 +485,6 @@ impl TokenStream {
491485
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
492486
TokenStream::token_alone(token::Lifetime(ident.name), ident.span),
493487
),
494-
token::Interpolated(ref nt) => TokenTree::Delimited(
495-
DelimSpan::from_single(token.span),
496-
DelimSpacing::new(Spacing::JointHidden, spacing),
497-
Delimiter::Invisible(InvisibleOrigin::FlattenToken),
498-
TokenStream::from_nonterminal_ast(&nt).flattened(),
499-
),
500488
_ => TokenTree::Token(token.clone(), spacing),
501489
}
502490
}
@@ -514,10 +502,9 @@ impl TokenStream {
514502
pub fn flattened(&self) -> TokenStream {
515503
fn can_skip(stream: &TokenStream) -> bool {
516504
stream.trees().all(|tree| match tree {
517-
TokenTree::Token(token, _) => !matches!(
518-
token.kind,
519-
token::NtIdent(..) | token::NtLifetime(..) | token::Interpolated(..)
520-
),
505+
TokenTree::Token(token, _) => {
506+
!matches!(token.kind, token::NtIdent(..) | token::NtLifetime(..))
507+
}
521508
TokenTree::Delimited(.., inner) => can_skip(inner),
522509
})
523510
}

compiler/rustc_ast_pretty/src/pprust/mod.rs

+1-5
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)