Skip to content

Commit

Permalink
Move def_path_hash_to_def_id to rustc_middle.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Oct 20, 2021
1 parent 88c6d3d commit e53404c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
6 changes: 1 addition & 5 deletions compiler/rustc_middle/src/dep_graph/dep_node.rs
Expand Up @@ -338,11 +338,7 @@ impl DepNodeExt for DepNode {
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.fingerprint_style() == FingerprintStyle::DefPathHash {
Some(
tcx.on_disk_cache
.as_ref()?
.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into())),
)
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into())))
} else {
None
}
Expand Down
26 changes: 21 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Expand Up @@ -79,11 +79,6 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
where
Self: Sized;

/// 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`.
fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, def_path_hash: DefPathHash) -> DefId;

fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>);

fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult;
Expand Down Expand Up @@ -1301,6 +1296,27 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

/// 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`.
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> DefId {
debug!("def_path_hash_to_def_id({:?})", hash);

let stable_crate_id = hash.stable_crate_id();

// If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`.
if stable_crate_id == self.sess.local_stable_crate_id() {
self.untracked_resolutions.definitions.local_def_path_hash_to_def_id(hash).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cstore = &self.untracked_resolutions.cstore;
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
cstore.def_path_hash_to_def_id(cnum, hash)
}
}

pub fn def_path_debug_str(self, def_id: DefId) -> String {
// We are explicitly not going through queries here in order to get
// crate name and stable crate id since this code is called from debug!()
Expand Down
19 changes: 1 addition & 18 deletions compiler/rustc_query_impl/src/on_disk_cache.rs
Expand Up @@ -358,23 +358,6 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
Ok(())
})
}

fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, hash: DefPathHash) -> DefId {
debug!("def_path_hash_to_def_id({:?})", hash);

let stable_crate_id = hash.stable_crate_id();

// If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`.
if stable_crate_id == tcx.sess.local_stable_crate_id() {
tcx.definitions_untracked().local_def_path_hash_to_def_id(hash).to_def_id()
} else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId.
let cnum = tcx.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id);
tcx.cstore_untracked().def_path_hash_to_def_id(cnum, hash)
}
}
}

impl<'sess> OnDiskCache<'sess> {
Expand Down Expand Up @@ -764,7 +747,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
// If we get to this point, then all of the query inputs were green,
// which means that the definition with this hash is guaranteed to
// still exist in the current compilation session.
Ok(d.tcx().on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(d.tcx(), def_path_hash))
Ok(d.tcx().def_path_hash_to_def_id(def_path_hash))
}
}

Expand Down

0 comments on commit e53404c

Please sign in to comment.