11use std:: borrow:: Cow ;
22use std:: fmt;
3- use std:: sync:: Arc ;
43
54pub use BinOpToken :: * ;
65pub use LitKind :: * ;
7- pub use Nonterminal :: * ;
86pub use NtExprKind :: * ;
97pub use NtPatKind :: * ;
108pub use TokenKind :: * ;
11- use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
129use rustc_macros:: { Decodable , Encodable , HashStable_Generic } ;
1310use rustc_span:: edition:: Edition ;
1411use rustc_span:: { DUMMY_SP , ErrorGuaranteed , Span , kw, sym} ;
@@ -17,7 +14,6 @@ use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span, kw, sym};
1714use rustc_span:: { Ident , Symbol } ;
1815
1916use crate :: ast;
20- use crate :: ptr:: P ;
2117use 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
@@ -353,9 +349,7 @@ impl From<IdentIsRaw> for bool {
353349 }
354350}
355351
356- // SAFETY: due to the `Clone` impl below, all fields of all variants other than
357- // `Interpolated` must impl `Copy`.
358- #[ derive( PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
352+ #[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
359353pub enum TokenKind {
360354 /* Expression-operator symbols. */
361355 /// `=`
@@ -444,21 +438,6 @@ pub enum TokenKind {
444438 /// the `lifetime` metavariable in the macro's RHS.
445439 NtLifetime ( Ident , IdentIsRaw ) ,
446440
447- /// An embedded AST node, as produced by a macro. This only exists for
448- /// historical reasons. We'd like to get rid of it, for multiple reasons.
449- /// - It's conceptually very strange. Saying a token can contain an AST
450- /// node is like saying, in natural language, that a word can contain a
451- /// sentence.
452- /// - It requires special handling in a bunch of places in the parser.
453- /// - It prevents `Token` from implementing `Copy`.
454- /// It adds complexity and likely slows things down. Please don't add new
455- /// occurrences of this token kind!
456- ///
457- /// The span in the surrounding `Token` is that of the metavariable in the
458- /// macro's RHS. The span within the Nonterminal is that of the fragment
459- /// passed to the macro at the call site.
460- Interpolated ( Arc < Nonterminal > ) ,
461-
462441 /// A doc comment token.
463442 /// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
464443 /// similarly to symbols in string literal tokens.
@@ -468,19 +447,6 @@ pub enum TokenKind {
468447 Eof ,
469448}
470449
471- impl Clone for TokenKind {
472- fn clone ( & self ) -> Self {
473- // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
474- // for all other variants, this implementation of `clone` is just like
475- // a copy. This is faster than the `derive(Clone)` version which has a
476- // separate path for every variant.
477- match self {
478- Interpolated ( nt) => Interpolated ( Arc :: clone ( nt) ) ,
479- _ => unsafe { std:: ptr:: read ( self ) } ,
480- }
481- }
482- }
483-
484450#[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
485451pub struct Token {
486452 pub kind : TokenKind ,
@@ -575,7 +541,6 @@ impl Token {
575541 pub fn uninterpolated_span ( & self ) -> Span {
576542 match self . kind {
577543 NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
578- Interpolated ( ref nt) => nt. use_span ( ) ,
579544 _ => self . span ,
580545 }
581546 }
@@ -593,7 +558,7 @@ impl Token {
593558 }
594559
595560 OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
596- | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | Eof => false ,
561+ | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Eof => false ,
597562 }
598563 }
599564
@@ -624,7 +589,6 @@ impl Token {
624589 PathSep | // global path
625590 Lifetime ( ..) | // labeled loop
626591 Pound => true , // expression attributes
627- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
628592 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
629593 MetaVarKind :: Block |
630594 MetaVarKind :: Expr { .. } |
@@ -697,7 +661,6 @@ impl Token {
697661 match self . kind {
698662 OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
699663 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
700- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
701664 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
702665 MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
703666 ) ) ) => true ,
@@ -825,31 +788,20 @@ impl Token {
825788 /// Is this a pre-parsed expression dropped into the token stream
826789 /// (which happens while parsing the result of macro expansion)?
827790 pub fn is_metavar_expr ( & self ) -> bool {
828- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
829- if let Interpolated ( nt) = & self . kind
830- && let NtBlock ( _) = & * * nt
831- {
832- true
833- } else if matches ! (
791+ matches ! (
834792 self . is_metavar_seq( ) ,
835- Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
836- ) {
837- true
838- } else {
839- false
840- }
793+ Some (
794+ MetaVarKind :: Expr { .. }
795+ | MetaVarKind :: Literal
796+ | MetaVarKind :: Path
797+ | MetaVarKind :: Block
798+ )
799+ )
841800 }
842801
843- /// Is the token an interpolated block (`$b:block`)?
844- pub fn is_whole_block ( & self ) -> bool {
845- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
846- if let Interpolated ( nt) = & self . kind
847- && let NtBlock ( ..) = & * * nt
848- {
849- return true ;
850- }
851-
852- false
802+ /// Are we at a block from a metavar (`$b:block`)?
803+ pub fn is_metavar_block ( & self ) -> bool {
804+ matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Block ) )
853805 }
854806
855807 /// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -1002,7 +954,7 @@ impl Token {
1002954 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
1003955 | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1004956 | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1005- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | DocComment ( ..) | Eof => {
957+ | Lifetime ( ..) | NtLifetime ( ..) | DocComment ( ..) | Eof => {
1006958 return None ;
1007959 }
1008960 } ;
@@ -1039,12 +991,6 @@ pub enum NtExprKind {
1039991 Expr2021 { inferred : bool } ,
1040992}
1041993
1042- #[ derive( Clone , Encodable , Decodable ) ]
1043- /// For interpolation during macro expansion.
1044- pub enum Nonterminal {
1045- NtBlock ( P < ast:: Block > ) ,
1046- }
1047-
1048994#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
1049995pub enum NonterminalKind {
1050996 Item ,
@@ -1128,47 +1074,6 @@ impl fmt::Display for NonterminalKind {
11281074 }
11291075}
11301076
1131- impl Nonterminal {
1132- pub fn use_span ( & self ) -> Span {
1133- match self {
1134- NtBlock ( block) => block. span ,
1135- }
1136- }
1137-
1138- pub fn descr ( & self ) -> & ' static str {
1139- match self {
1140- NtBlock ( ..) => "block" ,
1141- }
1142- }
1143- }
1144-
1145- impl PartialEq for Nonterminal {
1146- fn eq ( & self , _rhs : & Self ) -> bool {
1147- // FIXME: Assume that all nonterminals are not equal, we can't compare them
1148- // correctly based on data from AST. This will prevent them from matching each other
1149- // in macros. The comparison will become possible only when each nonterminal has an
1150- // attached token stream from which it was parsed.
1151- false
1152- }
1153- }
1154-
1155- impl fmt:: Debug for Nonterminal {
1156- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1157- match * self {
1158- NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1159- }
1160- }
1161- }
1162-
1163- impl < CTX > HashStable < CTX > for Nonterminal
1164- where
1165- CTX : crate :: HashStableContext ,
1166- {
1167- fn hash_stable ( & self , _hcx : & mut CTX , _hasher : & mut StableHasher ) {
1168- panic ! ( "interpolated tokens should not be present in the HIR" )
1169- }
1170- }
1171-
11721077// Some types are used a lot. Make sure they don't unintentionally get bigger.
11731078#[ cfg( target_pointer_width = "64" ) ]
11741079mod size_asserts {
@@ -1178,7 +1083,6 @@ mod size_asserts {
11781083 // tidy-alphabetical-start
11791084 static_assert_size ! ( Lit , 12 ) ;
11801085 static_assert_size ! ( LitKind , 2 ) ;
1181- static_assert_size ! ( Nonterminal , 8 ) ;
11821086 static_assert_size ! ( Token , 24 ) ;
11831087 static_assert_size ! ( TokenKind , 16 ) ;
11841088 // tidy-alphabetical-end
0 commit comments