1
1
use std:: borrow:: Cow ;
2
2
use std:: fmt;
3
3
4
- use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
5
- use rustc_data_structures:: sync:: Lrc ;
6
4
use rustc_macros:: { Decodable , Encodable , HashStable_Generic } ;
7
5
use rustc_span:: edition:: Edition ;
8
6
use rustc_span:: symbol:: { kw, sym} ;
@@ -12,13 +10,11 @@ use rustc_span::symbol::{Ident, Symbol};
12
10
use rustc_span:: { ErrorGuaranteed , Span , DUMMY_SP } ;
13
11
pub use BinOpToken :: * ;
14
12
pub use LitKind :: * ;
15
- pub use Nonterminal :: * ;
16
13
pub use NtExprKind :: * ;
17
14
pub use NtPatKind :: * ;
18
15
pub use TokenKind :: * ;
19
16
20
17
use crate :: ast;
21
- use crate :: ptr:: P ;
22
18
use crate :: util:: case:: Case ;
23
19
24
20
#[ derive( Clone , Copy , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
@@ -52,8 +48,8 @@ pub enum InvisibleOrigin {
52
48
// `proc_macro::Delimiter::to_internal`, i.e. returned by a proc macro.
53
49
ProcMacro ,
54
50
55
- // Converted from `TokenKind::Interpolated ` in
56
- // `TokenStream::flatten_token`. Treated similarly to `ProcMacro`.
51
+ // Converted from `TokenKind::NtLifetime ` in `TokenStream::flatten_token`.
52
+ // Treated similarly to `ProcMacro`.
57
53
FlattenToken ,
58
54
}
59
55
@@ -349,9 +345,7 @@ impl From<IdentIsRaw> for bool {
349
345
}
350
346
}
351
347
352
- // SAFETY: due to the `Clone` impl below, all fields of all variants other than
353
- // `Interpolated` must impl `Copy`.
354
- #[ derive( PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
348
+ #[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
355
349
pub enum TokenKind {
356
350
/* Expression-operator symbols. */
357
351
/// `=`
@@ -440,21 +434,6 @@ pub enum TokenKind {
440
434
/// the `lifetime` metavariable in the macro's RHS.
441
435
NtLifetime ( Ident , IdentIsRaw ) ,
442
436
443
- /// An embedded AST node, as produced by a macro. This only exists for
444
- /// historical reasons. We'd like to get rid of it, for multiple reasons.
445
- /// - It's conceptually very strange. Saying a token can contain an AST
446
- /// node is like saying, in natural language, that a word can contain a
447
- /// sentence.
448
- /// - It requires special handling in a bunch of places in the parser.
449
- /// - It prevents `Token` from implementing `Copy`.
450
- /// It adds complexity and likely slows things down. Please don't add new
451
- /// occurrences of this token kind!
452
- ///
453
- /// The span in the surrounding `Token` is that of the metavariable in the
454
- /// macro's RHS. The span within the Nonterminal is that of the fragment
455
- /// passed to the macro at the call site.
456
- Interpolated ( Lrc < Nonterminal > ) ,
457
-
458
437
/// A doc comment token.
459
438
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
460
439
/// similarly to symbols in string literal tokens.
@@ -464,19 +443,6 @@ pub enum TokenKind {
464
443
Eof ,
465
444
}
466
445
467
- impl Clone for TokenKind {
468
- fn clone ( & self ) -> Self {
469
- // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
470
- // for all other variants, this implementation of `clone` is just like
471
- // a copy. This is faster than the `derive(Clone)` version which has a
472
- // separate path for every variant.
473
- match self {
474
- Interpolated ( nt) => Interpolated ( nt. clone ( ) ) ,
475
- _ => unsafe { std:: ptr:: read ( self ) } ,
476
- }
477
- }
478
- }
479
-
480
446
#[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
481
447
pub struct Token {
482
448
pub kind : TokenKind ,
@@ -565,7 +531,6 @@ impl Token {
565
531
pub fn uninterpolated_span ( & self ) -> Span {
566
532
match self . kind {
567
533
NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
568
- Interpolated ( ref nt) => nt. use_span ( ) ,
569
534
_ => self . span ,
570
535
}
571
536
}
@@ -583,7 +548,7 @@ impl Token {
583
548
}
584
549
585
550
OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
586
- | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | Eof => false ,
551
+ | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Eof => false ,
587
552
}
588
553
}
589
554
@@ -614,7 +579,6 @@ impl Token {
614
579
PathSep | // global path
615
580
Lifetime ( ..) | // labeled loop
616
581
Pound => true , // expression attributes
617
- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
618
582
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
619
583
MetaVarKind :: Block |
620
584
MetaVarKind :: Expr { .. } |
@@ -687,7 +651,6 @@ impl Token {
687
651
match self . kind {
688
652
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
689
653
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
690
- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
691
654
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
692
655
MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
693
656
) ) ) => true ,
@@ -815,31 +778,20 @@ impl Token {
815
778
/// Is this a pre-parsed expression dropped into the token stream
816
779
/// (which happens while parsing the result of macro expansion)?
817
780
pub fn is_metavar_expr ( & self ) -> bool {
818
- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
819
- if let Interpolated ( nt) = & self . kind
820
- && let NtBlock ( _) = & * * nt
821
- {
822
- true
823
- } else if matches ! (
781
+ matches ! (
824
782
self . is_metavar_seq( ) ,
825
- Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
826
- ) {
827
- true
828
- } else {
829
- false
830
- }
783
+ Some (
784
+ MetaVarKind :: Expr { .. }
785
+ | MetaVarKind :: Literal
786
+ | MetaVarKind :: Path
787
+ | MetaVarKind :: Block
788
+ )
789
+ )
831
790
}
832
791
833
- /// Is the token an interpolated block (`$b:block`)?
834
- pub fn is_whole_block ( & self ) -> bool {
835
- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
836
- if let Interpolated ( nt) = & self . kind
837
- && let NtBlock ( ..) = & * * nt
838
- {
839
- return true ;
840
- }
841
-
842
- false
792
+ /// Are we at a block from a metavar (`$b:block`)?
793
+ pub fn is_metavar_block ( & self ) -> bool {
794
+ matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Block ) )
843
795
}
844
796
845
797
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -864,7 +816,8 @@ impl Token {
864
816
self . is_non_raw_ident_where ( |id| id. name == kw)
865
817
}
866
818
867
- /// 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.
819
+ /// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
820
+ /// token is an identifier equal to `kw` ignoring the case.
868
821
pub fn is_keyword_case ( & self , kw : Symbol , case : Case ) -> bool {
869
822
self . is_keyword ( kw)
870
823
|| ( case == Case :: Insensitive
@@ -985,7 +938,7 @@ impl Token {
985
938
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
986
939
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
987
940
| Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
988
- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | DocComment ( ..) | Eof => {
941
+ | Lifetime ( ..) | NtLifetime ( ..) | DocComment ( ..) | Eof => {
989
942
return None ;
990
943
}
991
944
} ;
@@ -1022,12 +975,6 @@ pub enum NtExprKind {
1022
975
Expr2021 { inferred : bool } ,
1023
976
}
1024
977
1025
- #[ derive( Clone , Encodable , Decodable ) ]
1026
- /// For interpolation during macro expansion.
1027
- pub enum Nonterminal {
1028
- NtBlock ( P < ast:: Block > ) ,
1029
- }
1030
-
1031
978
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
1032
979
pub enum NonterminalKind {
1033
980
Item ,
@@ -1111,47 +1058,6 @@ impl fmt::Display for NonterminalKind {
1111
1058
}
1112
1059
}
1113
1060
1114
- impl Nonterminal {
1115
- pub fn use_span ( & self ) -> Span {
1116
- match self {
1117
- NtBlock ( block) => block. span ,
1118
- }
1119
- }
1120
-
1121
- pub fn descr ( & self ) -> & ' static str {
1122
- match self {
1123
- NtBlock ( ..) => "block" ,
1124
- }
1125
- }
1126
- }
1127
-
1128
- impl PartialEq for Nonterminal {
1129
- fn eq ( & self , _rhs : & Self ) -> bool {
1130
- // FIXME: Assume that all nonterminals are not equal, we can't compare them
1131
- // correctly based on data from AST. This will prevent them from matching each other
1132
- // in macros. The comparison will become possible only when each nonterminal has an
1133
- // attached token stream from which it was parsed.
1134
- false
1135
- }
1136
- }
1137
-
1138
- impl fmt:: Debug for Nonterminal {
1139
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1140
- match * self {
1141
- NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1142
- }
1143
- }
1144
- }
1145
-
1146
- impl < CTX > HashStable < CTX > for Nonterminal
1147
- where
1148
- CTX : crate :: HashStableContext ,
1149
- {
1150
- fn hash_stable ( & self , _hcx : & mut CTX , _hasher : & mut StableHasher ) {
1151
- panic ! ( "interpolated tokens should not be present in the HIR" )
1152
- }
1153
- }
1154
-
1155
1061
// Some types are used a lot. Make sure they don't unintentionally get bigger.
1156
1062
#[ cfg( target_pointer_width = "64" ) ]
1157
1063
mod size_asserts {
@@ -1161,7 +1067,6 @@ mod size_asserts {
1161
1067
// tidy-alphabetical-start
1162
1068
static_assert_size ! ( Lit , 12 ) ;
1163
1069
static_assert_size ! ( LitKind , 2 ) ;
1164
- static_assert_size ! ( Nonterminal , 8 ) ;
1165
1070
static_assert_size ! ( Token , 24 ) ;
1166
1071
static_assert_size ! ( TokenKind , 16 ) ;
1167
1072
// tidy-alphabetical-end
0 commit comments