55//! expressions) that are mostly just leftovers.
66
77pub use crate :: def_id:: DefPathHash ;
8- use crate :: def_id:: { CrateNum , DefId , DefIndex , LocalDefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
8+ use crate :: def_id:: {
9+ CrateNum , DefId , DefIndex , LocalDefId , StableCrateId , CRATE_DEF_INDEX , LOCAL_CRATE ,
10+ } ;
911use crate :: hir;
1012
11- use rustc_ast:: crate_disambiguator:: CrateDisambiguator ;
1213use rustc_data_structures:: fx:: FxHashMap ;
1314use rustc_data_structures:: stable_hasher:: StableHasher ;
15+ use rustc_data_structures:: unhash:: UnhashMap ;
1416use rustc_index:: vec:: IndexVec ;
17+ use rustc_span:: crate_disambiguator:: CrateDisambiguator ;
1518use rustc_span:: hygiene:: ExpnId ;
1619use rustc_span:: symbol:: { kw, sym, Symbol } ;
1720
@@ -27,6 +30,7 @@ use tracing::debug;
2730pub struct DefPathTable {
2831 index_to_key : IndexVec < DefIndex , DefKey > ,
2932 def_path_hashes : IndexVec < DefIndex , DefPathHash > ,
33+ def_path_hash_to_index : UnhashMap < DefPathHash , DefIndex > ,
3034}
3135
3236impl DefPathTable {
@@ -39,6 +43,25 @@ impl DefPathTable {
3943 } ;
4044 self . def_path_hashes . push ( def_path_hash) ;
4145 debug_assert ! ( self . def_path_hashes. len( ) == self . index_to_key. len( ) ) ;
46+
47+ // Check for hash collisions of DefPathHashes. These should be
48+ // exceedingly rare.
49+ if let Some ( existing) = self . def_path_hash_to_index . insert ( def_path_hash, index) {
50+ let def_path1 = DefPath :: make ( LOCAL_CRATE , existing, |idx| self . def_key ( idx) ) ;
51+ let def_path2 = DefPath :: make ( LOCAL_CRATE , index, |idx| self . def_key ( idx) ) ;
52+
53+ // Continuing with colliding DefPathHashes can lead to correctness
54+ // issues. We must abort compilation.
55+ panic ! ( "Found DefPathHash collsion between {:?} and {:?}" , def_path1, def_path2) ;
56+ }
57+
58+ // Assert that all DefPathHashes correctly contain the local crate's
59+ // StableCrateId
60+ #[ cfg( debug_assertions) ]
61+ if let Some ( root) = self . def_path_hashes . get ( CRATE_DEF_INDEX ) {
62+ assert ! ( def_path_hash. stable_crate_id( ) == root. stable_crate_id( ) ) ;
63+ }
64+
4265 index
4366 }
4467
@@ -108,13 +131,10 @@ pub struct DefKey {
108131}
109132
110133impl DefKey {
111- fn compute_stable_hash ( & self , parent_hash : DefPathHash ) -> DefPathHash {
134+ fn compute_stable_hash ( & self , parent : DefPathHash ) -> DefPathHash {
112135 let mut hasher = StableHasher :: new ( ) ;
113136
114- // We hash a `0u8` here to disambiguate between regular `DefPath` hashes,
115- // and the special "root_parent" below.
116- 0u8 . hash ( & mut hasher) ;
117- parent_hash. hash ( & mut hasher) ;
137+ parent. hash ( & mut hasher) ;
118138
119139 let DisambiguatedDefPathData { ref data, disambiguator } = self . disambiguated_data ;
120140
@@ -127,19 +147,13 @@ impl DefKey {
127147
128148 disambiguator. hash ( & mut hasher) ;
129149
130- DefPathHash ( hasher. finish ( ) )
131- }
150+ let local_hash: u64 = hasher. finish ( ) ;
132151
133- fn root_parent_stable_hash (
134- crate_name : & str ,
135- crate_disambiguator : CrateDisambiguator ,
136- ) -> DefPathHash {
137- let mut hasher = StableHasher :: new ( ) ;
138- // Disambiguate this from a regular `DefPath` hash; see `compute_stable_hash()` above.
139- 1u8 . hash ( & mut hasher) ;
140- crate_name. hash ( & mut hasher) ;
141- crate_disambiguator. hash ( & mut hasher) ;
142- DefPathHash ( hasher. finish ( ) )
152+ // Construct the new DefPathHash, making sure that the `crate_id`
153+ // portion of the hash is properly copied from the parent. This way the
154+ // `crate_id` part will be recursively propagated from the root to all
155+ // DefPathHashes in this DefPathTable.
156+ DefPathHash :: new ( parent. stable_crate_id ( ) , local_hash)
143157 }
144158}
145159
@@ -295,6 +309,12 @@ impl Definitions {
295309 self . table . def_path_hash ( id. local_def_index )
296310 }
297311
312+ #[ inline]
313+ pub fn def_path_hash_to_def_id ( & self , def_path_hash : DefPathHash ) -> LocalDefId {
314+ let local_def_index = self . table . def_path_hash_to_index [ & def_path_hash] ;
315+ LocalDefId { local_def_index }
316+ }
317+
298318 /// Returns the path from the crate root to `index`. The root
299319 /// nodes are not included in the path (i.e., this will be an
300320 /// empty vector for the crate root). For an inlined item, this
@@ -332,7 +352,8 @@ impl Definitions {
332352 } ,
333353 } ;
334354
335- let parent_hash = DefKey :: root_parent_stable_hash ( crate_name, crate_disambiguator) ;
355+ let stable_crate_id = StableCrateId :: new ( crate_name, crate_disambiguator) ;
356+ let parent_hash = DefPathHash :: new ( stable_crate_id, 0 ) ;
336357 let def_path_hash = key. compute_stable_hash ( parent_hash) ;
337358
338359 // Create the root definition.
0 commit comments