@@ -487,7 +487,7 @@ impl PartialEq<FileName> for SourceFile {
487
487
pub enum TokenTree {
488
488
/// A token stream surrounded by bracket delimiters.
489
489
Group ( Group ) ,
490
- /// An identifier or lifetime identifier .
490
+ /// An identifier.
491
491
Ident ( Ident ) ,
492
492
/// A single punctuation character (`+`, `,`, `$`, etc.).
493
493
Punct ( Punct ) ,
@@ -702,9 +702,10 @@ impl !Sync for Punct {}
702
702
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
703
703
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
704
704
pub enum Spacing {
705
- /// e .g. `+` is `Alone` in `+ =`, `+ident` or `+()`.
705
+ /// E .g. `+` is `Alone` in `+ =`, `+ident` or `+()`.
706
706
Alone ,
707
- /// e.g. `+` is `Joint` in `+=` or `+#`.
707
+ /// E.g. `+` is `Joint` in `+=` or `'#`.
708
+ /// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`.
708
709
Joint ,
709
710
}
710
711
@@ -717,8 +718,8 @@ impl Punct {
717
718
/// which can be further configured with the `set_span` method below.
718
719
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
719
720
pub fn new ( ch : char , spacing : Spacing ) -> Punct {
720
- const LEGAL_CHARS : & [ char ] = & [ '=' , '<' , '>' , '!' , '~' , '+' , '-' , '*' , '/' , '%' ,
721
- '^' , ' &', '|' , '@' , '.' , ',' , ';' , ':' , '#' , '$' , '?' ] ;
721
+ const LEGAL_CHARS : & [ char ] = & [ '=' , '<' , '>' , '!' , '~' , '+' , '-' , '*' , '/' , '%' , '^' ,
722
+ '&' , '|' , '@' , '.' , ',' , ';' , ':' , '#' , '$' , '?' , '\' '] ;
722
723
if !LEGAL_CHARS . contains ( & ch) {
723
724
panic ! ( "unsupported character `{:?}`" , ch)
724
725
}
@@ -766,7 +767,7 @@ impl fmt::Display for Punct {
766
767
}
767
768
}
768
769
769
- /// An identifier (`ident`) or lifetime identifier (`'ident`) .
770
+ /// An identifier (`ident`).
770
771
#[ derive( Clone , Debug ) ]
771
772
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
772
773
pub struct Ident {
@@ -783,7 +784,7 @@ impl !Sync for Ident {}
783
784
impl Ident {
784
785
/// Creates a new `Ident` with the given `string` as well as the specified
785
786
/// `span`.
786
- /// The `string` argument must be a valid identifier or lifetime identifier permitted by the
787
+ /// The `string` argument must be a valid identifier permitted by the
787
788
/// language, otherwise the function will panic.
788
789
///
789
790
/// Note that `span`, currently in rustc, configures the hygiene information
@@ -817,8 +818,7 @@ impl Ident {
817
818
pub fn new_raw ( string : & str , span : Span ) -> Ident {
818
819
let mut ident = Ident :: new ( string, span) ;
819
820
if ident. sym == keywords:: Underscore . name ( ) ||
820
- token:: is_path_segment_keyword ( ast:: Ident :: with_empty_ctxt ( ident. sym ) ) ||
821
- ident. sym . as_str ( ) . starts_with ( "\' " ) {
821
+ token:: is_path_segment_keyword ( ast:: Ident :: with_empty_ctxt ( ident. sym ) ) {
822
822
panic ! ( "`{:?}` is not a valid raw identifier" , string)
823
823
}
824
824
ident. is_raw = true ;
@@ -1211,13 +1211,19 @@ impl TokenTree {
1211
1211
Pound => op ! ( '#' ) ,
1212
1212
Dollar => op ! ( '$' ) ,
1213
1213
Question => op ! ( '?' ) ,
1214
+ SingleQuote => op ! ( '\'' ) ,
1214
1215
1215
- Ident ( ident, false ) | Lifetime ( ident ) => {
1216
+ Ident ( ident, false ) => {
1216
1217
tt ! ( self :: Ident :: new( & ident. name. as_str( ) , Span ( span) ) )
1217
1218
}
1218
1219
Ident ( ident, true ) => {
1219
1220
tt ! ( self :: Ident :: new_raw( & ident. name. as_str( ) , Span ( span) ) )
1220
1221
}
1222
+ Lifetime ( ident) => {
1223
+ let ident = ident. without_first_quote ( ) ;
1224
+ stack. push ( tt ! ( self :: Ident :: new( & ident. name. as_str( ) , Span ( span) ) ) ) ;
1225
+ tt ! ( Punct :: new( '\'' , Spacing :: Joint ) )
1226
+ }
1221
1227
Literal ( lit, suffix) => tt ! ( self :: Literal { lit, suffix, span: Span ( span) } ) ,
1222
1228
DocComment ( c) => {
1223
1229
let style = comments:: doc_comment_style ( & c. as_str ( ) ) ;
@@ -1260,12 +1266,7 @@ impl TokenTree {
1260
1266
} ) . into ( ) ;
1261
1267
} ,
1262
1268
self :: TokenTree :: Ident ( tt) => {
1263
- let ident = ast:: Ident :: new ( tt. sym , tt. span . 0 ) ;
1264
- let token = if tt. sym . as_str ( ) . starts_with ( "'" ) {
1265
- Lifetime ( ident)
1266
- } else {
1267
- Ident ( ident, tt. is_raw )
1268
- } ;
1269
+ let token = Ident ( ast:: Ident :: new ( tt. sym , tt. span . 0 ) , tt. is_raw ) ;
1269
1270
return TokenTree :: Token ( tt. span . 0 , token) . into ( ) ;
1270
1271
}
1271
1272
self :: TokenTree :: Literal ( self :: Literal {
@@ -1324,6 +1325,7 @@ impl TokenTree {
1324
1325
'#' => Pound ,
1325
1326
'$' => Dollar ,
1326
1327
'?' => Question ,
1328
+ '\'' => SingleQuote ,
1327
1329
_ => unreachable ! ( ) ,
1328
1330
} ;
1329
1331
0 commit comments