@@ -198,16 +198,17 @@ impl Lit {
198
198
}
199
199
}
200
200
201
- /// Keep this in sync with `Token::can_begin_literal_maybe_minus` excluding unary negation.
201
+ /// Keep this in sync with `Token::can_begin_literal_maybe_minus` and
202
+ /// `Parser::eat_token_lit` (excluding unary negation).
202
203
pub fn from_token ( token : & Token ) -> Option < Lit > {
203
204
match token. uninterpolate ( ) . kind {
204
205
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
205
206
Literal ( token_lit) => Some ( token_lit) ,
206
- Interpolated ( ref nt )
207
- if let NtExpr ( expr ) | NtLiteral ( expr ) = & * * nt
208
- && let ast :: ExprKind :: Lit ( token_lit ) = expr . kind =>
209
- {
210
- Some ( token_lit )
207
+ OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
208
+ MetaVarKind :: Literal | MetaVarKind :: Expr { .. } ,
209
+ ) ) ) => {
210
+ // Unreachable with the current test suite.
211
+ panic ! ( "from_token metavar" ) ;
211
212
}
212
213
_ => None ,
213
214
}
@@ -590,6 +591,9 @@ impl Token {
590
591
/// for which spans affect name resolution and edition checks.
591
592
/// Note that keywords are also identifiers, so they should use this
592
593
/// if they keep spans or perform edition checks.
594
+ //
595
+ // Note: `Parser::uninterpolated_token_span` may give better information
596
+ // than this method does.
593
597
pub fn uninterpolated_span ( & self ) -> Span {
594
598
match self . kind {
595
599
NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
@@ -642,12 +646,7 @@ impl Token {
642
646
PathSep | // global path
643
647
Lifetime ( ..) | // labeled loop
644
648
Pound => true , // expression attributes
645
- Interpolated ( ref nt) =>
646
- matches ! ( & * * nt,
647
- NtBlock ( ..) |
648
- NtExpr ( ..) |
649
- NtLiteral ( ..)
650
- ) ,
649
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
651
650
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
652
651
MetaVarKind :: Block |
653
652
MetaVarKind :: Expr { .. } |
@@ -677,11 +676,6 @@ impl Token {
677
676
Lt | // path (UFCS constant)
678
677
Shl => true , // path (double UFCS)
679
678
Or => matches ! ( pat_kind, PatWithOr ) , // leading vert `|` or-pattern
680
- Interpolated ( nt) =>
681
- matches ! ( & * * nt,
682
- | NtExpr ( ..)
683
- | NtLiteral ( ..)
684
- ) ,
685
679
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
686
680
MetaVarKind :: Expr { .. } |
687
681
MetaVarKind :: Literal |
@@ -724,7 +718,7 @@ impl Token {
724
718
match self . kind {
725
719
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | Minus => true ,
726
720
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
727
- Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( .. ) | NtBlock ( .. ) | NtLiteral ( ..) ) ,
721
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
728
722
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
729
723
MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
730
724
) ) ) => true ,
@@ -768,22 +762,12 @@ impl Token {
768
762
///
769
763
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
770
764
///
771
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
765
+ /// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
766
+ /// (excluding unary negation).
772
767
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
773
768
match self . uninterpolate ( ) . kind {
774
769
Literal ( ..) | Minus => true ,
775
770
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
776
- Interpolated ( ref nt) => match & * * nt {
777
- NtLiteral ( _) => true ,
778
- NtExpr ( e) => match & e. kind {
779
- ast:: ExprKind :: Lit ( _) => true ,
780
- ast:: ExprKind :: Unary ( ast:: UnOp :: Neg , e) => {
781
- matches ! ( & e. kind, ast:: ExprKind :: Lit ( _) )
782
- }
783
- _ => false ,
784
- } ,
785
- _ => false ,
786
- } ,
787
771
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
788
772
MetaVarKind :: Literal => true ,
789
773
MetaVarKind :: Expr { can_begin_literal_maybe_minus, .. } => {
@@ -798,14 +782,6 @@ impl Token {
798
782
pub fn can_begin_string_literal ( & self ) -> bool {
799
783
match self . uninterpolate ( ) . kind {
800
784
Literal ( ..) => true ,
801
- Interpolated ( ref nt) => match & * * nt {
802
- NtLiteral ( _) => true ,
803
- NtExpr ( e) => match & e. kind {
804
- ast:: ExprKind :: Lit ( _) => true ,
805
- _ => false ,
806
- } ,
807
- _ => false ,
808
- } ,
809
785
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
810
786
MetaVarKind :: Literal => true ,
811
787
MetaVarKind :: Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -869,19 +845,25 @@ impl Token {
869
845
870
846
/// Is this a pre-parsed expression dropped into the token stream
871
847
/// (which happens while parsing the result of macro expansion)?
872
- pub fn is_whole_expr ( & self ) -> bool {
848
+ pub fn is_metavar_expr ( & self ) -> bool {
873
849
#[ allow( irrefutable_let_patterns) ] // FIXME: temporary
874
850
if let Interpolated ( nt) = & self . kind
875
- && let NtExpr ( _ ) | NtLiteral ( _ ) | NtBlock ( _) = & * * nt
851
+ && let NtBlock ( _) = & * * nt
876
852
{
877
853
true
854
+ } else if matches ! (
855
+ self . is_metavar_seq( ) ,
856
+ Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
857
+ ) {
858
+ true
878
859
} else {
879
860
matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Path ) )
880
861
}
881
862
}
882
863
883
864
/// Is the token an interpolated block (`$b:block`)?
884
865
pub fn is_whole_block ( & self ) -> bool {
866
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
885
867
if let Interpolated ( nt) = & self . kind
886
868
&& let NtBlock ( ..) = & * * nt
887
869
{
@@ -1100,8 +1082,6 @@ pub enum NtExprKind {
1100
1082
/// For interpolation during macro expansion.
1101
1083
pub enum Nonterminal {
1102
1084
NtBlock ( P < ast:: Block > ) ,
1103
- NtExpr ( P < ast:: Expr > ) ,
1104
- NtLiteral ( P < ast:: Expr > ) ,
1105
1085
}
1106
1086
1107
1087
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -1191,15 +1171,12 @@ impl Nonterminal {
1191
1171
pub fn use_span ( & self ) -> Span {
1192
1172
match self {
1193
1173
NtBlock ( block) => block. span ,
1194
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
1195
1174
}
1196
1175
}
1197
1176
1198
1177
pub fn descr ( & self ) -> & ' static str {
1199
1178
match self {
1200
1179
NtBlock ( ..) => "block" ,
1201
- NtExpr ( ..) => "expression" ,
1202
- NtLiteral ( ..) => "literal" ,
1203
1180
}
1204
1181
}
1205
1182
}
@@ -1218,8 +1195,6 @@ impl fmt::Debug for Nonterminal {
1218
1195
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1219
1196
match * self {
1220
1197
NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1221
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1222
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1223
1198
}
1224
1199
}
1225
1200
}
@@ -1242,7 +1217,7 @@ mod size_asserts {
1242
1217
// tidy-alphabetical-start
1243
1218
static_assert_size ! ( Lit , 12 ) ;
1244
1219
static_assert_size ! ( LitKind , 2 ) ;
1245
- static_assert_size ! ( Nonterminal , 16 ) ;
1220
+ static_assert_size ! ( Nonterminal , 8 ) ;
1246
1221
static_assert_size ! ( Token , 24 ) ;
1247
1222
static_assert_size ! ( TokenKind , 16 ) ;
1248
1223
// tidy-alphabetical-end
0 commit comments