@@ -68,7 +68,7 @@ impl DefPathTable {
6868 //
6969 // See the documentation for DefPathHash for more information.
7070 panic ! (
71- "found DefPathHash collision between {def_path1:?} and {def_path2:?}. \
71+ "found DefPathHash collision between {def_path1:# ?} and {def_path2:# ?}. \
7272 Compilation cannot continue."
7373 ) ;
7474 }
@@ -97,13 +97,31 @@ impl DefPathTable {
9797 }
9898}
9999
100+ #[ derive( Debug ) ]
101+ pub struct DisambiguatorState {
102+ next : UnordMap < ( LocalDefId , DefPathData ) , u32 > ,
103+ }
104+
105+ impl DisambiguatorState {
106+ pub fn new ( ) -> Self {
107+ Self { next : Default :: default ( ) }
108+ }
109+
110+ /// Creates a `DisambiguatorState` where the next allocated `(LocalDefId, DefPathData)` pair
111+ /// will have `index` as the disambiguator.
112+ pub fn with ( def_id : LocalDefId , data : DefPathData , index : u32 ) -> Self {
113+ let mut this = Self :: new ( ) ;
114+ this. next . insert ( ( def_id, data) , index) ;
115+ this
116+ }
117+ }
118+
100119/// The definition table containing node definitions.
101120/// It holds the `DefPathTable` for `LocalDefId`s/`DefPath`s.
102121/// It also stores mappings to convert `LocalDefId`s to/from `HirId`s.
103122#[ derive( Debug ) ]
104123pub struct Definitions {
105124 table : DefPathTable ,
106- next_disambiguator : UnordMap < ( LocalDefId , DefPathData ) , u32 > ,
107125}
108126
109127/// A unique identifier that we can use to lookup a definition
@@ -173,7 +191,11 @@ impl DisambiguatedDefPathData {
173191 }
174192 }
175193 DefPathDataName :: Anon { namespace } => {
176- write ! ( writer, "{{{}#{}}}" , namespace, self . disambiguator)
194+ if let DefPathData :: AnonAssocTy ( method) = self . data {
195+ write ! ( writer, "{}::{{{}#{}}}" , method, namespace, self . disambiguator)
196+ } else {
197+ write ! ( writer, "{{{}#{}}}" , namespace, self . disambiguator)
198+ }
177199 }
178200 }
179201 }
@@ -288,7 +310,7 @@ pub enum DefPathData {
288310 /// Argument position `impl Trait` have a `TypeNs` with their pretty-printed name.
289311 OpaqueTy ,
290312 /// An anonymous associated type from an RPITIT.
291- AnonAssocTy ,
313+ AnonAssocTy ( Symbol ) ,
292314 /// A synthetic body for a coroutine's by-move body.
293315 SyntheticCoroutineBody ,
294316}
@@ -342,24 +364,33 @@ impl Definitions {
342364 let root = LocalDefId { local_def_index : table. allocate ( key, def_path_hash) } ;
343365 assert_eq ! ( root. local_def_index, CRATE_DEF_INDEX ) ;
344366
345- Definitions { table, next_disambiguator : Default :: default ( ) }
367+ Definitions { table }
346368 }
347369
348- /// Adds a definition with a parent definition.
349- pub fn create_def ( & mut self , parent : LocalDefId , data : DefPathData ) -> LocalDefId {
370+ /// Creates a definition with a parent definition.
371+ /// If there are multiple definitions with the same DefPathData and the same parent, use
372+ /// `disambiguator` to differentiate them. Distinct `DisambiguatorState` instances are not
373+ /// guaranteed to generate unique disambiguators and should instead ensure that the `parent`
374+ /// and `data` pair is distinct from other instances.
375+ pub fn create_def (
376+ & mut self ,
377+ parent : LocalDefId ,
378+ data : DefPathData ,
379+ disambiguator : & mut DisambiguatorState ,
380+ ) -> LocalDefId {
350381 // We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
351382 // reference to `Definitions` and we're already holding a mutable reference.
352383 debug ! (
353384 "create_def(parent={}, data={data:?})" ,
354385 self . def_path( parent) . to_string_no_crate_verbose( ) ,
355386 ) ;
356387
357- // The root node must be created with `create_root_def ()`.
388+ // The root node must be created in `new ()`.
358389 assert ! ( data != DefPathData :: CrateRoot ) ;
359390
360391 // Find the next free disambiguator for this key.
361392 let disambiguator = {
362- let next_disamb = self . next_disambiguator . entry ( ( parent, data) ) . or_insert ( 0 ) ;
393+ let next_disamb = disambiguator . next . entry ( ( parent, data) ) . or_insert ( 0 ) ;
363394 let disambiguator = * next_disamb;
364395 * next_disamb = next_disamb. checked_add ( 1 ) . expect ( "disambiguator overflow" ) ;
365396 disambiguator
@@ -411,7 +442,9 @@ impl DefPathData {
411442 pub fn get_opt_name ( & self ) -> Option < Symbol > {
412443 use self :: DefPathData :: * ;
413444 match * self {
414- TypeNs ( name) | ValueNs ( name) | MacroNs ( name) | LifetimeNs ( name) => Some ( name) ,
445+ TypeNs ( name) | ValueNs ( name) | MacroNs ( name) | LifetimeNs ( name) | AnonAssocTy ( name) => {
446+ Some ( name)
447+ }
415448
416449 Impl
417450 | ForeignMod
@@ -422,7 +455,6 @@ impl DefPathData {
422455 | Ctor
423456 | AnonConst
424457 | OpaqueTy
425- | AnonAssocTy
426458 | SyntheticCoroutineBody => None ,
427459 }
428460 }
@@ -443,7 +475,7 @@ impl DefPathData {
443475 Ctor => DefPathDataName :: Anon { namespace : sym:: constructor } ,
444476 AnonConst => DefPathDataName :: Anon { namespace : sym:: constant } ,
445477 OpaqueTy => DefPathDataName :: Anon { namespace : sym:: opaque } ,
446- AnonAssocTy => DefPathDataName :: Anon { namespace : sym:: anon_assoc } ,
478+ AnonAssocTy ( .. ) => DefPathDataName :: Anon { namespace : sym:: anon_assoc } ,
447479 SyntheticCoroutineBody => DefPathDataName :: Anon { namespace : sym:: synthetic } ,
448480 }
449481 }
0 commit comments