Skip to content

Commit 7ac3e6c

Browse files
committed
Make def_path_hash_to_def_id a hook
1 parent a477d9b commit 7ac3e6c

File tree

4 files changed

+31
-38
lines changed

4 files changed

+31
-38
lines changed

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -650,14 +650,32 @@ 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 = |tcx, hash, err_msg| {
657+
debug!("def_path_hash_to_def_id({:?})", hash);
658+
659+
let stable_crate_id = hash.stable_crate_id();
660+
661+
// If this is a DefPathHash from the local crate, we can look up the
662+
// DefId in the tcx's `Definitions`.
663+
if stable_crate_id == tcx.stable_crate_id(LOCAL_CRATE) {
664+
tcx.untracked()
665+
.definitions
666+
.read()
667+
.local_def_path_hash_to_def_id(hash, err_msg)
668+
.to_def_id()
669+
} else {
670+
// If this is a DefPathHash from an upstream crate, let the CrateStore map
671+
// it to a DefId.
672+
let cstore = CStore::from_tcx(tcx.tcx);
673+
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
674+
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
675+
DefId { krate: cnum, index: def_index }
676+
}
677+
};
678+
661679
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
662680
let cstore = CStore::from_tcx(tcx.tcx);
663681
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)

compiler/rustc_middle/src/hooks/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use crate::mir;
77
use crate::query::TyCtxtAt;
88
use crate::ty::{Ty, TyCtxt};
9+
use rustc_hir::def_id::{DefId, DefPathHash};
910
use rustc_span::def_id::{CrateNum, LocalDefId};
1011
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};
1112

@@ -96,4 +97,10 @@ declare_hooks! {
9697
index_guess: u32,
9798
hash: ExpnHash
9899
) -> ExpnId;
100+
101+
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
102+
/// session, if it still exists. This is used during incremental compilation to
103+
/// turn a deserialized `DefPathHash` into its current `DefId`.
104+
/// Will fetch a DefId from a DefPathHash for a foreign crate.
105+
hook def_path_hash_to_def_id(hash: DefPathHash, err_msg: std::fmt::Arguments<'_>) -> DefId;
99106
}

compiler/rustc_middle/src/ty/context.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use rustc_session::config::CrateType;
6262
use rustc_session::cstore::{CrateStoreDyn, Untracked};
6363
use rustc_session::lint::Lint;
6464
use rustc_session::{Limit, MetadataKind, Session};
65-
use rustc_span::def_id::{DefPathHash, StableCrateId, CRATE_DEF_ID};
65+
use rustc_span::def_id::{StableCrateId, CRATE_DEF_ID};
6666
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6767
use rustc_span::{Span, DUMMY_SP};
6868
use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx};
@@ -1060,35 +1060,6 @@ impl<'tcx> TyCtxt<'tcx> {
10601060
}
10611061
}
10621062

1063-
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
1064-
/// session, if it still exists. This is used during incremental compilation to
1065-
/// turn a deserialized `DefPathHash` into its current `DefId`.
1066-
pub fn def_path_hash_to_def_id(
1067-
self,
1068-
hash: DefPathHash,
1069-
err_msg: std::fmt::Arguments<'_>,
1070-
) -> DefId {
1071-
debug!("def_path_hash_to_def_id({:?})", hash);
1072-
1073-
let stable_crate_id = hash.stable_crate_id();
1074-
1075-
// If this is a DefPathHash from the local crate, we can look up the
1076-
// DefId in the tcx's `Definitions`.
1077-
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
1078-
self.untracked
1079-
.definitions
1080-
.read()
1081-
.local_def_path_hash_to_def_id(hash, err_msg)
1082-
.to_def_id()
1083-
} else {
1084-
// If this is a DefPathHash from an upstream crate, let the CrateStore map
1085-
// it to a DefId.
1086-
let cstore = &*self.cstore_untracked();
1087-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1088-
cstore.def_path_hash_to_def_id(cnum, hash)
1089-
}
1090-
}
1091-
10921063
pub fn def_path_debug_str(self, def_id: DefId) -> String {
10931064
// We are explicitly not going through queries here in order to get
10941065
// crate name and stable crate id since this code is called from debug!()

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)