Skip to content

Commit

Permalink
Make def_path_hash_to_def_id a hook
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Mar 27, 2024
1 parent 0f5911c commit 24fc6e9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
14 changes: 9 additions & 5 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,14 +650,18 @@ impl CrateStore for CStore {
fn def_path_hash(&self, def: DefId) -> DefPathHash {
self.get_crate_data(def.krate).def_path_hash(def.index)
}

fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
DefId { krate: cnum, index: def_index }
}
}

fn provide_cstore_hooks(providers: &mut Providers) {
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = CStore::from_tcx(tcx.tcx);
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
DefId { krate: cnum, index: def_index }
};

providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
let cstore = CStore::from_tcx(tcx.tcx);
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_middle/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use crate::mir;
use crate::query::TyCtxtAt;
use crate::ty::{Ty, TyCtxt};
use rustc_hir::def_id::{DefId, DefPathHash};
use rustc_session::StableCrateId;
use rustc_span::def_id::{CrateNum, LocalDefId};
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};

Expand Down Expand Up @@ -94,4 +96,10 @@ declare_hooks! {
index_guess: u32,
hash: ExpnHash
) -> ExpnId;

/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
/// session, if it still exists. This is used during incremental compilation to
/// turn a deserialized `DefPathHash` into its current `DefId`.
/// Will fetch a DefId from a DefPathHash for a foreign crate.
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
}
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,7 @@ impl<'tcx> TyCtxt<'tcx> {
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &*self.cstore_untracked();
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
}
}

Expand Down
3 changes: 0 additions & 3 deletions compiler/rustc_session/src/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,6 @@ pub trait CrateStore: std::fmt::Debug {
fn crate_name(&self, cnum: CrateNum) -> Symbol;
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;

/// Fetch a DefId from a DefPathHash for a foreign crate.
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
}

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

0 comments on commit 24fc6e9

Please sign in to comment.