@@ -797,12 +797,6 @@ impl fmt::Display for Punct {
797
797
/// REVIEW We should guarantee that `Ident` contains a valid identifier permitted by
798
798
/// REVIEW the language and not a random unicode string, at least for a start.
799
799
///
800
- /// REVIEW We need to support raw identifiers here (`r#ident`) or at least be future compatible
801
- /// REVIEW with them. Currently they are supported using "string typing" - if string "r#ident" is
802
- /// REVIEW passed to `Ident::new` it will be interpreted as a raw identifier later on, we should add
803
- /// REVIEW a field `is_raw` and a separate constructor for it (`Ident::new_raw` or something) and
804
- /// REVIEW keep it unstable until raw identifiers are stabilized.
805
- ///
806
800
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
807
801
/// REVIEW Do we want to guarantee `Ident` to be `Copy`?
808
802
#[ derive( Copy , Clone , Debug ) ]
@@ -811,6 +805,7 @@ pub struct Ident {
811
805
// REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
812
806
sym : Symbol ,
813
807
span : Span ,
808
+ is_raw : bool ,
814
809
}
815
810
816
811
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
@@ -844,9 +839,18 @@ impl Ident {
844
839
Ident {
845
840
sym : Symbol :: intern ( string) ,
846
841
span,
842
+ is_raw : false ,
847
843
}
848
844
}
849
845
846
+ /// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
847
+ #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
848
+ pub fn new_raw ( string : & str , span : Span ) -> Ident {
849
+ let mut ident = Ident :: new ( string, span) ;
850
+ ident. is_raw = true ;
851
+ ident
852
+ }
853
+
850
854
// FIXME: Remove this, do not stabilize
851
855
/// Get a reference to the interned string.
852
856
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
@@ -1230,7 +1234,7 @@ impl TokenTree {
1230
1234
tt ! ( self :: Ident :: new( & ident. name. as_str( ) , Span ( span) ) )
1231
1235
}
1232
1236
Ident ( ident, true ) => {
1233
- tt ! ( self :: Ident :: new ( & format! ( "r#{}" , ident) , Span ( span) ) )
1237
+ tt ! ( self :: Ident :: new_raw ( & ident. name . as_str ( ) , Span ( span) ) )
1234
1238
}
1235
1239
Literal ( lit, suffix) => tt ! ( self :: Literal { lit, suffix, span: Span ( span) } ) ,
1236
1240
DocComment ( c) => {
@@ -1275,15 +1279,10 @@ impl TokenTree {
1275
1279
} ,
1276
1280
self :: TokenTree :: Ident ( tt) => {
1277
1281
let ident = ast:: Ident :: new ( tt. sym , tt. span . 0 ) ;
1278
- let sym_str = tt. sym . to_string ( ) ;
1279
- let token = if sym_str. starts_with ( "'" ) {
1282
+ let token = if tt. sym . as_str ( ) . starts_with ( "'" ) {
1280
1283
Lifetime ( ident)
1281
- } else if sym_str. starts_with ( "r#" ) {
1282
- let name = Symbol :: intern ( & sym_str[ 2 ..] ) ;
1283
- let ident = ast:: Ident :: new ( name, ident. span ) ;
1284
- Ident ( ident, true )
1285
1284
} else {
1286
- Ident ( ident, false )
1285
+ Ident ( ident, tt . is_raw )
1287
1286
} ;
1288
1287
return TokenTree :: Token ( tt. span . 0 , token) . into ( ) ;
1289
1288
}
0 commit comments