@@ -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
}
@@ -558,6 +559,9 @@ impl Token {
558
559
/// for which spans affect name resolution and edition checks.
559
560
/// Note that keywords are also identifiers, so they should use this
560
561
/// if they keep spans or perform edition checks.
562
+ //
563
+ // Note: `Parser::uninterpolated_token_span` may give better information
564
+ // than this method does.
561
565
pub fn uninterpolated_span ( & self ) -> Span {
562
566
match self . kind {
563
567
NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
@@ -610,12 +614,7 @@ impl Token {
610
614
PathSep | // global path
611
615
Lifetime ( ..) | // labeled loop
612
616
Pound => true , // expression attributes
613
- Interpolated ( ref nt) =>
614
- matches ! ( & * * nt,
615
- NtBlock ( ..) |
616
- NtExpr ( ..) |
617
- NtLiteral ( ..)
618
- ) ,
617
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
619
618
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
620
619
MetaVarKind :: Block |
621
620
MetaVarKind :: Expr { .. } |
@@ -646,11 +645,6 @@ impl Token {
646
645
BinOp ( Shl ) => true , // path (double UFCS)
647
646
// leading vert `|` or-pattern
648
647
BinOp ( Or ) => matches ! ( pat_kind, PatWithOr ) ,
649
- Interpolated ( nt) =>
650
- matches ! ( & * * nt,
651
- | NtExpr ( ..)
652
- | NtLiteral ( ..)
653
- ) ,
654
648
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
655
649
MetaVarKind :: Expr { .. } |
656
650
MetaVarKind :: Literal |
@@ -693,7 +687,7 @@ impl Token {
693
687
match self . kind {
694
688
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | BinOp ( Minus ) => true ,
695
689
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
696
- Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( .. ) | NtBlock ( .. ) | NtLiteral ( ..) ) ,
690
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
697
691
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
698
692
MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
699
693
) ) ) => true ,
@@ -737,22 +731,12 @@ impl Token {
737
731
///
738
732
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
739
733
///
740
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
734
+ /// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
735
+ /// (excluding unary negation).
741
736
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
742
737
match self . uninterpolate ( ) . kind {
743
738
Literal ( ..) | BinOp ( Minus ) => true ,
744
739
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
745
- Interpolated ( ref nt) => match & * * nt {
746
- NtLiteral ( _) => true ,
747
- NtExpr ( e) => match & e. kind {
748
- ast:: ExprKind :: Lit ( _) => true ,
749
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
750
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
751
- }
752
- _ => false ,
753
- } ,
754
- _ => false ,
755
- } ,
756
740
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
757
741
MetaVarKind :: Literal => true ,
758
742
MetaVarKind :: Expr { can_begin_literal_maybe_minus, .. } => {
@@ -767,14 +751,6 @@ impl Token {
767
751
pub fn can_begin_string_literal ( & self ) -> bool {
768
752
match self . uninterpolate ( ) . kind {
769
753
Literal ( ..) => true ,
770
- Interpolated ( ref nt) => match & * * nt {
771
- NtLiteral ( _) => true ,
772
- NtExpr ( e) => match & e. kind {
773
- ast:: ExprKind :: Lit ( _) => true ,
774
- _ => false ,
775
- } ,
776
- _ => false ,
777
- } ,
778
754
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
779
755
MetaVarKind :: Literal => true ,
780
756
MetaVarKind :: Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -838,13 +814,16 @@ impl Token {
838
814
839
815
/// Is this a pre-parsed expression dropped into the token stream
840
816
/// (which happens while parsing the result of macro expansion)?
841
- pub fn is_whole_expr ( & self ) -> bool {
817
+ pub fn is_metavar_expr ( & self ) -> bool {
842
818
#[ allow( irrefutable_let_patterns) ] // FIXME: temporary
843
819
if let Interpolated ( nt) = & self . kind
844
- && let NtExpr ( _ ) | NtLiteral ( _ ) | NtBlock ( _) = & * * nt
820
+ && let NtBlock ( _) = & * * nt
845
821
{
846
822
true
847
- } else if matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Path ) ) {
823
+ } else if matches ! (
824
+ self . is_metavar_seq( ) ,
825
+ Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
826
+ ) {
848
827
true
849
828
} else {
850
829
false
@@ -853,6 +832,7 @@ impl Token {
853
832
854
833
/// Is the token an interpolated block (`$b:block`)?
855
834
pub fn is_whole_block ( & self ) -> bool {
835
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
856
836
if let Interpolated ( nt) = & self . kind
857
837
&& let NtBlock ( ..) = & * * nt
858
838
{
@@ -1046,8 +1026,6 @@ pub enum NtExprKind {
1046
1026
/// For interpolation during macro expansion.
1047
1027
pub enum Nonterminal {
1048
1028
NtBlock ( P < ast:: Block > ) ,
1049
- NtExpr ( P < ast:: Expr > ) ,
1050
- NtLiteral ( P < ast:: Expr > ) ,
1051
1029
}
1052
1030
1053
1031
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -1137,15 +1115,12 @@ impl Nonterminal {
1137
1115
pub fn use_span ( & self ) -> Span {
1138
1116
match self {
1139
1117
NtBlock ( block) => block. span ,
1140
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
1141
1118
}
1142
1119
}
1143
1120
1144
1121
pub fn descr ( & self ) -> & ' static str {
1145
1122
match self {
1146
1123
NtBlock ( ..) => "block" ,
1147
- NtExpr ( ..) => "expression" ,
1148
- NtLiteral ( ..) => "literal" ,
1149
1124
}
1150
1125
}
1151
1126
}
@@ -1164,8 +1139,6 @@ impl fmt::Debug for Nonterminal {
1164
1139
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1165
1140
match * self {
1166
1141
NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1167
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1168
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1169
1142
}
1170
1143
}
1171
1144
}
@@ -1188,7 +1161,7 @@ mod size_asserts {
1188
1161
// tidy-alphabetical-start
1189
1162
static_assert_size ! ( Lit , 12 ) ;
1190
1163
static_assert_size ! ( LitKind , 2 ) ;
1191
- static_assert_size ! ( Nonterminal , 16 ) ;
1164
+ static_assert_size ! ( Nonterminal , 8 ) ;
1192
1165
static_assert_size ! ( Token , 24 ) ;
1193
1166
static_assert_size ! ( TokenKind , 16 ) ;
1194
1167
// tidy-alphabetical-end
0 commit comments