@@ -20,27 +20,42 @@ use std::hash::Hash;
20
20
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
21
21
/// stores the `DefIndex` of its parent.
22
22
/// There is one `DefPathTable` for each crate.
23
- #[ derive( Clone , Default , Debug ) ]
23
+ #[ derive( Debug ) ]
24
24
pub struct DefPathTable {
25
+ stable_crate_id : StableCrateId ,
25
26
index_to_key : IndexVec < DefIndex , DefKey > ,
26
- def_path_hashes : IndexVec < DefIndex , DefPathHash > ,
27
+ // We do only store the local hash, as all the definitions are from the current crate.
28
+ def_path_hashes : IndexVec < DefIndex , Hash64 > ,
27
29
def_path_hash_to_index : DefPathHashMap ,
28
30
}
29
31
30
32
impl DefPathTable {
33
+ fn new ( stable_crate_id : StableCrateId ) -> DefPathTable {
34
+ DefPathTable {
35
+ stable_crate_id,
36
+ index_to_key : Default :: default ( ) ,
37
+ def_path_hashes : Default :: default ( ) ,
38
+ def_path_hash_to_index : Default :: default ( ) ,
39
+ }
40
+ }
41
+
31
42
fn allocate ( & mut self , key : DefKey , def_path_hash : DefPathHash ) -> DefIndex {
43
+ // Assert that all DefPathHashes correctly contain the local crate's StableCrateId.
44
+ debug_assert_eq ! ( self . stable_crate_id, def_path_hash. stable_crate_id( ) ) ;
45
+ let local_hash = def_path_hash. local_hash ( ) ;
46
+
32
47
let index = {
33
48
let index = DefIndex :: from ( self . index_to_key . len ( ) ) ;
34
49
debug ! ( "DefPathTable::insert() - {:?} <-> {:?}" , key, index) ;
35
50
self . index_to_key . push ( key) ;
36
51
index
37
52
} ;
38
- self . def_path_hashes . push ( def_path_hash ) ;
53
+ self . def_path_hashes . push ( local_hash ) ;
39
54
debug_assert ! ( self . def_path_hashes. len( ) == self . index_to_key. len( ) ) ;
40
55
41
56
// Check for hash collisions of DefPathHashes. These should be
42
57
// exceedingly rare.
43
- if let Some ( existing) = self . def_path_hash_to_index . insert ( & def_path_hash , & index) {
58
+ if let Some ( existing) = self . def_path_hash_to_index . insert ( & local_hash , & index) {
44
59
let def_path1 = DefPath :: make ( LOCAL_CRATE , existing, |idx| self . def_key ( idx) ) ;
45
60
let def_path2 = DefPath :: make ( LOCAL_CRATE , index, |idx| self . def_key ( idx) ) ;
46
61
@@ -58,13 +73,6 @@ impl DefPathTable {
58
73
) ;
59
74
}
60
75
61
- // Assert that all DefPathHashes correctly contain the local crate's
62
- // StableCrateId
63
- #[ cfg( debug_assertions) ]
64
- if let Some ( root) = self . def_path_hashes . get ( CRATE_DEF_INDEX ) {
65
- assert ! ( def_path_hash. stable_crate_id( ) == root. stable_crate_id( ) ) ;
66
- }
67
-
68
76
index
69
77
}
70
78
@@ -73,19 +81,19 @@ impl DefPathTable {
73
81
self . index_to_key [ index]
74
82
}
75
83
84
+ #[ instrument( level = "trace" , skip( self ) , ret) ]
76
85
#[ inline( always) ]
77
86
pub fn def_path_hash ( & self , index : DefIndex ) -> DefPathHash {
78
87
let hash = self . def_path_hashes [ index] ;
79
- debug ! ( "def_path_hash({:?}) = {:?}" , index, hash) ;
80
- hash
88
+ DefPathHash :: new ( self . stable_crate_id , hash)
81
89
}
82
90
83
91
pub fn enumerated_keys_and_path_hashes (
84
92
& self ,
85
- ) -> impl Iterator < Item = ( DefIndex , & DefKey , & DefPathHash ) > + ExactSizeIterator + ' _ {
93
+ ) -> impl Iterator < Item = ( DefIndex , & DefKey , DefPathHash ) > + ExactSizeIterator + ' _ {
86
94
self . index_to_key
87
95
. iter_enumerated ( )
88
- . map ( move |( index, key) | ( index, key, & self . def_path_hashes [ index] ) )
96
+ . map ( move |( index, key) | ( index, key, self . def_path_hash ( index) ) )
89
97
}
90
98
}
91
99
@@ -96,9 +104,6 @@ impl DefPathTable {
96
104
pub struct Definitions {
97
105
table : DefPathTable ,
98
106
next_disambiguator : UnordMap < ( LocalDefId , DefPathData ) , u32 > ,
99
-
100
- /// The [StableCrateId] of the local crate.
101
- stable_crate_id : StableCrateId ,
102
107
}
103
108
104
109
/// A unique identifier that we can use to lookup a definition
@@ -329,11 +334,11 @@ impl Definitions {
329
334
let def_path_hash = key. compute_stable_hash ( parent_hash) ;
330
335
331
336
// Create the root definition.
332
- let mut table = DefPathTable :: default ( ) ;
337
+ let mut table = DefPathTable :: new ( stable_crate_id ) ;
333
338
let root = LocalDefId { local_def_index : table. allocate ( key, def_path_hash) } ;
334
339
assert_eq ! ( root. local_def_index, CRATE_DEF_INDEX ) ;
335
340
336
- Definitions { table, next_disambiguator : Default :: default ( ) , stable_crate_id }
341
+ Definitions { table, next_disambiguator : Default :: default ( ) }
337
342
}
338
343
339
344
/// Adds a definition with a parent definition.
@@ -375,10 +380,10 @@ impl Definitions {
375
380
hash : DefPathHash ,
376
381
err : & mut dyn FnMut ( ) -> !,
377
382
) -> LocalDefId {
378
- debug_assert ! ( hash. stable_crate_id( ) == self . stable_crate_id) ;
383
+ debug_assert ! ( hash. stable_crate_id( ) == self . table . stable_crate_id) ;
379
384
self . table
380
385
. def_path_hash_to_index
381
- . get ( & hash)
386
+ . get ( & hash. local_hash ( ) )
382
387
. map ( |local_def_index| LocalDefId { local_def_index } )
383
388
. unwrap_or_else ( || err ( ) )
384
389
}
0 commit comments