@@ -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,7 +281,7 @@ 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 ( ) ) ,
@@ -819,7 +785,7 @@ impl<'a> PrintState<'a> for State<'a> {
819
785
}
820
786
821
787
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 ( ) ) ;
823
789
self . ann . post ( self , AnnNode :: Ident ( & ident) )
824
790
}
825
791
0 commit comments