Skip to content

Commit 732dd13

Browse files
committed
Recover when attempting to force a query with unknown DefPathHash.
1 parent 0b6f079 commit 732dd13

File tree

8 files changed

+22
-17
lines changed

8 files changed

+22
-17
lines changed

compiler/rustc_hir/src/definitions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,14 @@ impl Definitions {
442442
self.def_id_to_hir_id.iter_enumerated().map(|(k, _)| k)
443443
}
444444

445+
// FIXME(cjgillot) Stop returning an Option.
445446
#[inline(always)]
446-
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> LocalDefId {
447-
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
447+
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
448+
debug_assert_eq!(hash.stable_crate_id(), self.stable_crate_id);
448449
self.table
449450
.def_path_hash_to_index
450451
.get(&hash)
451452
.map(|local_def_index| LocalDefId { local_def_index })
452-
.unwrap()
453453
}
454454

455455
pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16271627
}
16281628

16291629
#[inline]
1630-
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> DefIndex {
1630+
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> Option<DefIndex> {
16311631
self.def_path_hash_map.def_path_hash_to_def_index(&hash)
16321632
}
16331633

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,11 @@ impl CrateStore for CStore {
507507
self.get_crate_data(def.krate).def_path_hash(def.index)
508508
}
509509

510-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
511-
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
512-
DefId { krate: cnum, index: def_index }
510+
// FIXME(cjgillot) Stop returning an Option.
511+
fn def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<DefId> {
512+
let cnum = self.stable_crate_id_to_crate_num(hash.stable_crate_id());
513+
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash)?;
514+
Some(DefId { krate: cnum, index: def_index })
513515
}
514516

515517
fn expn_hash_to_expn_id(

compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ crate enum DefPathHashMapRef<'tcx> {
1212
}
1313

1414
impl DefPathHashMapRef<'tcx> {
15+
// FIXME(cjgillot) Stop returning an Option.
1516
#[inline]
16-
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
17+
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> Option<DefIndex> {
1718
match *self {
18-
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
19+
DefPathHashMapRef::OwnedFromMetadata(ref map) => map.get(def_path_hash),
1920
DefPathHashMapRef::BorrowedFromTcx(_) => {
2021
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
2122
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl DepNodeExt for DepNode {
266266
/// has been removed.
267267
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
268268
if self.kind.fingerprint_style(tcx) == FingerprintStyle::DefPathHash {
269-
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into())))
269+
tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()))
270270
} else {
271271
None
272272
}

compiler/rustc_middle/src/ty/context.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1305,21 +1305,23 @@ impl<'tcx> TyCtxt<'tcx> {
13051305
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
13061306
/// session, if it still exists. This is used during incremental compilation to
13071307
/// turn a deserialized `DefPathHash` into its current `DefId`.
1308-
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> DefId {
1308+
// FIXME(cjgillot) Stop returning an Option.
1309+
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> Option<DefId> {
13091310
debug!("def_path_hash_to_def_id({:?})", hash);
13101311

13111312
let stable_crate_id = hash.stable_crate_id();
13121313

13131314
// If this is a DefPathHash from the local crate, we can look up the
13141315
// DefId in the tcx's `Definitions`.
13151316
if stable_crate_id == self.sess.local_stable_crate_id() {
1316-
self.untracked_resolutions.definitions.local_def_path_hash_to_def_id(hash).to_def_id()
1317+
self.untracked_resolutions
1318+
.definitions
1319+
.local_def_path_hash_to_def_id(hash)
1320+
.map(LocalDefId::to_def_id)
13171321
} else {
13181322
// If this is a DefPathHash from an upstream crate, let the CrateStore map
13191323
// it to a DefId.
1320-
let cstore = &self.untracked_resolutions.cstore;
1321-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1322-
cstore.def_path_hash_to_def_id(cnum, hash)
1324+
self.untracked_resolutions.cstore.def_path_hash_to_def_id(hash)
13231325
}
13241326
}
13251327

compiler/rustc_query_impl/src/on_disk_cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
747747
// If we get to this point, then all of the query inputs were green,
748748
// which means that the definition with this hash is guaranteed to
749749
// still exist in the current compilation session.
750-
Ok(d.tcx().def_path_hash_to_def_id(def_path_hash))
750+
Ok(d.tcx().def_path_hash_to_def_id(def_path_hash).unwrap())
751751
}
752752
}
753753

compiler/rustc_session/src/cstore.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub trait CrateStore: std::fmt::Debug {
193193
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
194194

195195
/// Fetch a DefId from a DefPathHash for a foreign crate.
196-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
196+
fn def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<DefId>;
197197
fn expn_hash_to_expn_id(
198198
&self,
199199
sess: &Session,

0 commit comments

Comments
 (0)