Skip to content

Commit df3277b

Browse files
committed
Auto merge of rust-lang#124141 - nnethercote:rm-Nonterminal-and-TokenKind-Interpolated, r=<try>
Remove `Nonterminal` and `TokenKind::Interpolated` A third attempt at this; the first attempt was rust-lang#96724 and the second was rust-lang#114647. r? `@ghost`
2 parents 70dab5a + 1830245 commit df3277b

File tree

61 files changed

+164
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+164
-532
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,
@@ -206,19 +205,6 @@ impl HasTokens for Attribute {
206205
}
207206
}
208207

209-
impl HasTokens for Nonterminal {
210-
fn tokens(&self) -> Option<&LazyAttrTokenStream> {
211-
match self {
212-
Nonterminal::NtBlock(block) => block.tokens(),
213-
}
214-
}
215-
fn tokens_mut(&mut self) -> Option<&mut Option<LazyAttrTokenStream>> {
216-
match self {
217-
Nonterminal::NtBlock(block) => block.tokens_mut(),
218-
}
219-
}
220-
}
221-
222208
/// A trait for AST nodes having (or not having) attributes.
223209
pub trait HasAttrs {
224210
/// This is `true` if this `HasAttrs` might support 'custom' (proc-macro) inner

compiler/rustc_ast/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
// tidy-alphabetical-start
88
#![allow(internal_features)]
9-
#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
109
#![doc(
1110
html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/",
1211
test(attr(deny(warnings)))

compiler/rustc_ast/src/mut_visit.rs

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

846-
/// Applies ident visitor if it's an ident; applies other visits to interpolated nodes.
847-
/// In practice the ident part is not actually used by specific visitors right now,
848-
/// but there's a test below checking that it works.
846+
/// Applies ident visitor if it's an ident. In practice this is not actually
847+
/// used by specific visitors right now, but there's a test below checking that
848+
/// it works.
849849
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
850850
pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
851851
let Token { kind, span } = t;
@@ -863,45 +863,11 @@ pub fn visit_token<T: MutVisitor>(vis: &mut T, t: &mut Token) {
863863
token::NtLifetime(ident, _is_raw) => {
864864
vis.visit_ident(ident);
865865
}
866-
token::Interpolated(nt) => {
867-
let nt = Arc::make_mut(nt);
868-
visit_nonterminal(vis, nt);
869-
}
870866
_ => {}
871867
}
872868
vis.visit_span(span);
873869
}
874870

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

compiler/rustc_ast/src/token.rs

+16-117
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::borrow::Cow;
22
use std::fmt;
3-
use std::sync::Arc;
43

54
pub use LitKind::*;
6-
pub use Nonterminal::*;
75
pub use NtExprKind::*;
86
pub use NtPatKind::*;
97
pub use TokenKind::*;
10-
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
118
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
129
use rustc_span::edition::Edition;
1310
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
@@ -16,7 +13,6 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
1613
use rustc_span::{Ident, Symbol};
1714

1815
use crate::ast;
19-
use crate::ptr::P;
2016
use crate::util::case::Case;
2117

2218
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
@@ -34,10 +30,6 @@ pub enum InvisibleOrigin {
3430
// Converted from `proc_macro::Delimiter` in
3531
// `proc_macro::Delimiter::to_internal`, i.e. returned by a proc macro.
3632
ProcMacro,
37-
38-
// Converted from `TokenKind::Interpolated` in
39-
// `TokenStream::flatten_token`. Treated similarly to `ProcMacro`.
40-
FlattenToken,
4133
}
4234

4335
impl PartialEq for InvisibleOrigin {
@@ -134,9 +126,7 @@ impl Delimiter {
134126
match self {
135127
Delimiter::Parenthesis | Delimiter::Bracket | Delimiter::Brace => false,
136128
Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) => false,
137-
Delimiter::Invisible(InvisibleOrigin::FlattenToken | InvisibleOrigin::ProcMacro) => {
138-
true
139-
}
129+
Delimiter::Invisible(InvisibleOrigin::ProcMacro) => true,
140130
}
141131
}
142132

@@ -337,9 +327,7 @@ impl From<IdentIsRaw> for bool {
337327
}
338328
}
339329

