@@ -3,7 +3,7 @@ use crate::pp::{self, Breaks};
3
3
4
4
use rustc_span:: edition:: Edition ;
5
5
use rustc_span:: source_map:: { SourceMap , Spanned } ;
6
- use rustc_span:: symbol:: { kw, sym} ;
6
+ use rustc_span:: symbol:: { kw, sym, IdentPrinter } ;
7
7
use rustc_span:: { BytePos , FileName , Span } ;
8
8
use syntax:: ast:: { self , BlockCheckMode , PatKind , RangeEnd , RangeSyntax } ;
9
9
use syntax:: ast:: { Attribute , GenericArg , MacArgs } ;
@@ -196,40 +196,6 @@ pub fn literal_to_string(lit: token::Lit) -> String {
196
196
out
197
197
}
198
198
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
-
233
199
/// Print the token kind precisely, without converting `$crate` into its respective crate name.
234
200
pub fn token_kind_to_string ( tok : & TokenKind ) -> String {
235
201
token_kind_to_string_ext ( tok, None )
@@ -280,7 +246,7 @@ fn token_kind_to_string_ext(tok: &TokenKind, convert_dollar_crate: Option<Span>)
280
246
token:: Literal ( lit) => literal_to_string ( lit) ,
281
247
282
248
/* 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 ( ) ,
284
250
token:: Lifetime ( s) => s. to_string ( ) ,
285
251
286
252
/* Other */
@@ -315,14 +281,11 @@ pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
315
281
token:: NtBlock ( ref e) => block_to_string ( e) ,
316
282
token:: NtStmt ( ref e) => stmt_to_string ( e) ,
317
283
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 ( ) ,
319
285
token:: NtLifetime ( e) => e. to_string ( ) ,
320
286
token:: NtLiteral ( ref e) => expr_to_string ( e) ,
321
287
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) ,
324
288
token:: NtVis ( ref e) => vis_to_string ( e) ,
325
- token:: NtForeignItem ( ref e) => foreign_item_to_string ( e) ,
326
289
}
327
290
}
328
291
@@ -358,10 +321,6 @@ pub fn item_to_string(i: &ast::Item) -> String {
358
321
to_string ( |s| s. print_item ( i) )
359
322
}
360
323
361
- fn assoc_item_to_string ( i : & ast:: AssocItem ) -> String {
362
- to_string ( |s| s. print_assoc_item ( i) )
363
- }
364
-
365
324
pub fn generic_params_to_string ( generic_params : & [ ast:: GenericParam ] ) -> String {
366
325
to_string ( |s| s. print_generic_params ( generic_params) )
367
326
}
@@ -404,10 +363,6 @@ pub fn param_to_string(arg: &ast::Param) -> String {
404
363
to_string ( |s| s. print_param ( arg, false ) )
405
364
}
406
365
407
- fn foreign_item_to_string ( arg : & ast:: ForeignItem ) -> String {
408
- to_string ( |s| s. print_foreign_item ( arg) )
409
- }
410
-
411
366
fn visibility_qualified ( vis : & ast:: Visibility , s : & str ) -> String {
412
367
format ! ( "{}{}" , to_string( |s| s. print_visibility( vis) ) , s)
413
368
}
@@ -819,7 +774,7 @@ impl<'a> PrintState<'a> for State<'a> {
819
774
}
820
775
821
776
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 ( ) ) ;
823
778
self . ann . post ( self , AnnNode :: Ident ( & ident) )
824
779
}
825
780
0 commit comments