@@ -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,14 +281,11 @@ 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 ( ) ) ,
322- // FIXME(Centril): merge these variants.
323- token:: NtImplItem ( ref e) | token:: NtTraitItem ( ref e) => assoc_item_to_string ( e) ,
324288 token:: NtVis ( ref e) => vis_to_string ( e) ,
325- token:: NtForeignItem ( ref e) => foreign_item_to_string ( e) ,
326289 }
327290}
328291
@@ -358,10 +321,6 @@ pub fn item_to_string(i: &ast::Item) -> String {
358321 to_string ( |s| s. print_item ( i) )
359322}
360323
361- fn assoc_item_to_string ( i : & ast:: AssocItem ) -> String {
362- to_string ( |s| s. print_assoc_item ( i) )
363- }
364-
365324pub fn generic_params_to_string ( generic_params : & [ ast:: GenericParam ] ) -> String {
366325 to_string ( |s| s. print_generic_params ( generic_params) )
367326}
@@ -404,10 +363,6 @@ pub fn param_to_string(arg: &ast::Param) -> String {
404363 to_string ( |s| s. print_param ( arg, false ) )
405364}
406365
407- fn foreign_item_to_string ( arg : & ast:: ForeignItem ) -> String {
408- to_string ( |s| s. print_foreign_item ( arg) )
409- }
410-
411366fn visibility_qualified ( vis : & ast:: Visibility , s : & str ) -> String {
412367 format ! ( "{}{}" , to_string( |s| s. print_visibility( vis) ) , s)
413368}
@@ -819,7 +774,7 @@ impl<'a> PrintState<'a> for State<'a> {
819774 }
820775
821776 fn print_ident ( & mut self , ident : ast:: Ident ) {
822- self . s . word ( ast_ident_to_string ( ident, ident. is_raw_guess ( ) ) ) ;
777+ self . s . word ( IdentPrinter :: for_ast_ident ( ident, ident. is_raw_guess ( ) ) . to_string ( ) ) ;
823778 self . ann . post ( self , AnnNode :: Ident ( & ident) )
824779 }
825780
0 commit comments