@@ -111,7 +111,7 @@ impl Lit {
111111 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => Some ( Lit :: new ( Bool , name, None ) ) ,
112112 Literal ( token_lit) => Some ( token_lit) ,
113113 Interpolated ( ref nt)
114- if let NtExpr ( expr) | NtLiteral ( expr) = & nt . 0
114+ if let NtExpr ( expr) | NtLiteral ( expr) = & * * nt
115115 && let ast:: ExprKind :: Lit ( token_lit) = expr. kind =>
116116 {
117117 Some ( token_lit)
@@ -314,15 +314,24 @@ pub enum TokenKind {
314314 Literal ( Lit ) ,
315315
316316 /// Identifier token.
317- /// Do not forget about `NtIdent ` when you want to match on identifiers.
317+ /// Do not forget about `InterpolatedIdent ` when you want to match on identifiers.
318318 /// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
319319 /// treat regular and interpolated identifiers in the same way.
320320 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+ InterpolatedIdent ( Ident , IdentIsRaw ) ,
325+
321326 /// Lifetime identifier token.
322- /// Do not forget about `NtLifetime ` when you want to match on lifetime identifiers.
327+ /// Do not forget about `InterpolatedLIfetime ` when you want to match on lifetime identifiers.
323328 /// It's recommended to use `Token::(lifetime,uninterpolate,uninterpolated_span)` to
324329 /// treat regular and interpolated lifetime identifiers in the same way.
325330 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+ InterpolatedLifetime ( Ident ) ,
326335
327336 /// An embedded AST node, as produced by a macro. This only exists for
328337 /// historical reasons. We'd like to get rid of it, for multiple reasons.
@@ -333,7 +342,11 @@ pub enum TokenKind {
333342 /// - It prevents `Token` from implementing `Copy`.
334343 /// It adds complexity and likely slows things down. Please don't add new
335344 /// 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 > ) ,
337350
338351 /// A doc comment token.
339352 /// `Symbol` is the doc comment's data excluding its "quotes" (`///`, `/**`, etc)
@@ -440,8 +453,9 @@ impl Token {
440453 /// Note that keywords are also identifiers, so they should use this
441454 /// if they keep spans or perform edition checks.
442455 pub fn uninterpolated_span ( & self ) -> Span {
443- match & self . kind {
444- Interpolated ( nt) => nt. 0 . use_span ( ) ,
456+ match self . kind {
457+ InterpolatedIdent ( ident, _) | InterpolatedLifetime ( ident) => ident. span ,
458+ Interpolated ( ref nt) => nt. use_span ( ) ,
445459 _ => self . span ,
446460 }
447461 }
@@ -458,8 +472,16 @@ impl Token {
458472 true
459473 }
460474
461- OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | DocComment ( ..) | Ident ( ..)
462- | Lifetime ( ..) | Interpolated ( ..) | Eof => false ,
475+ OpenDelim ( ..)
476+ | CloseDelim ( ..)
477+ | Literal ( ..)
478+ | DocComment ( ..)
479+ | Ident ( ..)
480+ | InterpolatedIdent ( ..)
481+ | Lifetime ( ..)
482+ | InterpolatedLifetime ( ..)
483+ | Interpolated ( ..)
484+ | Eof => false ,
463485 }
464486 }
465487
@@ -486,7 +508,7 @@ impl Token {
486508 PathSep | // global path
487509 Lifetime ( ..) | // labeled loop
488510 Pound => true , // expression attributes
489- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtLiteral ( ..) |
511+ Interpolated ( ref nt) => matches ! ( & * * nt , NtLiteral ( ..) |
490512 NtExpr ( ..) |
491513 NtBlock ( ..) |
492514 NtPath ( ..) ) ,
@@ -510,7 +532,7 @@ impl Token {
510532 | DotDot | DotDotDot | DotDotEq // ranges
511533 | Lt | BinOp ( Shl ) // associated path
512534 | PathSep => true , // global path
513- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtLiteral ( ..) |
535+ Interpolated ( ref nt) => matches ! ( & * * nt , NtLiteral ( ..) |
514536 NtPat ( ..) |
515537 NtBlock ( ..) |
516538 NtPath ( ..) ) ,
@@ -533,7 +555,7 @@ impl Token {
533555 Lifetime ( ..) | // lifetime bound in trait object
534556 Lt | BinOp ( Shl ) | // associated path
535557 PathSep => true , // global path
536- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtTy ( ..) | NtPath ( ..) ) ,
558+ Interpolated ( ref nt) => matches ! ( & * * nt , NtTy ( ..) | NtPath ( ..) ) ,
537559 // For anonymous structs or unions, which only appear in specific positions
538560 // (type of struct fields or union fields), we don't consider them as regular types
539561 _ => false ,
@@ -544,7 +566,7 @@ impl Token {
544566 pub fn can_begin_const_arg ( & self ) -> bool {
545567 match self . kind {
546568 OpenDelim ( Delimiter :: Brace ) => true ,
547- Interpolated ( ref nt) => matches ! ( & nt . 0 , NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
569+ Interpolated ( ref nt) => matches ! ( & * * nt , NtExpr ( ..) | NtBlock ( ..) | NtLiteral ( ..) ) ,
548570 _ => self . can_begin_literal_maybe_minus ( ) ,
549571 }
550572 }
@@ -589,7 +611,7 @@ impl Token {
589611 match self . uninterpolate ( ) . kind {
590612 Literal ( ..) | BinOp ( Minus ) => true ,
591613 Ident ( name, IdentIsRaw :: No ) if name. is_bool_lit ( ) => true ,
592- Interpolated ( ref nt) => match & nt . 0 {
614+ Interpolated ( ref nt) => match & * * nt {
593615 NtLiteral ( _) => true ,
594616 NtExpr ( e) => match & e. kind {
595617 ast:: ExprKind :: Lit ( _) => true ,
@@ -609,14 +631,11 @@ impl Token {
609631 /// into the regular identifier or lifetime token it refers to,
610632 /// otherwise returns the original token.
611633 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- } ,
634+ match self . kind {
635+ InterpolatedIdent ( ident, is_raw) => {
636+ Cow :: Owned ( Token :: new ( Ident ( ident. name , is_raw) , ident. span ) )
637+ }
638+ InterpolatedLifetime ( ident) => Cow :: Owned ( Token :: new ( Lifetime ( ident. name ) , ident. span ) ) ,
620639 _ => Cow :: Borrowed ( self ) ,
621640 }
622641 }
@@ -625,12 +644,9 @@ impl Token {
625644 #[ inline]
626645 pub fn ident ( & self ) -> Option < ( Ident , IdentIsRaw ) > {
627646 // 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- } ,
647+ match self . kind {
648+ Ident ( name, is_raw) => Some ( ( Ident :: new ( name, self . span ) , is_raw) ) ,
649+ InterpolatedIdent ( ident, is_raw) => Some ( ( ident, is_raw) ) ,
634650 _ => None ,
635651 }
636652 }
@@ -639,12 +655,9 @@ impl Token {
639655 #[ inline]
640656 pub fn lifetime ( & self ) -> Option < Ident > {
641657 // 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- } ,
658+ match self . kind {
659+ Lifetime ( name) => Some ( Ident :: new ( name, self . span ) ) ,
660+ InterpolatedLifetime ( ident) => Some ( ident) ,
648661 _ => None ,
649662 }
650663 }
@@ -668,7 +681,7 @@ impl Token {
668681 /// Returns `true` if the token is an interpolated path.
669682 fn is_whole_path ( & self ) -> bool {
670683 if let Interpolated ( nt) = & self . kind
671- && let NtPath ( ..) = & nt . 0
684+ && let NtPath ( ..) = & * * nt
672685 {
673686 return true ;
674687 }
@@ -681,7 +694,7 @@ impl Token {
681694 /// (which happens while parsing the result of macro expansion)?
682695 pub fn is_whole_expr ( & self ) -> bool {
683696 if let Interpolated ( nt) = & self . kind
684- && let NtExpr ( _) | NtLiteral ( _) | NtPath ( _) | NtBlock ( _) = & nt . 0
697+ && let NtExpr ( _) | NtLiteral ( _) | NtPath ( _) | NtBlock ( _) = & * * nt
685698 {
686699 return true ;
687700 }
@@ -692,7 +705,7 @@ impl Token {
692705 /// Is the token an interpolated block (`$b:block`)?
693706 pub fn is_whole_block ( & self ) -> bool {
694707 if let Interpolated ( nt) = & self . kind
695- && let NtBlock ( ..) = & nt . 0
708+ && let NtBlock ( ..) = & * * nt
696709 {
697710 return true ;
698711 }
@@ -831,10 +844,36 @@ impl Token {
831844 _ => return None ,
832845 } ,
833846
834- Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq ( ..) | At | DotDotDot
835- | DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
836- | Question | OpenDelim ( ..) | CloseDelim ( ..) | Literal ( ..) | Ident ( ..)
837- | Lifetime ( ..) | Interpolated ( ..) | DocComment ( ..) | Eof => return None ,
847+ Le
848+ | EqEq
849+ | Ne
850+ | Ge
851+ | AndAnd
852+ | OrOr
853+ | Tilde
854+ | BinOpEq ( ..)
855+ | At
856+ | DotDotDot
857+ | DotDotEq
858+ | Comma
859+ | Semi
860+ | PathSep
861+ | RArrow
862+ | LArrow
863+ | FatArrow
864+ | Pound
865+ | Dollar
866+ | Question
867+ | OpenDelim ( ..)
868+ | CloseDelim ( ..)
869+ | Literal ( ..)
870+ | Ident ( ..)
871+ | InterpolatedIdent ( ..)
872+ | Lifetime ( ..)
873+ | InterpolatedLifetime ( ..)
874+ | Interpolated ( ..)
875+ | DocComment ( ..)
876+ | Eof => return None ,
838877 } ;
839878
840879 Some ( Token :: new ( kind, self . span . to ( joint. span ) ) )
@@ -857,8 +896,6 @@ pub enum Nonterminal {
857896 NtPat ( P < ast:: Pat > ) ,
858897 NtExpr ( P < ast:: Expr > ) ,
859898 NtTy ( P < ast:: Ty > ) ,
860- NtIdent ( Ident , IdentIsRaw ) ,
861- NtLifetime ( Ident ) ,
862899 NtLiteral ( P < ast:: Expr > ) ,
863900 /// Stuff inside brackets for attributes
864901 NtMeta ( P < ast:: AttrItem > ) ,
@@ -953,7 +990,6 @@ impl Nonterminal {
953990 NtPat ( pat) => pat. span ,
954991 NtExpr ( expr) | NtLiteral ( expr) => expr. span ,
955992 NtTy ( ty) => ty. span ,
956- NtIdent ( ident, _) | NtLifetime ( ident) => ident. span ,
957993 NtMeta ( attr_item) => attr_item. span ( ) ,
958994 NtPath ( path) => path. span ,
959995 NtVis ( vis) => vis. span ,
@@ -969,8 +1005,6 @@ impl Nonterminal {
9691005 NtExpr ( ..) => "expression" ,
9701006 NtLiteral ( ..) => "literal" ,
9711007 NtTy ( ..) => "type" ,
972- NtIdent ( ..) => "identifier" ,
973- NtLifetime ( ..) => "lifetime" ,
9741008 NtMeta ( ..) => "attribute" ,
9751009 NtPath ( ..) => "path" ,
9761010 NtVis ( ..) => "visibility" ,
@@ -979,18 +1013,12 @@ impl Nonterminal {
9791013}
9801014
9811015impl 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- }
1016+ fn eq ( & self , _rhs : & Self ) -> bool {
1017+ // FIXME: Assume that all nonterminals are not equal, we can't compare them
1018+ // correctly based on data from AST. This will prevent them from matching each other
1019+ // in macros. The comparison will become possible only when each nonterminal has an
1020+ // attached token stream from which it was parsed.
1021+ false
9941022 }
9951023}
9961024
@@ -1003,12 +1031,10 @@ impl fmt::Debug for Nonterminal {
10031031 NtPat ( ..) => f. pad ( "NtPat(..)" ) ,
10041032 NtExpr ( ..) => f. pad ( "NtExpr(..)" ) ,
10051033 NtTy ( ..) => f. pad ( "NtTy(..)" ) ,
1006- NtIdent ( ..) => f. pad ( "NtIdent(..)" ) ,
10071034 NtLiteral ( ..) => f. pad ( "NtLiteral(..)" ) ,
10081035 NtMeta ( ..) => f. pad ( "NtMeta(..)" ) ,
10091036 NtPath ( ..) => f. pad ( "NtPath(..)" ) ,
10101037 NtVis ( ..) => f. pad ( "NtVis(..)" ) ,
1011- NtLifetime ( ..) => f. pad ( "NtLifetime(..)" ) ,
10121038 }
10131039 }
10141040}
0 commit comments