@@ -187,8 +187,9 @@ pub fn literal_to_string(lit: token::Lit) -> String {
187
187
out
188
188
}
189
189
190
- fn ident_to_string ( ident : ast:: Ident , is_raw : bool ) -> String {
191
- ident_to_string_ext ( ident. name , is_raw, Some ( ident. span ) )
190
+ /// Print an ident from AST, `$crate` is converted into its respective crate name.
191
+ fn ast_ident_to_string ( ident : ast:: Ident , is_raw : bool ) -> String {
192
+ ident_to_string ( ident. name , is_raw, Some ( ident. span ) )
192
193
}
193
194
194
195
// AST pretty-printer is used as a fallback for turning AST structures into token streams for
@@ -202,9 +203,7 @@ fn ident_to_string(ident: ast::Ident, is_raw: bool) -> String {
202
203
// but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
203
204
// so we should not perform this lossy conversion if the top level call to the pretty-printer was
204
205
// done for a token stream or a single token.
205
- fn ident_to_string_ext (
206
- name : ast:: Name , is_raw : bool , convert_dollar_crate : Option < Span >
207
- ) -> String {
206
+ fn ident_to_string ( name : ast:: Name , is_raw : bool , convert_dollar_crate : Option < Span > ) -> String {
208
207
if is_raw {
209
208
format ! ( "r#{}" , name)
210
209
} else {
@@ -222,6 +221,7 @@ fn ident_to_string_ext(
222
221
}
223
222
}
224
223
224
+ /// Print the token kind precisely, without converting `$crate` into its respective crate name.
225
225
pub fn token_kind_to_string ( tok : & TokenKind ) -> String {
226
226
token_kind_to_string_ext ( tok, None )
227
227
}
@@ -272,7 +272,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
272
272
token:: Literal ( lit) => literal_to_string ( lit) ,
273
273
274
274
/* Name components */
275
- token:: Ident ( s, is_raw) => ident_to_string_ext ( s, is_raw, convert_dollar_crate) ,
275
+ token:: Ident ( s, is_raw) => ident_to_string ( s, is_raw, convert_dollar_crate) ,
276
276
token:: Lifetime ( s) => s. to_string ( ) ,
277
277
278
278
/* Other */
@@ -286,6 +286,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
286
286
}
287
287
}
288
288
289
+ /// Print the token precisely, without converting `$crate` into its respective crate name.
289
290
pub fn token_to_string ( token : & Token ) -> String {
290
291
token_to_string_ext ( token, false )
291
292
}
@@ -305,7 +306,7 @@ crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
305
306
token:: NtBlock ( ref e) => block_to_string ( e) ,
306
307
token:: NtStmt ( ref e) => stmt_to_string ( e) ,
307
308
token:: NtPat ( ref e) => pat_to_string ( e) ,
308
- token:: NtIdent ( e, is_raw) => ident_to_string ( e, is_raw) ,
309
+ token:: NtIdent ( e, is_raw) => ast_ident_to_string ( e, is_raw) ,
309
310
token:: NtLifetime ( e) => e. to_string ( ) ,
310
311
token:: NtLiteral ( ref e) => expr_to_string ( e) ,
311
312
token:: NtTT ( ref tree) => tt_to_string ( tree. clone ( ) ) ,
@@ -341,7 +342,7 @@ pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String {
341
342
}
342
343
343
344
pub fn tokens_to_string ( tokens : TokenStream ) -> String {
344
- to_string ( |s| s. print_tts_ext ( tokens, false ) )
345
+ to_string ( |s| s. print_tts ( tokens, false ) )
345
346
}
346
347
347
348
pub fn stmt_to_string ( stmt : & ast:: Stmt ) -> String {
@@ -601,7 +602,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
601
602
self . word ( "::" ) ;
602
603
}
603
604
if segment. ident . name != kw:: PathRoot {
604
- self . word ( ident_to_string ( segment. ident , segment. ident . is_raw_guess ( ) ) ) ;
605
+ self . word ( ast_ident_to_string ( segment. ident , segment. ident . is_raw_guess ( ) ) ) ;
605
606
}
606
607
}
607
608
}
@@ -629,7 +630,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
629
630
} else {
630
631
self . print_attribute_path ( & attr. path ) ;
631
632
self . space ( ) ;
632
- self . print_tts ( attr. tokens . clone ( ) ) ;
633
+ self . print_tts ( attr. tokens . clone ( ) , true ) ;
633
634
}
634
635
self . word ( "]" ) ;
635
636
}
@@ -689,18 +690,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
689
690
TokenTree :: Delimited ( _, delim, tts) => {
690
691
self . word ( token_kind_to_string ( & token:: OpenDelim ( delim) ) ) ;
691
692
self . space ( ) ;
692
- self . print_tts ( tts) ;
693
+ self . print_tts ( tts, convert_dollar_crate ) ;
693
694
self . space ( ) ;
694
695
self . word ( token_kind_to_string ( & token:: CloseDelim ( delim) ) )
695
696
} ,
696
697
}
697
698
}
698
699
699
- fn print_tts ( & mut self , tts : tokenstream:: TokenStream ) {
700
- self . print_tts_ext ( tts, true )
701
- }
702
-
703
- fn print_tts_ext ( & mut self , tts : tokenstream:: TokenStream , convert_dollar_crate : bool ) {
700
+ fn print_tts ( & mut self , tts : tokenstream:: TokenStream , convert_dollar_crate : bool ) {
704
701
self . ibox ( 0 ) ;
705
702
for ( i, tt) in tts. into_trees ( ) . enumerate ( ) {
706
703
if i != 0 {
@@ -1247,7 +1244,7 @@ impl<'a> State<'a> {
1247
1244
self . print_ident ( item. ident ) ;
1248
1245
self . cbox ( INDENT_UNIT ) ;
1249
1246
self . popen ( ) ;
1250
- self . print_tts ( mac. node . stream ( ) ) ;
1247
+ self . print_tts ( mac. node . stream ( ) , true ) ;
1251
1248
self . pclose ( ) ;
1252
1249
self . s . word ( ";" ) ;
1253
1250
self . end ( ) ;
@@ -1258,7 +1255,7 @@ impl<'a> State<'a> {
1258
1255
self . print_ident ( item. ident ) ;
1259
1256
self . cbox ( INDENT_UNIT ) ;
1260
1257
self . popen ( ) ;
1261
- self . print_tts ( tts. stream ( ) ) ;
1258
+ self . print_tts ( tts. stream ( ) , true ) ;
1262
1259
self . pclose ( ) ;
1263
1260
self . s . word ( ";" ) ;
1264
1261
self . end ( ) ;
@@ -1659,7 +1656,7 @@ impl<'a> State<'a> {
1659
1656
self . bopen ( ) ;
1660
1657
}
1661
1658
}
1662
- self . print_tts ( m. node . stream ( ) ) ;
1659
+ self . print_tts ( m. node . stream ( ) , true ) ;
1663
1660
match m. node . delim {
1664
1661
MacDelimiter :: Parenthesis => self . pclose ( ) ,
1665
1662
MacDelimiter :: Bracket => self . s . word ( "]" ) ,
@@ -2209,7 +2206,7 @@ impl<'a> State<'a> {
2209
2206
}
2210
2207
2211
2208
crate fn print_ident ( & mut self , ident : ast:: Ident ) {
2212
- self . s . word ( ident_to_string ( ident, ident. is_raw_guess ( ) ) ) ;
2209
+ self . s . word ( ast_ident_to_string ( ident, ident. is_raw_guess ( ) ) ) ;
2213
2210
self . ann . post ( self , AnnNode :: Ident ( & ident) )
2214
2211
}
2215
2212
0 commit comments