@@ -111,7 +111,7 @@ impl Lit {
111
111
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
112
112
Literal ( token_lit) => Some ( token_lit) ,
113
113
Interpolated ( ref nt)
114
- if let NtExpr ( expr) | NtLiteral ( expr) = & nt . 0
114
+ if let NtExpr ( expr) | NtLiteral ( expr) = & * * nt
115
115
&& let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
116
116
{
117
117
Some ( token_lit)
@@ -318,11 +318,20 @@ pub enum TokenKind {
318
318
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
319
319
/// treat regular and interpolated identifiers in the same way.
320
320
Ident ( Symbol , IdentIsRaw ) ,
321
+ /// This identifier (and its span) is the identifier passed to the
322
+ /// declarative macro. The span in the surrounding `Token` is the span of
323
+ /// the `ident` metavariable in the macro's RHS.
324
+ NtIdent ( Ident , IdentIsRaw ) ,
325
+
321
326
/// Lifetime identifier token.
322
327
/// Do not forget about `NtLifetime` when you want to match on lifetime identifiers.
323
328
/// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
324
329
/// treat regular and interpolated lifetime identifiers in the same way.
325
330
Lifetime ( Symbol ) ,
331
+ /// This identifier (and its span) is the lifetime passed to the
332
+ /// declarative macro. The span in the surrounding `Token` is the span of
333
+ /// the `lifetime` metavariable in the macro's RHS.
334
+ NtLifetime ( Ident ) ,
326
335
327
336
/// An embedded AST node, as produced by a macro. This only exists for
328
337
/// historical reasons. We'd like to get rid of it, for multiple reasons.
@@ -333,7 +342,11 @@ pub enum TokenKind {
333
342
/// - It prevents `Token` from implementing `Copy`.
334
343
/// It adds complexity and likely slows things down. Please don't add new
335
344
/// occurrences of this token kind!
336
- Interpolated ( Lrc < ( Nonterminal , Span ) > ) ,
345
+ ///
346
+ /// The span in the surrounding `Token` is that of the metavariable in the
347
+ /// macro's RHS. The span within the Nonterminal is that of the fragment
348
+ /// passed to the macro at the call site.
349
+ Interpolated ( Lrc < Nonterminal > ) ,
337
350
338
351
/// A doc comment token.
339
352
/// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
@@ -440,8 +453,9 @@ impl Token {
440
453
/// Note that keywords are also identifiers, so they should use this
441
454
/// if they keep spans or perform edition checks.
442
455
pub fn uninterpolated_span ( & self ) -> Span {
443
- match & self . kind {
444
- Interpolated ( nt) => nt. 0 . use_span ( ) ,
456
+ match self . kind {
457
+ NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
458
+ Interpolated ( ref nt) => nt. use_span ( ) ,
445
459
_ => self . span ,
446
460
}
447
461
}
@@ -459,7 +473,7 @@ impl Token {
459
473
}
460
474
461
475
OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
462
- | Lifetime ( ..) | Interpolated ( ..) | Eof => false ,
476
+ | NtIdent ( .. ) | Lifetime ( .. ) | NtLifetime ( ..) | Interpolated ( ..) | Eof => false ,
463
477
}
464
478
}
465
479
@@ -486,7 +500,7 @@ impl Token {
486
500
PathSep | // global path
487
501
Lifetime ( ..) | // labeled loop
488
502
Pound => true , // expression attributes
489
- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtLiteral ( ..) |
503
+ Interpolated ( ref nt) => matches ! ( & * * nt , NtLiteral ( ..) |
490
504
NtExpr ( ..) |
491
505
NtBlock ( ..) |
492
506
NtPath ( ..) ) ,
@@ -510,7 +524,7 @@ impl Token {
510
524
| DotDot | DotDotDot | DotDotEq // ranges
511
525
| Lt | BinOp ( Shl ) // associated path
512
526
| PathSep => true , // global path
513
- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtLiteral ( ..) |
527
+ Interpolated ( ref nt) => matches ! ( & * * nt , NtLiteral ( ..) |
514
528
NtPat ( ..) |
515
529
NtBlock ( ..) |
516
530
NtPath ( ..) ) ,
@@ -533,7 +547,7 @@ impl Token {
533
547
Lifetime ( ..) | // lifetime bound in trait object
534
548
Lt | BinOp ( Shl ) | // associated path
535
549
PathSep => true , // global path
536
- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtTy ( ..) | NtPath ( ..) ) ,
550
+ Interpolated ( ref nt) => matches ! ( & * * nt , NtTy ( ..) | NtPath ( ..) ) ,
537
551
// For anonymous structs or unions, which only appear in specific positions
538
552
// (type of struct fields or union fields), we don't consider them as regular types
539
553
_ => false ,
@@ -544,7 +558,7 @@ impl Token {
544
558
pub fn can_begin_const_arg ( & self ) -> bool {
545
559
match self . kind {
546
560
OpenDelim ( Delimiter :: Brace ) => true ,
547
- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
561
+ Interpolated ( ref nt) => matches ! ( & * * nt , NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
548
562
_ => self . can_begin_literal_maybe_minus ( ) ,
549
563
}
550
564
}
@@ -589,7 +603,7 @@ impl Token {
589
603
match self . uninterpolate ( ) . kind {
590
604
Literal ( ..) | BinOp ( Minus ) => true ,
591
605
Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
592
- Interpolated ( ref nt) => match & nt . 0 {
606
+ Interpolated ( ref nt) => match & * * nt {
593
607
NtLiteral ( _) => true ,
594
608
NtExpr ( e) => match & e. kind {
595
609
ast:: ExprKind :: Lit ( _) => true ,
@@ -609,14 +623,9 @@ impl Token {
609
623
/// into the regular identifier or lifetime token it refers to,
610
624
/// otherwise returns the original token.
611
625
pub fn uninterpolate ( & self ) -> Cow < ' _ , Token > {
612
- match & self . kind {
613
- Interpolated ( nt) => match & nt. 0 {
614
- NtIdent ( ident, is_raw) => {
615
- Cow :: Owned ( Token :: new ( Ident ( ident. name , * is_raw) , ident. span ) )
616
- }
617
- NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
618
- _ => Cow :: Borrowed ( self ) ,
619
- } ,
626
+ match self . kind {
627
+ NtIdent ( ident, is_raw) => Cow :: Owned ( Token :: new ( Ident ( ident. name , is_raw) , ident. span ) ) ,
628
+ NtLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
620
629
_ => Cow :: Borrowed ( self ) ,
621
630
}
622
631
}
@@ -625,12 +634,9 @@ impl Token {
625
634
#[ inline]
626
635
pub fn ident ( & self ) -> Option < ( Ident , IdentIsRaw ) > {
627
636
// We avoid using `Token::uninterpolate` here because it's slow.
628
- match & self . kind {
629
- & Ident ( name, is_raw) => Some ( ( Ident :: new ( name, self . span ) , is_raw) ) ,
630
- Interpolated ( nt) => match & nt. 0 {
631
- NtIdent ( ident, is_raw) => Some ( ( * ident, * is_raw) ) ,
632
- _ => None ,
633
- } ,
637
+ match self . kind {
638
+ Ident ( name, is_raw) => Some ( ( Ident :: new ( name, self . span ) , is_raw) ) ,
639
+ NtIdent ( ident, is_raw) => Some ( ( ident, is_raw) ) ,
634
640
_ => None ,
635
641
}
636
642
}
@@ -639,12 +645,9 @@ impl Token {
639
645
#[ inline]
640
646
pub fn lifetime ( & self ) -> Option < Ident > {
641
647
// We avoid using `Token::uninterpolate` here because it's slow.
642
- match & self . kind {
643
- & Lifetime ( name) => Some ( Ident :: new ( name, self . span ) ) ,
644
- Interpolated ( nt) => match & nt. 0 {
645
- NtLifetime ( ident) => Some ( * ident) ,
646
- _ => None ,
647
- } ,
648
+ match self . kind {
649
+ Lifetime ( name) => Some ( Ident :: new ( name, self . span ) ) ,
650
+ NtLifetime ( ident) => Some ( ident) ,
648
651
_ => None ,
649
652
}
650
653
}
@@ -668,7 +671,7 @@ impl Token {
668
671
/// Returns `true` if the token is an interpolated path.
669
672
fn is_whole_path ( & self ) -> bool {
670
673
if let Interpolated ( nt) = & self . kind
671
- && let NtPath ( ..) = & nt . 0
674
+ && let NtPath ( ..) = & * * nt
672
675
{
673
676
return true ;
674
677
}
@@ -681,7 +684,7 @@ impl Token {
681
684
/// (which happens while parsing the result of macro expansion)?
682
685
pub fn is_whole_expr ( & self ) -> bool {
683
686
if let Interpolated ( nt) = & self . kind
684
- && let NtExpr ( _) | NtLiteral ( _) | NtPath ( _) | NtBlock ( _) = & nt . 0
687
+ && let NtExpr ( _) | NtLiteral ( _) | NtPath ( _) | NtBlock ( _) = & * * nt
685
688
{
686
689
return true ;
687
690
}
@@ -692,7 +695,7 @@ impl Token {
692
695
/// Is the token an interpolated block (`$b:block`)?
693
696
pub fn is_whole_block ( & self ) -> bool {
694
697
if let Interpolated ( nt) = & self . kind
695
- && let NtBlock ( ..) = & nt . 0
698
+ && let NtBlock ( ..) = & * * nt
696
699
{
697
700
return true ;
698
701
}
@@ -833,8 +836,10 @@ impl Token {
833
836
834
837
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
835
838
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
836
- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..)
837
- | Lifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => return None ,
839
+ | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..) | NtIdent ( ..)
840
+ | Lifetime ( ..) | NtLifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => {
841
+ return None ;
842
+ }
838
843
} ;
839
844
840
845
Some ( Token :: new ( kind, self . span . to ( joint. span ) ) )
@@ -857,8 +862,6 @@ pub enum Nonterminal {
857
862
NtPat ( P < ast:: Pat > ) ,
858
863
NtExpr ( P < ast:: Expr > ) ,
859
864
NtTy ( P < ast:: Ty > ) ,
860
- NtIdent ( Ident , IdentIsRaw ) ,
861
- NtLifetime ( Ident ) ,
862
865
NtLiteral ( P < ast:: Expr > ) ,
863
866
/// Stuff inside brackets for attributes
864
867
NtMeta ( P < ast:: AttrItem > ) ,
@@ -953,7 +956,6 @@ impl Nonterminal {
953
956
NtPat ( pat) => pat. span ,
954
957
NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
955
958
NtTy ( ty) => ty. span ,
956
- NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
957
959
NtMeta ( attr_item) => attr_item. span ( ) ,
958
960
NtPath ( path) => path. span ,
959
961
NtVis ( vis) => vis. span ,
@@ -969,8 +971,6 @@ impl Nonterminal {
969
971
NtExpr ( ..) => "expression" ,
970
972
NtLiteral ( ..) => "literal" ,
971
973
NtTy ( ..) => "type" ,
972
- NtIdent ( ..) => "identifier" ,
973
- NtLifetime ( ..) => "lifetime" ,
974
974
NtMeta ( ..) => "attribute" ,
975
975
NtPath ( ..) => "path" ,
976
976
NtVis ( ..) => "visibility" ,
@@ -979,18 +979,12 @@ impl Nonterminal {
979
979
}
980
980
981
981
impl PartialEq for Nonterminal {
982
- fn eq ( & self , rhs : & Self ) -> bool {
983
- match ( self , rhs) {
984
- ( NtIdent ( ident_lhs, is_raw_lhs) , NtIdent ( ident_rhs, is_raw_rhs) ) => {
985
- ident_lhs == ident_rhs && is_raw_lhs == is_raw_rhs
986
- }
987
- ( NtLifetime ( ident_lhs) , NtLifetime ( ident_rhs) ) => ident_lhs == ident_rhs,
988
- // FIXME: Assume that all "complex" nonterminal are not equal, we can't compare them
989
- // correctly based on data from AST. This will prevent them from matching each other
990
- // in macros. The comparison will become possible only when each nonterminal has an
991
- // attached token stream from which it was parsed.
992
- _ => false ,
993
- }
982
+ fn eq ( & self , _rhs : & Self ) -> bool {
983
+ // FIXME: Assume that all nonterminals are not equal, we can't compare them
984
+ // correctly based on data from AST. This will prevent them from matching each other
985
+ // in macros. The comparison will become possible only when each nonterminal has an
986
+ // attached token stream from which it was parsed.
987
+ false
994
988
}
995
989
}
996
990
@@ -1003,12 +997,10 @@ impl fmt::Debug for Nonterminal {
1003
997
NtPat ( ..) => f. pad ( "NtPat(..)" ) ,
1004
998
NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
1005
999
NtTy ( ..) => f. pad ( "NtTy(..)" ) ,
1006
- NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
1007
1000
NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
1008
1001
NtMeta ( ..) => f. pad ( "NtMeta(..)" ) ,
1009
1002
NtPath ( ..) => f. pad ( "NtPath(..)" ) ,
1010
1003
NtVis ( ..) => f. pad ( "NtVis(..)" ) ,
1011
- NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
1012
1004
}
1013
1005
}
1014
1006
}
0 commit comments