@@ -210,16 +210,17 @@ impl Lit {
210
210
}
211
211
}
212
212
213
- /// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
213
+ /// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
214
+ /// `Parser::eat_token_lit` (excluding unary negation).
214
215
pub fn from_token ( token : & Token ) -> Option < Lit > {
215
216
match token. uninterpolate ( ) . kind {
216
217
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
217
218
Literal ( token_lit) => Some ( token_lit) ,
218
- Interpolated ( ref nt )
219
- if let NtExpr ( expr ) | NtLiteral ( expr ) = & * * nt
220
- && let ast :: ExprKind :: Lit ( token_lit ) = expr . kind =>
221
- {
222
- Some ( token_lit )
219
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
220
+ MetaVarKind :: Literal | MetaVarKind :: Expr { .. } ,
221
+ ) ) ) => {
222
+ // Unreachable with the current test suite.
223
+ panic ! ( "from_token metavar" ) ;
223
224
}
224
225
_ => None ,
225
226
}
@@ -564,6 +565,9 @@ impl Token {
564
565
/// for which spans affect name resolution and edition checks.
565
566
/// Note that keywords are also identifiers, so they should use this
566
567
/// if they keep spans or perform edition checks.
568
+ //
569
+ // Note: `Parser::uninterpolated_token_span` may give better information
570
+ // than this method does.
567
571
pub fn uninterpolated_span ( & self ) -> Span {
568
572
match self . kind {
569
573
NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
@@ -616,12 +620,7 @@ impl Token {
616
620
PathSep | // global path
617
621
Lifetime ( ..) | // labeled loop
618
622
Pound => true , // expression attributes
619
- Interpolated ( ref nt) =>
620
- matches ! ( & * * nt,
621
- NtBlock ( ..) |
622
- NtExpr ( ..) |
623
- NtLiteral ( ..)
624
- ) ,
623
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
625
624
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
626
625
MetaVarKind :: Block |
627
626
MetaVarKind :: Expr { .. } |
@@ -652,11 +651,6 @@ impl Token {
652
651
BinOp ( Shl ) => true , // path (double UFCS)
653
652
// leading vert `|` or-pattern
654
653
BinOp ( Or ) => matches ! ( pat_kind, PatWithOr ) ,
655
- Interpolated ( nt) =>
656
- matches ! ( & * * nt,
657
- | NtExpr ( ..)
658
- | NtLiteral ( ..)
659
- ) ,
660
654
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
661
655
MetaVarKind :: Expr { .. } |
662
656
MetaVarKind :: Literal |
@@ -699,7 +693,7 @@ impl Token {
699
693
match self . kind {
700
694
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
701
695
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
702
- Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( .. ) | NtBlock ( .. ) | NtLiteral ( ..) ) ,
696
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
703
697
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
704
698
MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
705
699
) ) ) => true ,
@@ -743,22 +737,12 @@ impl Token {
743
737
///
744
738
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
745
739
///
746
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
740
+ /// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
741
+ /// (excluding unary negation).
747
742
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
748
743
match self . uninterpolate ( ) . kind {
749
744
Literal ( ..) | BinOp ( Minus ) => true ,
750
745
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
751
- Interpolated ( ref nt) => match & * * nt {
752
- NtLiteral ( _) => true ,
753
- NtExpr ( e) => match & e. kind {
754
- ast:: ExprKind :: Lit ( _) => true ,
755
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
756
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
757
- }
758
- _ => false ,
759
- } ,
760
- _ => false ,
761
- } ,
762
746
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
763
747
MetaVarKind :: Literal => true ,
764
748
MetaVarKind :: Expr { can_begin_literal_maybe_minus, .. } => {
@@ -773,14 +757,6 @@ impl Token {
773
757
pub fn can_begin_string_literal ( & self ) -> bool {
774
758
match self . uninterpolate ( ) . kind {
775
759
Literal ( ..) => true ,
776
- Interpolated ( ref nt) => match & * * nt {
777
- NtLiteral ( _) => true ,
778
- NtExpr ( e) => match & e. kind {
779
- ast:: ExprKind :: Lit ( _) => true ,
780
- _ => false ,
781
- } ,
782
- _ => false ,
783
- } ,
784
760
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
785
761
MetaVarKind :: Literal => true ,
786
762
MetaVarKind :: Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -844,13 +820,16 @@ impl Token {
844
820
845
821
/// Is this a pre-parsed expression dropped into the token stream
846
822
/// (which happens while parsing the result of macro expansion)?
847
- pub fn is_whole_expr ( & self ) -> bool {
823
+ pub fn is_metavar_expr ( & self ) -> bool {
848
824
#[ allow( irrefutable_let_patterns) ] // FIXME: temporary
849
825
if let Interpolated ( nt) = & self . kind
850
- && let NtExpr ( _ ) | NtLiteral ( _ ) | NtBlock ( _) = & * * nt
826
+ && let NtBlock ( _) = & * * nt
851
827
{
852
828
true
853
- } else if matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Path ) ) {
829
+ } else if matches ! (
830
+ self . is_metavar_seq( ) ,
831
+ Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
832
+ ) {
854
833
true
855
834
} else {
856
835
false
@@ -859,6 +838,7 @@ impl Token {
859
838
860
839
/// Is the token an interpolated block (`$b:block`)?
861
840
pub fn is_whole_block ( & self ) -> bool {
841
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
862
842
if let Interpolated ( nt) = & self . kind
863
843
&& let NtBlock ( ..) = & * * nt
864
844
{
@@ -1052,8 +1032,6 @@ pub enum NtExprKind {
1052
1032
/// For interpolation during macro expansion.
1053
1033
pub enum Nonterminal {
1054
1034
NtBlock ( P < ast:: Block > ) ,
1055
- NtExpr ( P < ast:: Expr > ) ,
1056
- NtLiteral ( P < ast:: Expr > ) ,
1057
1035
}
1058
1036
1059
1037
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -1143,15 +1121,12 @@ impl Nonterminal {
1143
1121
pub fn use_span ( & self ) -> Span {
1144
1122
match self {
1145
1123
NtBlock ( block) => block. span ,
1146
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
1147
1124
}
1148
1125
}
1149
1126
1150
1127
pub fn descr ( & self ) -> & ' static str {
1151
1128
match self {
1152
1129
NtBlock ( ..) => "block" ,
1153
- NtExpr ( ..) => "expression" ,
1154
- NtLiteral ( ..) => "literal" ,
1155
1130
}
1156
1131
}
1157
1132
}
@@ -1170,8 +1145,6 @@ impl fmt::Debug for Nonterminal {
1170
1145
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1171
1146
match * self {
1172
1147
NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1173
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1174
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1175
1148
}
1176
1149
}
1177
1150
}
@@ -1194,7 +1167,7 @@ mod size_asserts {
1194
1167
// tidy-alphabetical-start
1195
1168
static_assert_size ! ( Lit , 12 ) ;
1196
1169
static_assert_size ! ( LitKind , 2 ) ;
1197
- static_assert_size ! ( Nonterminal , 16 ) ;
1170
+ static_assert_size ! ( Nonterminal , 8 ) ;
1198
1171
static_assert_size ! ( Token , 24 ) ;
1199
1172
static_assert_size ! ( TokenKind , 16 ) ;
1200
1173
// tidy-alphabetical-end
0 commit comments