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
@@ -350,9 +346,7 @@ impl From<IdentIsRaw> for bool {
350346 }
351347}
352348
353- // SAFETY: due to the `Clone` impl below, all fields of all variants other than
354- // `Interpolated` must impl `Copy`.
355- #[ derive( PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
349+ #[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
356350pub enum TokenKind {
357351 /* Expression-operator symbols. */
358352 /// `=`
@@ -441,21 +435,6 @@ pub enum TokenKind {
441435 /// the `lifetime` metavariable in the macro's RHS.
442436 NtLifetime ( Ident , IdentIsRaw ) ,
443437
444- /// An embedded AST node, as produced by a macro. This only exists for
445- /// historical reasons. We'd like to get rid of it, for multiple reasons.
446- /// - It's conceptually very strange. Saying a token can contain an AST
447- /// node is like saying, in natural language, that a word can contain a
448- /// sentence.
449- /// - It requires special handling in a bunch of places in the parser.
450- /// - It prevents `Token` from implementing `Copy`.
451- /// It adds complexity and likely slows things down. Please don't add new
452- /// occurrences of this token kind!
453- ///
454- /// The span in the surrounding `Token` is that of the metavariable in the
455- /// macro's RHS. The span within the Nonterminal is that of the fragment
456- /// passed to the macro at the call site.
457- Interpolated ( Arc < Nonterminal > ) ,
458-
459438 /// A doc comment token.
460439 /// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
461440 /// similarly to symbols in string literal tokens.
@@ -465,19 +444,6 @@ pub enum TokenKind {
465444 Eof ,
466445}
467446
468- impl Clone for TokenKind {
469- fn clone ( & self ) -> Self {
470- // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
471- // for all other variants, this implementation of `clone` is just like
472- // a copy. This is faster than the `derive(Clone)` version which has a
473- // separate path for every variant.
474- match self {
475- Interpolated ( nt) => Interpolated ( Arc :: clone ( nt) ) ,
476- _ => unsafe { std:: ptr:: read ( self ) } ,
477- }
478- }
479- }
480-
481447#[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
482448pub struct Token {
483449 pub kind : TokenKind ,
@@ -572,7 +538,6 @@ impl Token {
572538 pub fn uninterpolated_span ( & self ) -> Span {
573539 match self . kind {
574540 NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
575- Interpolated ( ref nt) => nt. use_span ( ) ,
576541 _ => self . span ,
577542 }
578543 }
@@ -590,7 +555,7 @@ impl Token {
590555 }
591556
592557 OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
593- | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | Eof => false ,
558+ | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Eof => false ,
594559 }
595560 }
596561
@@ -621,7 +586,6 @@ impl Token {
621586 PathSep | // global path
622587 Lifetime ( ..) | // labeled loop
623588 Pound => true , // expression attributes
624- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
625589 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
626590 MetaVarKind :: Block |
627591 MetaVarKind :: Expr { .. } |
@@ -694,7 +658,6 @@ impl Token {
694658 match self . kind {
695659 OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
696660 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
697- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
698661 OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
699662 MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
700663 ) ) ) => true ,
@@ -822,31 +785,20 @@ impl Token {
822785 /// Is this a pre-parsed expression dropped into the token stream
823786 /// (which happens while parsing the result of macro expansion)?
824787 pub fn is_metavar_expr ( & self ) -> bool {
825- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
826- if let Interpolated ( nt) = & self . kind
827- && let NtBlock ( _) = & * * nt
828- {
829- true
830- } else if matches ! (
788+ matches ! (
831789 self . is_metavar_seq( ) ,
832- Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
833- ) {
834- true
835- } else {
836- false
837- }
790+ Some (
791+ MetaVarKind :: Expr { .. }
792+ | MetaVarKind :: Literal
793+ | MetaVarKind :: Path
794+ | MetaVarKind :: Block
795+ )
796+ )
838797 }
839798
840- /// Is the token an interpolated block (`$b:block`)?
841- pub fn is_whole_block ( & self ) -> bool {
842- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
843- if let Interpolated ( nt) = & self . kind
844- && let NtBlock ( ..) = & * * nt
845- {
846- return true ;
847- }
848-
849- false
799+ /// Are we at a block from a metavar (`$b:block`)?
800+ pub fn is_metavar_block ( & self ) -> bool {
801+ matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Block ) )
850802 }
851803
852804 /// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -999,7 +951,7 @@ impl Token {
999951 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
1000952 | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1001953 | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
1002- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | DocComment ( ..) | Eof => {
954+ | Lifetime ( ..) | NtLifetime ( ..) | DocComment ( ..) | Eof => {
1003955 return None ;
1004956 }
1005957 } ;
@@ -1036,12 +988,6 @@ pub enum NtExprKind {
1036988 Expr2021 { inferred : bool } ,
1037989}
1038990
1039- #[ derive( Clone , Encodable , Decodable ) ]
1040- /// For interpolation during macro expansion.
1041- pub enum Nonterminal {
1042- NtBlock ( P < ast:: Block > ) ,
1043- }
1044-
1045991#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
1046992pub enum NonterminalKind {
1047993 Item ,
@@ -1125,47 +1071,6 @@ impl fmt::Display for NonterminalKind {
11251071 }
11261072}
11271073
1128- impl Nonterminal {
1129- pub fn use_span ( & self ) -> Span {
1130- match self {
1131- NtBlock ( block) => block. span ,
1132- }
1133- }
1134-
1135- pub fn descr ( & self ) -> & ' static str {
1136- match self {
1137- NtBlock ( ..) => "block" ,
1138- }
1139- }
1140- }
1141-
1142- impl PartialEq for Nonterminal {
1143- fn eq ( & self , _rhs : & Self ) -> bool {
1144- // FIXME: Assume that all nonterminals are not equal, we can't compare them
1145- // correctly based on data from AST. This will prevent them from matching each other
1146- // in macros. The comparison will become possible only when each nonterminal has an
1147- // attached token stream from which it was parsed.
1148- false
1149- }
1150- }
1151-
1152- impl fmt:: Debug for Nonterminal {
1153- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1154- match * self {
1155- NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1156- }
1157- }
1158- }
1159-
1160- impl < CTX > HashStable < CTX > for Nonterminal
1161- where
1162- CTX : crate :: HashStableContext ,
1163- {
1164- fn hash_stable ( & self , _hcx : & mut CTX , _hasher : & mut StableHasher ) {
1165- panic ! ( "interpolated tokens should not be present in the HIR" )
1166- }
1167- }
1168-
11691074// Some types are used a lot. Make sure they don't unintentionally get bigger.
11701075#[ cfg( target_pointer_width = "64" ) ]
11711076mod size_asserts {
@@ -1175,7 +1080,6 @@ mod size_asserts {
11751080 // tidy-alphabetical-start
11761081 static_assert_size ! ( Lit , 12 ) ;
11771082 static_assert_size ! ( LitKind , 2 ) ;
1178- static_assert_size ! ( Nonterminal , 8 ) ;
11791083 static_assert_size ! ( Token , 24 ) ;
11801084 static_assert_size ! ( TokenKind , 16 ) ;
11811085 // tidy-alphabetical-end
0 commit comments