340-
// SAFETY: due to the `Clone` impl below, all fields of all variants other than
341-
// `Interpolated` must impl `Copy`.
342-
#[derive(PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
330+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
343331
pub enum TokenKind {
344332
/* Expression-operator symbols. */
345333
/// `=`
@@ -468,21 +456,6 @@ pub enum TokenKind {
468456
/// the `lifetime` metavariable in the macro's RHS.
469457
NtLifetime(Ident, IdentIsRaw),
470458

471-
/// An embedded AST node, as produced by a macro. This only exists for
472-
/// historical reasons. We'd like to get rid of it, for multiple reasons.
473-
/// - It's conceptually very strange. Saying a token can contain an AST
474-
/// node is like saying, in natural language, that a word can contain a
475-
/// sentence.
476-
/// - It requires special handling in a bunch of places in the parser.
477-
/// - It prevents `Token` from implementing `Copy`.
478-
/// It adds complexity and likely slows things down. Please don't add new
479-
/// occurrences of this token kind!
480-
///
481-
/// The span in the surrounding `Token` is that of the metavariable in the
482-
/// macro's RHS. The span within the Nonterminal is that of the fragment
483-
/// passed to the macro at the call site.
484-
Interpolated(Arc<Nonterminal>),
485-
486459
/// A doc comment token.
487460
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
488461
/// similarly to symbols in string literal tokens.
@@ -492,20 +465,7 @@ pub enum TokenKind {
492465
Eof,
493466
}
494467

495-
impl Clone for TokenKind {
496-
fn clone(&self) -> Self {
497-
// `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
498-
// for all other variants, this implementation of `clone` is just like
499-
// a copy. This is faster than the `derive(Clone)` version which has a
500-
// separate path for every variant.
501-
match self {
502-
Interpolated(nt) => Interpolated(Arc::clone(nt)),
503-
_ => unsafe { std::ptr::read(self) },
504-
}
505-
}
506-
}
507-
508-
#[derive(Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
468+
#[derive(Clone, Copy, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
509469
pub struct Token {
510470
pub kind: TokenKind,
511471
pub span: Span,
@@ -600,7 +560,7 @@ impl Token {
600560
| FatArrow | Pound | Dollar | Question | SingleQuote => true,
601561

602562
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
603-
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
563+
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
604564
}
605565
}
606566

@@ -631,7 +591,6 @@ impl Token {
631591
PathSep | // global path
632592
Lifetime(..) | // labeled loop
633593
Pound => true, // expression attributes
634-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
635594
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
636595
MetaVarKind::Block |
637596
MetaVarKind::Expr { .. } |
@@ -703,7 +662,6 @@ impl Token {
703662
match self.kind {
704663
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
705664
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
706-
Interpolated(ref nt) => matches!(&**nt, NtBlock(..)),
707665
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
708666
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
709667
))) => true,
@@ -831,31 +789,20 @@ impl Token {
831789
/// Is this a pre-parsed expression dropped into the token stream
832790
/// (which happens while parsing the result of macro expansion)?
833791
pub fn is_metavar_expr(&self) -> bool {
834-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
835-
if let Interpolated(nt) = &self.kind
836-
&& let NtBlock(_) = &**nt
837-
{
838-
true
839-
} else if matches!(
792+
matches!(
840793
self.is_metavar_seq(),
841-
Some(MetaVarKind::Expr { .. } | MetaVarKind::Literal | MetaVarKind::Path)
842-
) {
843-
true
844-
} else {
845-
matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
846-
}
794+
Some(
795+
MetaVarKind::Expr { .. }
796+
| MetaVarKind::Literal
797+
| MetaVarKind::Path
798+
| MetaVarKind::Block
799+
)
800+
)
847801
}
848802

849-
/// Is the token an interpolated block (`$b:block`)?
850-
pub fn is_whole_block(&self) -> bool {
851-
#[allow(irrefutable_let_patterns)] // FIXME: temporary
852-
if let Interpolated(nt) = &self.kind
853-
&& let NtBlock(..) = &**nt
854-
{
855-
return true;
856-
}
857-
858-
false
803+
/// Are we at a block from a metavar (`$b:block`)?
804+
pub fn is_metavar_block(&self) -> bool {
805+
matches!(self.is_metavar_seq(), Some(MetaVarKind::Block))
859806
}
860807

861808
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -1024,7 +971,7 @@ impl Token {
1024971
| PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
1025972
| Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
1026973
| OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
1027-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof,
974+
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof,
1028975
_,
1029976
) => {
1030977
return None;
@@ -1063,12 +1010,6 @@ pub enum NtExprKind {
10631010
Expr2021 { inferred: bool },
10641011
}
10651012

1066-
#[derive(Clone, Encodable, Decodable)]
1067-
/// For interpolation during macro expansion.
1068-
pub enum Nonterminal {
1069-
NtBlock(P<ast::Block>),
1070-
}
1071-
10721013
#[derive(Debug, Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, HashStable_Generic)]
10731014
pub enum NonterminalKind {
10741015
Item,
@@ -1152,47 +1093,6 @@ impl fmt::Display for NonterminalKind {
11521093
}
11531094
}
11541095

1155-
impl Nonterminal {
1156-
pub fn use_span(&self) -> Span {
1157-
match self {
1158-
NtBlock(block) => block.span,
1159-
}
1160-
}
1161-
1162-
pub fn descr(&self) -> &'static str {
1163-
match self {
1164-
NtBlock(..) => "block",
1165-
}
1166-
}
1167-
}
1168-
1169-
impl PartialEq for Nonterminal {
1170-
fn eq(&self, _rhs: &Self) -> bool {
1171-
// FIXME: Assume that all nonterminals are not equal, we can't compare them
1172-
// correctly based on data from AST. This will prevent them from matching each other
1173-
// in macros. The comparison will become possible only when each nonterminal has an
1174-
// attached token stream from which it was parsed.
1175-
false
1176-
}
1177-
}
1178-
1179-
impl fmt::Debug for Nonterminal {
1180-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1181-
match *self {
1182-
NtBlock(..) => f.pad("NtBlock(..)"),
1183-
}
1184-
}
1185-
}
1186-
1187-
impl<CTX> HashStable<CTX> for Nonterminal
1188-
where
1189-
CTX: crate::HashStableContext,
1190-
{
1191-
fn hash_stable(&self, _hcx: &mut CTX, _hasher: &mut StableHasher) {
1192-
panic!("interpolated tokens should not be present in the HIR")
1193-
}
1194-
}
1195-
11961096
// Some types are used a lot. Make sure they don't unintentionally get bigger.
11971097
#[cfg(target_pointer_width = "64")]
11981098
mod size_asserts {
@@ -1202,7 +1102,6 @@ mod size_asserts {
12021102
// tidy-alphabetical-start
12031103
static_assert_size!(Lit, 12);
12041104
static_assert_size!(LitKind, 2);
1205-
static_assert_size!(Nonterminal, 8);
12061105
static_assert_size!(Token, 24);
12071106
static_assert_size!(TokenKind, 16);
12081107
// tidy-alphabetical-end

0 commit comments

Comments
 (0)