Skip to content

Commit 53c94be

Browse files
committed
Auto merge of rust-lang#119259 - cjgillot:single-crate-id, r=<try>
Only store StableCrateId once in DefPathTable. rust-lang#119238 made me think of this. cc `@Mark-Simulacrum`
2 parents cdd6374 + 62b611e commit 53c94be

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

compiler/rustc_hir/src/definitions.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,36 @@ use std::hash::Hash;
2020
/// Internally the `DefPathTable` holds a tree of `DefKey`s, where each `DefKey`
2121
/// stores the `DefIndex` of its parent.
2222
/// There is one `DefPathTable` for each crate.
23-
#[derive(Clone, Default, Debug)]
23+
#[derive(Debug)]
2424
pub struct DefPathTable {
25+
stable_crate_id: StableCrateId,
2526
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>,
2729
def_path_hash_to_index: DefPathHashMap,
2830
}
2931

3032
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+
3142
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+
3246
let index = {
3347
let index = DefIndex::from(self.index_to_key.len());
3448
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
3549
self.index_to_key.push(key);
3650
index
3751
};
38-
self.def_path_hashes.push(def_path_hash);
52+
self.def_path_hashes.push(def_path_hash.local_hash());
3953
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
4054

4155
// Check for hash collisions of DefPathHashes. These should be
@@ -58,13 +72,6 @@ impl DefPathTable {
5872
);
5973
}
6074

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-
6875
index
6976
}
7077

@@ -73,19 +80,19 @@ impl DefPathTable {
7380
self.index_to_key[index]
7481
}
7582

83+
#[instrument(level = "trace", skip(self), ret)]
7684
#[inline(always)]
7785
pub fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
7886
let hash = self.def_path_hashes[index];
79-
debug!("def_path_hash({:?}) = {:?}", index, hash);
80-
hash
87+
DefPathHash::new(self.stable_crate_id, hash)
8188
}
8289

8390
pub fn enumerated_keys_and_path_hashes(
8491
&self,
85-
) -> impl Iterator<Item = (DefIndex, &DefKey, &DefPathHash)> + ExactSizeIterator + '_ {
92+
) -> impl Iterator<Item = (DefIndex, &DefKey, DefPathHash)> + ExactSizeIterator + '_ {
8693
self.index_to_key
8794
.iter_enumerated()
88-
.map(move |(index, key)| (index, key, &self.def_path_hashes[index]))
95+
.map(move |(index, key)| (index, key, self.def_path_hash(index)))
8996
}
9097
}
9198

@@ -96,9 +103,6 @@ impl DefPathTable {
96103
pub struct Definitions {
97104
table: DefPathTable,
98105
next_disambiguator: UnordMap<(LocalDefId, DefPathData), u32>,
99-
100-
/// The [StableCrateId] of the local crate.
101-
stable_crate_id: StableCrateId,
102106
}
103107

104108
/// A unique identifier that we can use to lookup a definition
@@ -117,7 +121,9 @@ impl DefKey {
117121
pub(crate) fn compute_stable_hash(&self, parent: DefPathHash) -> DefPathHash {
118122
let mut hasher = StableHasher::new();
119123

120-
parent.hash(&mut hasher);
124+
// The new path is in the same crate as `parent`, and will contain the stable_crate_id.
125+
// Therefore, we only need to include information of the parent's local hash.
126+
parent.local_hash().hash(&mut hasher);
121127

122128
let DisambiguatedDefPathData { ref data, disambiguator } = self.disambiguated_data;
123129

@@ -329,11 +335,11 @@ impl Definitions {
329335
let def_path_hash = key.compute_stable_hash(parent_hash);
330336

331337
// Create the root definition.
332-
let mut table = DefPathTable::default();
338+
let mut table = DefPathTable::new(stable_crate_id);
333339
let root = LocalDefId { local_def_index: table.allocate(key, def_path_hash) };
334340
assert_eq!(root.local_def_index, CRATE_DEF_INDEX);
335341

336-
Definitions { table, next_disambiguator: Default::default(), stable_crate_id }
342+
Definitions { table, next_disambiguator: Default::default() }
337343
}
338344

339345
/// Adds a definition with a parent definition.
@@ -375,7 +381,7 @@ impl Definitions {
375381
hash: DefPathHash,
376382
err: &mut dyn FnMut() -> !,
377383
) -> LocalDefId {
378-
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
384+
debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
379385
self.table
380386
.def_path_hash_to_index
381387
.get(&hash)

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
473473
for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
474474
let def_key = self.lazy(def_key);
475475
self.tables.def_keys.set_some(def_index, def_key);
476-
self.tables.def_path_hashes.set(def_index, *def_path_hash);
476+
self.tables.def_path_hashes.set(def_index, def_path_hash);
477477
}
478478
}
479479
}

compiler/rustc_span/src/def_id.rs

-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ impl DefPathHash {
114114
}
115115

116116
/// Returns the crate-local part of the [DefPathHash].
117-
///
118-
/// Used for tests.
119117
#[inline]
120118
pub fn local_hash(&self) -> Hash64 {
121119
self.0.split().1

0 commit comments

Comments
 (0)