@@ -13,9 +13,9 @@ use rustc_data_structures::fx::FxHashMap;
1313use rustc_data_structures:: stable_hasher:: StableHasher ;
1414use rustc_index:: vec:: IndexVec ;
1515use rustc_span:: hygiene:: ExpnId ;
16- use rustc_span:: symbol:: { sym, Symbol } ;
16+ use rustc_span:: symbol:: { kw , sym, Symbol } ;
1717
18- use std:: fmt:: Write ;
18+ use std:: fmt:: { self , Write } ;
1919use std:: hash:: Hash ;
2020use tracing:: debug;
2121
@@ -155,6 +155,29 @@ pub struct DisambiguatedDefPathData {
155155 pub disambiguator : u32 ,
156156}
157157
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+
158181#[ derive( Clone , Debug , Encodable , Decodable ) ]
159182pub struct DefPath {
160183 /// The path leading from the crate root to the item.
@@ -198,33 +221,11 @@ impl DefPath {
198221 /// Returns a string representation of the `DefPath` without
199222 /// the crate-prefix. This method is useful if you don't have
200223 /// a `TyCtxt` available.
201- pub fn to_string_no_crate ( & self ) -> String {
224+ pub fn to_string_no_crate_verbose ( & self ) -> String {
202225 let mut s = String :: with_capacity ( self . data . len ( ) * 16 ) ;
203226
204227 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 ( ) ;
228229 }
229230
230231 s
@@ -240,12 +241,9 @@ impl DefPath {
240241 for component in & self . data {
241242 s. extend ( opt_delimiter) ;
242243 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 ( ) ;
248245 }
246+
249247 s
250248 }
251249}
@@ -427,6 +425,12 @@ impl Definitions {
427425 }
428426}
429427
428+ #[ derive( Copy , Clone , PartialEq , Debug ) ]
429+ pub enum DefPathDataName {
430+ Named ( Symbol ) ,
431+ Anon { namespace : Symbol } ,
432+ }
433+
430434impl DefPathData {
431435 pub fn get_opt_name ( & self ) -> Option < Symbol > {
432436 use self :: DefPathData :: * ;
@@ -437,22 +441,30 @@ impl DefPathData {
437441 }
438442 }
439443
440- pub fn as_symbol ( & self ) -> Symbol {
444+ pub fn name ( & self ) -> DefPathDataName {
441445 use self :: DefPathData :: * ;
442446 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+ }
444450 // 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 } ,
452458 }
453459 }
460+ }
454461
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+ }
457469 }
458470}
0 commit comments