Skip to content

Commit

Permalink
Recover when attempting to force a query with unknown DefPathHash.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Dec 10, 2021
1 parent 0b6f079 commit 732dd13
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_hir/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,14 @@ impl Definitions {
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
}

// FIXME(cjgillot) Stop returning an Option.
#[inline(always)]
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> LocalDefId {
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
debug_assert_eq!(hash.stable_crate_id(), self.stable_crate_id);
self.table
.def_path_hash_to_index
.get(&hash)
.map(|local_def_index| LocalDefId { local_def_index })
.unwrap()
}

pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

#[inline]
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> DefIndex {
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> Option<DefIndex> {
self.def_path_hash_map.def_path_hash_to_def_index(&hash)
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,11 @@ impl CrateStore for CStore {
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 }
// FIXME(cjgillot) Stop returning an Option.
fn def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<DefId> {
let cnum = self.stable_crate_id_to_crate_num(hash.stable_crate_id());
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash)?;
Some(DefId { krate: cnum, index: def_index })
}

fn expn_hash_to_expn_id(
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ crate enum DefPathHashMapRef<'tcx> {
}

impl DefPathHashMapRef<'tcx> {
// FIXME(cjgillot) Stop returning an Option.
#[inline]
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> Option<DefIndex> {
match *self {
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash),
DefPathHashMapRef::BorrowedFromTcx(_) => {
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl DepNodeExt for DepNode {
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into())))
tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()))
} else {
None
}
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,21 +1305,23 @@ 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 {
// FIXME(cjgillot) Stop returning an Option.
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> Option<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()
self.untracked_resolutions
.definitions
.local_def_path_hash_to_def_id(hash)
.map(LocalDefId::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)
self.untracked_resolutions.cstore.def_path_hash_to_def_id(hash)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_query_impl/src/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,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().def_path_hash_to_def_id(def_path_hash))
Ok(d.tcx().def_path_hash_to_def_id(def_path_hash).unwrap())
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub trait CrateStore: std::fmt::Debug {
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;
fn def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<DefId>;
fn expn_hash_to_expn_id(
&self,
sess: &Session,
Expand Down

0 comments on commit 732dd13

Please sign in to comment.