@@ -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
}
@@ -447,8 +448,9 @@ pub enum TokenKind {
447
448
448
449
/// Identifier token.
449
450
/// Do not forget about `NtIdent` when you want to match on identifiers.
450
- /// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
451
- /// treat regular and interpolated identifiers in the same way.
451
+ /// It's recommended to use `Token::{ident,uninterpolate}` and
452
+ /// `Parser::token_uninterpolated_span` to treat regular and interpolated
453
+ /// identifiers in the same way.
452
454
Ident ( Symbol , IdentIsRaw ) ,
453
455
/// This identifier (and its span) is the identifier passed to the
454
456
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -457,8 +459,9 @@ pub enum TokenKind {
457
459
458
460
/// Lifetime identifier token.
459
461
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
460
- /// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
461
- /// treat regular and interpolated lifetime identifiers in the same way.
462
+ /// It's recommended to use `Token::{ident,uninterpolate}` and
463
+ /// `Parser::token_uninterpolated_span` to treat regular and interpolated
464
+ /// identifiers in the same way.
462
465
Lifetime ( Symbol , IdentIsRaw ) ,
463
466
/// This identifier (and its span) is the lifetime passed to the
464
467
/// declarative macro. The span in the surrounding `Token` is the span of
@@ -584,20 +587,6 @@ impl Token {
584
587
Token :: new ( Ident ( ident. name , ident. is_raw_guess ( ) . into ( ) ) , ident. span )
585
588
}
586
589
587
- /// For interpolated tokens, returns a span of the fragment to which the interpolated
588
- /// token refers. For all other tokens this is just a regular span.
589
- /// It is particularly important to use this for identifiers and lifetimes
590
- /// for which spans affect name resolution and edition checks.
591
- /// Note that keywords are also identifiers, so they should use this
592
- /// if they keep spans or perform edition checks.
593
- pub fn uninterpolated_span ( & self ) -> Span {
594
- match self . kind {
595
- NtIdent ( ident, _) | NtLifetime ( ident, _) => ident. span ,
596
- Interpolated ( ref nt) => nt. use_span ( ) ,
597
- _ => self . span ,
598
- }
599
- }
600
-
601
590
pub fn is_range_separator ( & self ) -> bool {
602
591
[ DotDot , DotDotDot , DotDotEq ] . contains ( & self . kind )
603
592
}
@@ -642,12 +631,7 @@ impl Token {
642
631
PathSep | // global path
643
632
Lifetime ( ..) | // labeled loop
644
633
Pound => true , // expression attributes
645
- Interpolated ( ref nt) =>
646
- matches ! ( & * * nt,
647
- NtBlock ( ..) |
648
- NtExpr ( ..) |
649
- NtLiteral ( ..)
650
- ) ,
634
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
651
635
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
652
636
MetaVarKind :: Block |
653
637
MetaVarKind :: Expr { .. } |
@@ -677,11 +661,6 @@ impl Token {
677
661
Lt | // path (UFCS constant)
678
662
Shl => true , // path (double UFCS)
679
663
Or => matches ! ( pat_kind, PatWithOr ) , // leading vert `|` or-pattern
680
- Interpolated ( nt) =>
681
- matches ! ( & * * nt,
682
- | NtExpr ( ..)
683
- | NtLiteral ( ..)
684
- ) ,
685
664
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
686
665
MetaVarKind :: Expr { .. } |
687
666
MetaVarKind :: Literal |
@@ -724,7 +703,7 @@ impl Token {
724
703
match self . kind {
725
704
OpenDelim ( Delimiter :: Brace ) | Literal ( ..) | Minus => true ,
726
705
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
727
- Interpolated ( ref nt) => matches ! ( & * * nt, NtExpr ( .. ) | NtBlock ( .. ) | NtLiteral ( ..) ) ,
706
+ Interpolated ( ref nt) => matches ! ( & * * nt, NtBlock ( ..) ) ,
728
707
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar (
729
708
MetaVarKind :: Expr { .. } | MetaVarKind :: Block | MetaVarKind :: Literal ,
730
709
) ) ) => true ,
@@ -768,22 +747,12 @@ impl Token {
768
747
///
769
748
/// In other words, would this token be a valid start of `parse_literal_maybe_minus`?
770
749
///
771
- /// Keep this in sync with and `Lit::from_token`, excluding unary negation.
750
+ /// Keep this in sync with `Lit::from_token` and `Parser::eat_token_lit`
751
+ /// (excluding unary negation).
772
752
pub fn can_begin_literal_maybe_minus ( & self ) -> bool {
773
753
match self . uninterpolate ( ) . kind {
774
754
Literal ( ..) | Minus => true ,
775
755
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
756
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
788
757
MetaVarKind :: Literal => true ,
789
758
MetaVarKind :: Expr { can_begin_literal_maybe_minus, .. } => {
@@ -798,14 +767,6 @@ impl Token {
798
767
pub fn can_begin_string_literal ( & self ) -> bool {
799
768
match self . uninterpolate ( ) . kind {
800
769
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
770
OpenDelim ( Delimiter :: Invisible ( InvisibleOrigin :: MetaVar ( mv_kind) ) ) => match mv_kind {
810
771
MetaVarKind :: Literal => true ,
811
772
MetaVarKind :: Expr { can_begin_string_literal, .. } => can_begin_string_literal,
@@ -869,19 +830,25 @@ impl Token {
869
830
870
831
/// Is this a pre-parsed expression dropped into the token stream
871
832
/// (which happens while parsing the result of macro expansion)?
872
- pub fn is_whole_expr ( & self ) -> bool {
833
+ pub fn is_metavar_expr ( & self ) -> bool {
873
834
#[ allow( irrefutable_let_patterns) ] // FIXME: temporary
874
835
if let Interpolated ( nt) = & self . kind
875
- && let NtExpr ( _ ) | NtLiteral ( _ ) | NtBlock ( _) = & * * nt
836
+ && let NtBlock ( _) = & * * nt
876
837
{
877
838
true
839
+ } else if matches ! (
840
+ self . is_metavar_seq( ) ,
841
+ Some ( MetaVarKind :: Expr { .. } | MetaVarKind :: Literal | MetaVarKind :: Path )
842
+ ) {
843
+ true
878
844
} else {
879
845
matches ! ( self . is_metavar_seq( ) , Some ( MetaVarKind :: Path ) )
880
846
}
881
847
}
882
848
883
849
/// Is the token an interpolated block (`$b:block`)?
884
850
pub fn is_whole_block ( & self ) -> bool {
851
+ #[ allow( irrefutable_let_patterns) ] // FIXME: temporary
885
852
if let Interpolated ( nt) = & self . kind
886
853
&& let NtBlock ( ..) = & * * nt
887
854
{
@@ -1100,8 +1067,6 @@ pub enum NtExprKind {
1100
1067
/// For interpolation during macro expansion.
1101
1068
pub enum Nonterminal {
1102
1069
NtBlock ( P < ast:: Block > ) ,
1103
- NtExpr ( P < ast:: Expr > ) ,
1104
- NtLiteral ( P < ast:: Expr > ) ,
1105
1070
}
1106
1071
1107
1072
#[ derive( Debug , Copy , Clone , PartialEq , Eq , Encodable , Decodable , Hash , HashStable_Generic ) ]
@@ -1191,15 +1156,12 @@ impl Nonterminal {
1191
1156
pub fn use_span ( & self ) -> Span {
1192
1157
match self {
1193
1158
NtBlock ( block) => block. span ,
1194
- NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
1195
1159
}
1196
1160
}
1197
1161
1198
1162
pub fn descr ( & self ) -> & ' static str {
1199
1163
match self {
1200
1164
NtBlock ( ..) => "block" ,
1201
- NtExpr ( ..) => "expression" ,
1202
- NtLiteral ( ..) => "literal" ,
1203
1165
}
1204
1166
}
1205
1167
}
@@ -1218,8 +1180,6 @@ impl fmt::Debug for Nonterminal {
1218
1180
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1219
1181
match * self {
1220
1182
NtBlock ( ..) => f. pad ( "NtBlock(..)" ) ,
1221
- NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1222
- NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1223
1183
}
1224
1184
}
1225
1185
}
@@ -1242,7 +1202,7 @@ mod size_asserts {
1242
1202
// tidy-alphabetical-start
1243
1203
static_assert_size ! ( Lit , 12 ) ;
1244
1204
static_assert_size ! ( LitKind , 2 ) ;
1245
- static_assert_size ! ( Nonterminal , 16 ) ;
1205
+ static_assert_size ! ( Nonterminal , 8 ) ;
1246
1206
static_assert_size ! ( Token , 24 ) ;
1247
1207
static_assert_size ! ( TokenKind , 16 ) ;
1248
1208
// tidy-alphabetical-end
0 commit comments