Skip to content

Commit 2b60338

Browse files
Make DefPathHash->DefId panic for if the mapping fails.
We only use this mapping for cases where we know that it must succeed. Letting it panic otherwise makes it harder to use the API in unsupported ways.
1 parent 5445715 commit 2b60338

File tree

8 files changed

+18
-25
lines changed

8 files changed

+18
-25
lines changed

Diff for: compiler/rustc_hir/src/definitions.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,13 @@ impl Definitions {
443443
}
444444

445445
#[inline(always)]
446-
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
446+
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> LocalDefId {
447447
debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
448448
self.table
449449
.def_path_hash_to_index
450450
.get(&hash)
451451
.map(|local_def_index| LocalDefId { local_def_index })
452+
.unwrap()
452453
}
453454

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

Diff for: compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16221622
}
16231623

16241624
#[inline]
1625-
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> Option<DefIndex> {
1625+
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> DefIndex {
16261626
self.def_path_hash_map.def_path_hash_to_def_index(&hash)
16271627
}
16281628

Diff for: compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -517,10 +517,9 @@ impl CrateStore for CStore {
517517
self.get_crate_data(def.krate).def_path_hash(def.index)
518518
}
519519

520-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> Option<DefId> {
521-
self.get_crate_data(cnum)
522-
.def_path_hash_to_def_index(hash)
523-
.map(|index| DefId { krate: cnum, index })
520+
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
521+
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
522+
DefId { krate: cnum, index: def_index }
524523
}
525524

526525
fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId {

Diff for: compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ crate enum DefPathHashMap<'tcx> {
1515

1616
impl DefPathHashMap<'tcx> {
1717
#[inline]
18-
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> Option<DefIndex> {
18+
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
1919
match *self {
20-
DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash),
20+
DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
2121
DefPathHashMap::BorrowedFromTcx(_) => {
2222
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
2323
}

Diff for: compiler/rustc_middle/src/dep_graph/dep_node.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,11 @@ impl DepNodeExt for DepNode {
336336
/// has been removed.
337337
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
338338
if self.kind.can_reconstruct_query_key() {
339-
tcx.on_disk_cache.as_ref()?.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into()))
339+
Some(
340+
tcx.on_disk_cache
341+
.as_ref()?
342+
.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into())),
343+
)
340344
} else {
341345
None
342346
}

Diff for: compiler/rustc_middle/src/middle/cstore.rs

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

204204
/// Fetch a DefId from a DefPathHash for a foreign crate.
205-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> Option<DefId>;
205+
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
206206
fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId;
207207

208208
// utility functions

Diff for: compiler/rustc_middle/src/ty/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,7 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
8383
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
8484
/// session, if it still exists. This is used during incremental compilation to
8585
/// turn a deserialized `DefPathHash` into its current `DefId`.
86-
fn def_path_hash_to_def_id(
87-
&self,
88-
tcx: TyCtxt<'tcx>,
89-
def_path_hash: DefPathHash,
90-
) -> Option<DefId>;
86+
fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, def_path_hash: DefPathHash) -> DefId;
9187

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

Diff for: compiler/rustc_query_impl/src/on_disk_cache.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -361,17 +361,15 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
361361
})
362362
}
363363

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

367367
let stable_crate_id = hash.stable_crate_id();
368368

369369
// If this is a DefPathHash from the local crate, we can look up the
370370
// DefId in the tcx's `Definitions`.
371371
if stable_crate_id == tcx.sess.local_stable_crate_id() {
372-
tcx.definitions_untracked()
373-
.local_def_path_hash_to_def_id(hash)
374-
.map(LocalDefId::to_def_id)
372+
tcx.definitions_untracked().local_def_path_hash_to_def_id(hash).to_def_id()
375373
} else {
376374
// If this is a DefPathHash from an upstream crate, let the CrateStore map
377375
// it to a DefId.
@@ -779,12 +777,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
779777
// If we get to this point, then all of the query inputs were green,
780778
// which means that the definition with this hash is guaranteed to
781779
// still exist in the current compilation session.
782-
Ok(d.tcx()
783-
.on_disk_cache
784-
.as_ref()
785-
.unwrap()
786-
.def_path_hash_to_def_id(d.tcx(), def_path_hash)
787-
.unwrap())
780+
Ok(d.tcx().on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(d.tcx(), def_path_hash))
788781
}
789782
}
790783

0 commit comments

Comments
 (0)