1
1
pub use BinOpToken :: * ;
2
2
pub use LitKind :: * ;
3
- pub use Nonterminal :: * ;
4
3
pub use TokenKind :: * ;
5
4
6
5
use crate :: ast;
7
- use crate :: ptr:: P ;
8
6
use crate :: util:: case:: Case ;
9
7
10
- use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
11
- use rustc_data_structures:: sync:: Lrc ;
12
8
use rustc_macros:: { Decodable , Encodable , HashStable_Generic } ;
13
9
use rustc_span:: symbol:: { kw, sym} ;
14
10
#[ allow( clippy:: useless_attribute) ] // FIXME: following use of `hidden_glob_reexports` incorrectly triggers `useless_attribute` lint.
@@ -283,9 +279,7 @@ impl From<IdentIsRaw> for bool {
283
279
}
284
280
}
285
281
286
- // SAFETY: due to the `Clone` impl below, all fields of all variants other than
287
- // `Interpolated` must impl `Copy`.
288
- #[ derive( PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
282
+ #[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
289
283
pub enum TokenKind {
290
284
/* Expression-operator symbols. */
291
285
/// `=`
@@ -374,21 +368,6 @@ pub enum TokenKind {
374
368
/// the `lifetime` metavariable in the macro's RHS.
375
369
NtLifetime ( Ident ) ,
376
370
377
- /// An embedded AST node, as produced by a macro. This only exists for
378
- /// historical reasons. We'd like to get rid of it, for multiple reasons.
379
- /// - It's conceptually very strange. Saying a token can contain an AST
380
- /// node is like saying, in natural language, that a word can contain a
381
- /// sentence.
382
- /// - It requires special handling in a bunch of places in the parser.
383
- /// - It prevents `Token` from implementing `Copy`.
384
- /// It adds complexity and likely slows things down. Please don't add new
385
- /// occurrences of this token kind!
386
- ///
387
- /// The span in the surrounding `Token` is that of the metavariable in the
388
- /// macro's RHS. The span within the Nonterminal is that of the fragment
389
- /// passed to the macro at the call site.
390
- Interpolated ( Lrc < Nonterminal > ) ,
391
-
392
371
/// A doc comment token.
393
372
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
394
373
/// similarly to symbols in string literal tokens.
@@ -398,19 +377,6 @@ pub enum TokenKind {
398
377
Eof ,
399
378
}
400
379
401
- impl Clone for TokenKind {
402
- fn clone ( & self ) -> Self {
403
- // `TokenKind` would impl `Copy` if it weren't for `Interpolated`. So
404
- // for all other variants, this implementation of `clone` is just like
405
- // a copy. This is faster than the `derive(Clone)` version which has a
406
- // separate path for every variant.
407
- match self {
408
- Interpolated ( nt) => Interpolated ( nt. clone ( ) ) ,
409
- _ => unsafe { std:: ptr:: read ( self ) } ,
410
- }
411
- }
412
- }
413
-
414
380
#[ derive( Clone , PartialEq , Encodable , Decodable , Debug , HashStable_Generic ) ]
415
381
pub struct Token {
416
382
pub kind : TokenKind ,
@@ -497,7 +463,6 @@ impl Token {
497
463
pub fn uninterpolated_span ( & self ) -> Span {
498
464
match self . kind {
499
465
NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
500
- Interpolated ( ref nt) => nt. use_span ( ) ,
501
466
_ => self . span ,
502
467
}
503
468
}
@@ -515,7 +480,7 @@ impl Token {
515
480
}
516
481
517
482
OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
518
- | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | Eof => false ,
483
+ | NtIdent ( ..) | Lifetime ( ..) | NtLifetime ( ..) | Eof => false ,
519
484
}
520
485
}
521
486
@@ -543,7 +508,6 @@ impl Token {
543
508
PathSep | // global path
544
509
Lifetime ( ..) | // labeled loop
545
510
Pound => true , // expression attributes
546
- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
547
511
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
548
512
NonterminalKind :: Block |
549
513
NonterminalKind :: Expr |
@@ -571,7 +535,6 @@ impl Token {
571
535
| DotDot | DotDotDot | DotDotEq // ranges
572
536
| Lt | BinOp ( Shl ) // associated path
573
537
| PathSep => true , // global path
574
- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
575
538
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
576
539
NonterminalKind :: Block |
577
540
NonterminalKind :: PatParam { .. } |
@@ -613,7 +576,6 @@ impl Token {
613
576
match self . kind {
614
577
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
615
578
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
616
- Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
617
579
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
618
580
NonterminalKind :: Expr
619
581
| NonterminalKind :: Expr2021 { .. }
@@ -760,31 +722,20 @@ impl Token {
760
722
/// That is, is this a pre-parsed expression dropped into the token stream
761
723
/// (which happens while parsing the result of macro expansion)?
762
724
pub fn is_metavar_expr ( & self ) -> bool {
763
- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
764
- if let Interpolated ( nt) = & self . kind
765
- && let NtBlock ( _) = & * * nt
766
- {
767
- true
768
- } else if matches ! (
725
+ matches ! (
769
726
self . is_metavar_seq( ) ,
770
- Some ( NonterminalKind :: Expr | NonterminalKind :: Literal | NonterminalKind :: Path )
771
- ) {
772
- true
773
- } else {
774
- false
775
- }
727
+ Some (
728
+ NonterminalKind :: Expr
729
+ | NonterminalKind :: Literal
730
+ | NonterminalKind :: Path
731
+ | NonterminalKind :: Block
732
+ )
733
+ )
776
734
}
777
735
778
- /// Is the token an interpolated block (`$b:block`)?
779
- pub fn is_whole_block ( & self ) -> bool {
780
- #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
781
- if let Interpolated ( nt) = & self . kind
782
- && let NtBlock ( ..) = & * * nt
783
- {
784
- return true ;
785
- }
786
-
787
- false
736
+ /// Are we at a block from a metavar (`$b:block`)?
737
+ pub fn is_metavar_block ( & self ) -> bool {
738
+ matches ! ( self . is_metavar_seq( ) , Some ( NonterminalKind :: Block ) )
788
739
}
789
740
790
741
/// Returns `true` if the token is either the `mut` or `const` keyword.
@@ -809,7 +760,8 @@ impl Token {
809
760
self . is_non_raw_ident_where ( |id| id. name == kw)
810
761
}
811
762
812
- /// 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.
763
+ /// Returns `true` if the token is a given keyword, `kw` or if `case` is `Insensitive` and this
764
+ /// token is an identifier equal to `kw` ignoring the case.
813
765
pub fn is_keyword_case ( & self , kw : Symbol , case : Case ) -> bool {
814
766
self . is_keyword ( kw)
815
767
|| ( case == Case :: Insensitive
@@ -930,7 +882,7 @@ impl Token {
930
882
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
931
883
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
932
884
| Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
933
- | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( .. ) | DocComment ( ..) | Eof => {
885
+ | Lifetime ( ..) | NtLifetime ( ..) | DocComment ( ..) | Eof => {
934
886
return None ;
935
887
}
936
888
} ;
@@ -946,12 +898,6 @@ impl PartialEq<TokenKind> for Token {
946
898
}
947
899
}
948
900
949
- #[ derive( Clone , Encodable , Decodable ) ]
950
- /// For interpolation during macro expansion.
951
- pub enum Nonterminal {
952
- NtBlock ( P < ast:: Block > ) ,
953
- }
954
-
955
901
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
956
902
pub enum NonterminalKind {
957
903
Item ,
@@ -1043,47 +989,6 @@ impl fmt::Display for NonterminalKind {
1043
989
}
1044
990
}
1045
991
1046
- impl Nonterminal {
1047
- pub fn use_span ( & self ) -> Span {
1048
- match self {
1049
- NtBlock ( block) => block. span ,
1050
- }
1051
- }
1052
-
1053
- pub fn descr ( & self ) -> & ' static str {
1054
- match self {
1055
- NtBlock ( ..) => "block" ,
1056
- }
1057
- }
1058
- }
1059
-
1060
- impl PartialEq for Nonterminal {
1061
- fn eq ( & self , _rhs : & Self ) -> bool {
1062
- // FIXME: Assume that all nonterminals are not equal, we can't compare them
1063
- // correctly based on data from AST. This will prevent them from matching each other
1064
- // in macros. The comparison will become possible only when each nonterminal has an
1065
- // attached token stream from which it was parsed.
1066
- false
1067
- }
1068
- }
1069
-
1070
- impl fmt:: Debug for Nonterminal {
1071
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1072
- match * self {
1073
- NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1074
- }
1075
- }
1076
- }
1077
-
1078
- impl < CTX > HashStable < CTX > for Nonterminal
1079
- where
1080
- CTX : crate :: HashStableContext ,
1081
- {
1082
- fn hash_stable ( & self , _hcx : & mut CTX , _hasher : & mut StableHasher ) {
1083
- panic ! ( "interpolated tokens should not be present in the HIR" )
1084
- }
1085
- }
1086
-
1087
992
// Some types are used a lot. Make sure they don't unintentionally get bigger.
1088
993
#[ cfg( target_pointer_width = "64" ) ]
1089
994
mod size_asserts {
@@ -1092,7 +997,6 @@ mod size_asserts {
1092
997
// tidy-alphabetical-start
1093
998
static_assert_size ! ( Lit , 12 ) ;
1094
999
static_assert_size ! ( LitKind , 2 ) ;
1095
- static_assert_size ! ( Nonterminal , 8 ) ;
1096
1000
static_assert_size ! ( Token , 24 ) ;
1097
1001
static_assert_size ! ( TokenKind , 16 ) ;
1098
1002
// tidy-alphabetical-end
0 commit comments