@@ -187,8 +187,9 @@ pub fn literal_to_string(lit: token::Lit) -> String {
187187 out
188188}
189189
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 ) )
192193}
193194
194195// 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 {
202203// but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
203204// so we should not perform this lossy conversion if the top level call to the pretty-printer was
204205// 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 {
208207 if is_raw {
209208 format ! ( "r#{}" , name)
210209 } else {
@@ -222,6 +221,7 @@ fn ident_to_string_ext(
222221 }
223222}
224223
224+ /// Print the token kind precisely, without converting `$crate` into its respective crate name.
225225pub fn token_kind_to_string ( tok : & TokenKind ) -> String {
226226 token_kind_to_string_ext ( tok, None )
227227}
@@ -272,7 +272,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
272272 token:: Literal ( lit) => literal_to_string ( lit) ,
273273
274274 /* 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) ,
276276 token:: Lifetime ( s) => s. to_string ( ) ,
277277
278278 /* Other */
@@ -286,6 +286,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
286286 }
287287}
288288
289+ /// Print the token precisely, without converting `$crate` into its respective crate name.
289290pub fn token_to_string ( token : & Token ) -> String {
290291 token_to_string_ext ( token, false )
291292}
@@ -305,7 +306,7 @@ crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
305306 token:: NtBlock ( ref e) => block_to_string ( e) ,
306307 token:: NtStmt ( ref e) => stmt_to_string ( e) ,
307308 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) ,
309310 token:: NtLifetime ( e) => e. to_string ( ) ,
310311 token:: NtLiteral ( ref e) => expr_to_string ( e) ,
311312 token:: NtTT ( ref tree) => tt_to_string ( tree. clone ( ) ) ,
@@ -341,7 +342,7 @@ pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String {
341342}
342343
343344pub 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 ) )
345346}
346347
347348pub 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
601602 self . word ( "::" ) ;
602603 }
603604 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 ( ) ) ) ;
605606 }
606607 }
607608 }
@@ -629,7 +630,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
629630 } else {
630631 self . print_attribute_path ( & attr. path ) ;
631632 self . space ( ) ;
632- self . print_tts ( attr. tokens . clone ( ) ) ;
633+ self . print_tts ( attr. tokens . clone ( ) , true ) ;
633634 }
634635 self . word ( "]" ) ;
635636 }
@@ -689,18 +690,14 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
689690 TokenTree :: Delimited ( _, delim, tts) => {
690691 self . word ( token_kind_to_string ( & token:: OpenDelim ( delim) ) ) ;
691692 self . space ( ) ;
692- self . print_tts ( tts) ;
693+ self . print_tts ( tts, convert_dollar_crate ) ;
693694 self . space ( ) ;
694695 self . word ( token_kind_to_string ( & token:: CloseDelim ( delim) ) )
695696 } ,
696697 }
697698 }
698699
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 ) {
704701 self . ibox ( 0 ) ;
705702 for ( i, tt) in tts. into_trees ( ) . enumerate ( ) {
706703 if i != 0 {
@@ -1247,7 +1244,7 @@ impl<'a> State<'a> {
12471244 self . print_ident ( item. ident ) ;
12481245 self . cbox ( INDENT_UNIT ) ;
12491246 self . popen ( ) ;
1250- self . print_tts ( mac. node . stream ( ) ) ;
1247+ self . print_tts ( mac. node . stream ( ) , true ) ;
12511248 self . pclose ( ) ;
12521249 self . s . word ( ";" ) ;
12531250 self . end ( ) ;
@@ -1258,7 +1255,7 @@ impl<'a> State<'a> {
12581255 self . print_ident ( item. ident ) ;
12591256 self . cbox ( INDENT_UNIT ) ;
12601257 self . popen ( ) ;
1261- self . print_tts ( tts. stream ( ) ) ;
1258+ self . print_tts ( tts. stream ( ) , true ) ;
12621259 self . pclose ( ) ;
12631260 self . s . word ( ";" ) ;
12641261 self . end ( ) ;
@@ -1659,7 +1656,7 @@ impl<'a> State<'a> {
16591656 self . bopen ( ) ;
16601657 }
16611658 }
1662- self . print_tts ( m. node . stream ( ) ) ;
1659+ self . print_tts ( m. node . stream ( ) , true ) ;
16631660 match m. node . delim {
16641661 MacDelimiter :: Parenthesis => self . pclose ( ) ,
16651662 MacDelimiter :: Bracket => self . s . word ( "]" ) ,
@@ -2209,7 +2206,7 @@ impl<'a> State<'a> {
22092206 }
22102207
22112208 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 ( ) ) ) ;
22132210 self . ann . post ( self , AnnNode :: Ident ( & ident) )
22142211 }
22152212
0 commit comments