@@ -3,7 +3,7 @@ use crate::pp::{self, Breaks};
33
44use rustc_span:: edition:: Edition ;
55use rustc_span:: source_map:: { SourceMap , Spanned } ;
6- use rustc_span:: symbol:: { kw, sym} ;
6+ use rustc_span:: symbol:: { kw, sym, IdentPrinter } ;
77use rustc_span:: { BytePos , FileName , Span } ;
88use syntax:: ast:: { self , BlockCheckMode , PatKind , RangeEnd , RangeSyntax } ;
99use syntax:: ast:: { Attribute , GenericArg , MacArgs } ;
@@ -196,40 +196,6 @@ pub fn literal_to_string(lit: token::Lit) -> String {
196196 out
197197}
198198
199- /// Print an ident from AST, `$crate` is converted into its respective crate name.
200- pub fn ast_ident_to_string ( ident : ast:: Ident , is_raw : bool ) -> String {
201- ident_to_string ( ident. name , is_raw, Some ( ident. span ) )
202- }
203-
204- // AST pretty-printer is used as a fallback for turning AST structures into token streams for
205- // proc macros. Additionally, proc macros may stringify their input and expect it survive the
206- // stringification (especially true for proc macro derives written between Rust 1.15 and 1.30).
207- // So we need to somehow pretty-print `$crate` in a way preserving at least some of its
208- // hygiene data, most importantly name of the crate it refers to.
209- // As a result we print `$crate` as `crate` if it refers to the local crate
210- // and as `::other_crate_name` if it refers to some other crate.
211- // Note, that this is only done if the ident token is printed from inside of AST pretty-pringing,
212- // but not otherwise. Pretty-printing is the only way for proc macros to discover token contents,
213- // so we should not perform this lossy conversion if the top level call to the pretty-printer was
214- // done for a token stream or a single token.
215- fn ident_to_string ( name : ast:: Name , is_raw : bool , convert_dollar_crate : Option < Span > ) -> String {
216- if is_raw {
217- format ! ( "r#{}" , name)
218- } else {
219- if name == kw:: DollarCrate {
220- if let Some ( span) = convert_dollar_crate {
221- let converted = span. ctxt ( ) . dollar_crate_name ( ) ;
222- return if converted. is_path_segment_keyword ( ) {
223- converted. to_string ( )
224- } else {
225- format ! ( "::{}" , converted)
226- } ;
227- }
228- }
229- name. to_string ( )
230- }
231- }
232-
233199/// Print the token kind precisely, without converting `$crate` into its respective crate name.
234200pub fn token_kind_to_string ( tok : & TokenKind ) -> String {
235201 token_kind_to_string_ext ( tok, None )
@@ -280,7 +246,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
280246 token:: Literal ( lit) => literal_to_string ( lit) ,
281247
282248 /* Name components */
283- token:: Ident ( s, is_raw) => ident_to_string ( s, is_raw, convert_dollar_crate) ,
249+ token:: Ident ( s, is_raw) => IdentPrinter :: new ( s, is_raw, convert_dollar_crate) . to_string ( ) ,
284250 token:: Lifetime ( s) => s. to_string ( ) ,
285251
286252 /* Other */
@@ -315,7 +281,7 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
315281 token:: NtBlock ( ref e) => block_to_string ( e) ,
316282 token:: NtStmt ( ref e) => stmt_to_string ( e) ,
317283 token:: NtPat ( ref e) => pat_to_string ( e) ,
318- token:: NtIdent ( e, is_raw) => ast_ident_to_string ( e, is_raw) ,
284+ token:: NtIdent ( e, is_raw) => IdentPrinter :: for_ast_ident ( e, is_raw) . to_string ( ) ,
319285 token:: NtLifetime ( e) => e. to_string ( ) ,
320286 token:: NtLiteral ( ref e) => expr_to_string ( e) ,
321287 token:: NtTT ( ref tree) => tt_to_string ( tree. clone ( ) ) ,
@@ -819,7 +785,7 @@ impl<'a> PrintState<'a> for State<'a> {
819785 }
820786
821787 fn print_ident ( & mut self , ident : ast:: Ident ) {
822- self . s . word ( ast_ident_to_string ( ident, ident. is_raw_guess ( ) ) ) ;
788+ self . s . word ( IdentPrinter :: for_ast_ident ( ident, ident. is_raw_guess ( ) ) . to_string ( ) ) ;
823789 self . ann . post ( self , AnnNode :: Ident ( & ident) )
824790 }
825791
0 commit comments