@@ -13,9 +13,9 @@ use rustc_data_structures::fx::FxHashMap;
13
13
use rustc_data_structures:: stable_hasher:: StableHasher ;
14
14
use rustc_index:: vec:: IndexVec ;
15
15
use rustc_span:: hygiene:: ExpnId ;
16
- use rustc_span:: symbol:: { sym, Symbol } ;
16
+ use rustc_span:: symbol:: { kw , sym, Symbol } ;
17
17
18
- use std:: fmt:: Write ;
18
+ use std:: fmt:: { self , Write } ;
19
19
use std:: hash:: Hash ;
20
20
use tracing:: debug;
21
21
@@ -155,6 +155,29 @@ pub struct DisambiguatedDefPathData {
155
155
pub disambiguator : u32 ,
156
156
}
157
157
158
+ impl DisambiguatedDefPathData {
159
+ pub fn fmt_maybe_verbose ( & self , writer : & mut impl Write , verbose : bool ) -> fmt:: Result {
160
+ match self . data . name ( ) {
161
+ DefPathDataName :: Named ( name) => {
162
+ if verbose && self . disambiguator != 0 {
163
+ write ! ( writer, "{}#{}" , name, self . disambiguator)
164
+ } else {
165
+ writer. write_str ( & name. as_str ( ) )
166
+ }
167
+ }
168
+ DefPathDataName :: Anon { namespace } => {
169
+ write ! ( writer, "{{{}#{}}}" , namespace, self . disambiguator)
170
+ }
171
+ }
172
+ }
173
+ }
174
+
175
+ impl fmt:: Display for DisambiguatedDefPathData {
176
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
177
+ self . fmt_maybe_verbose ( f, true )
178
+ }
179
+ }
180
+
158
181
#[ derive( Clone , Debug , Encodable , Decodable ) ]
159
182
pub struct DefPath {
160
183
/// The path leading from the crate root to the item.
@@ -198,33 +221,11 @@ impl DefPath {
198
221
/// Returns a string representation of the `DefPath` without
199
222
/// the crate-prefix. This method is useful if you don't have
200
223
/// a `TyCtxt` available.
201
- pub fn to_string_no_crate ( & self ) -> String {
224
+ pub fn to_string_no_crate_verbose ( & self ) -> String {
202
225
let mut s = String :: with_capacity ( self . data . len ( ) * 16 ) ;
203
226
204
227
for component in & self . data {
205
- write ! ( s, "::{}[{}]" , component. data. as_symbol( ) , component. disambiguator) . unwrap ( ) ;
206
- }
207
-
208
- s
209
- }
210
-
211
- /// Returns a filename-friendly string for the `DefPath`, with the
212
- /// crate-prefix.
213
- pub fn to_string_friendly < F > ( & self , crate_imported_name : F ) -> String
214
- where
215
- F : FnOnce ( CrateNum ) -> Symbol ,
216
- {
217
- let crate_name_str = crate_imported_name ( self . krate ) . as_str ( ) ;
218
- let mut s = String :: with_capacity ( crate_name_str. len ( ) + self . data . len ( ) * 16 ) ;
219
-
220
- write ! ( s, "::{}" , crate_name_str) . unwrap ( ) ;
221
-
222
- for component in & self . data {
223
- if component. disambiguator == 0 {
224
- write ! ( s, "::{}" , component. data. as_symbol( ) ) . unwrap ( ) ;
225
- } else {
226
- write ! ( s, "{}[{}]" , component. data. as_symbol( ) , component. disambiguator) . unwrap ( ) ;
227
- }
228
+ write ! ( s, "::{}" , component) . unwrap ( ) ;
228
229
}
229
230
230
231
s
@@ -240,12 +241,9 @@ impl DefPath {
240
241
for component in & self . data {
241
242
s. extend ( opt_delimiter) ;
242
243
opt_delimiter = Some ( '-' ) ;
243
- if component. disambiguator == 0 {
244
- write ! ( s, "{}" , component. data. as_symbol( ) ) . unwrap ( ) ;
245
- } else {
246
- write ! ( s, "{}[{}]" , component. data. as_symbol( ) , component. disambiguator) . unwrap ( ) ;
247
- }
244
+ write ! ( s, "{}" , component) . unwrap ( ) ;
248
245
}
246
+
249
247
s
250
248
}
251
249
}
@@ -427,6 +425,12 @@ impl Definitions {
427
425
}
428
426
}
429
427
428
+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
429
+ pub enum DefPathDataName {
430
+ Named ( Symbol ) ,
431
+ Anon { namespace : Symbol } ,
432
+ }
433
+
430
434
impl DefPathData {
431
435
pub fn get_opt_name ( & self ) -> Option < Symbol > {
432
436
use self :: DefPathData :: * ;
@@ -437,22 +441,30 @@ impl DefPathData {
437
441
}
438
442
}
439
443
440
- pub fn as_symbol ( & self ) -> Symbol {
444
+ pub fn name ( & self ) -> DefPathDataName {
441
445
use self :: DefPathData :: * ;
442
446
match * self {
443
- TypeNs ( name) | ValueNs ( name) | MacroNs ( name) | LifetimeNs ( name) => name,
447
+ TypeNs ( name) | ValueNs ( name) | MacroNs ( name) | LifetimeNs ( name) => {
448
+ DefPathDataName :: Named ( name)
449
+ }
444
450
// Note that this does not show up in user print-outs.
445
- CrateRoot => sym :: double_braced_crate ,
446
- Impl => sym :: double_braced_impl ,
447
- Misc => sym:: double_braced_misc ,
448
- ClosureExpr => sym:: double_braced_closure ,
449
- Ctor => sym:: double_braced_constructor ,
450
- AnonConst => sym:: double_braced_constant ,
451
- ImplTrait => sym:: double_braced_opaque ,
451
+ CrateRoot => DefPathDataName :: Anon { namespace : kw :: Crate } ,
452
+ Impl => DefPathDataName :: Anon { namespace : kw :: Impl } ,
453
+ Misc => DefPathDataName :: Anon { namespace : sym:: misc } ,
454
+ ClosureExpr => DefPathDataName :: Anon { namespace : sym:: closure } ,
455
+ Ctor => DefPathDataName :: Anon { namespace : sym:: constructor } ,
456
+ AnonConst => DefPathDataName :: Anon { namespace : sym:: constant } ,
457
+ ImplTrait => DefPathDataName :: Anon { namespace : sym:: opaque } ,
452
458
}
453
459
}
460
+ }
454
461
455
- pub fn to_string ( & self ) -> String {
456
- self . as_symbol ( ) . to_string ( )
462
+ impl fmt:: Display for DefPathData {
463
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
464
+ match self . name ( ) {
465
+ DefPathDataName :: Named ( name) => f. write_str ( & name. as_str ( ) ) ,
466
+ // FIXME(#70334): this will generate legacy {{closure}}, {{impl}}, etc
467
+ DefPathDataName :: Anon { namespace } => write ! ( f, "{{{{{}}}}}" , namespace) ,
468
+ }
457
469
}
458
470
}
0 commit comments