Skip to content

Commit 24fc6e9

Browse files
committed
Make def_path_hash_to_def_id a hook
1 parent 0f5911c commit 24fc6e9

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,18 @@ impl CrateStore for CStore {
650650
fn def_path_hash(&self, def: DefId) -> DefPathHash {
651651
self.get_crate_data(def.krate).def_path_hash(def.index)
652652
}
653-
654-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
655-
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
656-
DefId { krate: cnum, index: def_index }
657-
}
658653
}
659654

660655
fn provide_cstore_hooks(providers: &mut Providers) {
656+
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
657+
// If this is a DefPathHash from an upstream crate, let the CrateStore map
658+
// it to a DefId.
659+
let cstore = CStore::from_tcx(tcx.tcx);
660+
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
661+
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
662+
DefId { krate: cnum, index: def_index }
663+
};
664+
661665
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
662666
let cstore = CStore::from_tcx(tcx.tcx);
663667
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)

compiler/rustc_middle/src/hooks/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use crate::mir;
77
use crate::query::TyCtxtAt;
88
use crate::ty::{Ty, TyCtxt};
9+
use rustc_hir::def_id::{DefId, DefPathHash};
10+
use rustc_session::StableCrateId;
911
use rustc_span::def_id::{CrateNum, LocalDefId};
1012
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};
1113

@@ -94,4 +96,10 @@ declare_hooks! {
9496
index_guess: u32,
9597
hash: ExpnHash
9698
) -> ExpnId;
99+
100+
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
101+
/// session, if it still exists. This is used during incremental compilation to
102+
/// turn a deserialized `DefPathHash` into its current `DefId`.
103+
/// Will fetch a DefId from a DefPathHash for a foreign crate.
104+
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
97105
}

compiler/rustc_middle/src/ty/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1073,11 +1073,7 @@ impl<'tcx> TyCtxt<'tcx> {
10731073
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
10741074
self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id()
10751075
} else {
1076-
// If this is a DefPathHash from an upstream crate, let the CrateStore map
1077-
// it to a DefId.
1078-
let cstore = &*self.cstore_untracked();
1079-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1080-
cstore.def_path_hash_to_def_id(cnum, hash)
1076+
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
10811077
}
10821078
}
10831079

compiler/rustc_session/src/cstore.rs

-3
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ pub trait CrateStore: std::fmt::Debug {
218218
fn crate_name(&self, cnum: CrateNum) -> Symbol;
219219
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
220220
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
221-
222-
/// Fetch a DefId from a DefPathHash for a foreign crate.
223-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
224221
}
225222

226223
pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;

0 commit comments

Comments
 (0)