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.
This commit is contained in:
Michael Woerister 2021-07-20 14:18:37 +02:00
parent 5445715c20
commit 2b60338ee9
8 changed files with 18 additions and 25 deletions

View File

@ -443,12 +443,13 @@ impl Definitions {
} }
#[inline(always)] #[inline(always)]
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> { pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> LocalDefId {
debug_assert!(hash.stable_crate_id() == self.stable_crate_id); debug_assert!(hash.stable_crate_id() == self.stable_crate_id);
self.table self.table
.def_path_hash_to_index .def_path_hash_to_index
.get(&hash) .get(&hash)
.map(|local_def_index| LocalDefId { local_def_index }) .map(|local_def_index| LocalDefId { local_def_index })
.unwrap()
} }
pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap { pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {

View File

@ -1622,7 +1622,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
} }
#[inline] #[inline]
fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> Option<DefIndex> { fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> DefIndex {
self.def_path_hash_map.def_path_hash_to_def_index(&hash) self.def_path_hash_map.def_path_hash_to_def_index(&hash)
} }

View File

@ -517,10 +517,9 @@ impl CrateStore for CStore {
self.get_crate_data(def.krate).def_path_hash(def.index) self.get_crate_data(def.krate).def_path_hash(def.index)
} }
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> Option<DefId> { fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
self.get_crate_data(cnum) let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
.def_path_hash_to_def_index(hash) DefId { krate: cnum, index: def_index }
.map(|index| DefId { krate: cnum, index })
} }
fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId { fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId {

View File

@ -15,9 +15,9 @@ crate enum DefPathHashMap<'tcx> {
impl DefPathHashMap<'tcx> { impl DefPathHashMap<'tcx> {
#[inline] #[inline]
pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> Option<DefIndex> { pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> DefIndex {
match *self { match *self {
DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash), DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
DefPathHashMap::BorrowedFromTcx(_) => { DefPathHashMap::BorrowedFromTcx(_) => {
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization") panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
} }

View File

@ -336,7 +336,11 @@ impl DepNodeExt for DepNode {
/// has been removed. /// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> { fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() { if self.kind.can_reconstruct_query_key() {
tcx.on_disk_cache.as_ref()?.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into())) Some(
tcx.on_disk_cache
.as_ref()?
.def_path_hash_to_def_id(tcx, DefPathHash(self.hash.into())),
)
} else { } else {
None None
} }

View File

@ -202,7 +202,7 @@ pub trait CrateStore: std::fmt::Debug {
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum; fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
/// Fetch a DefId from a DefPathHash for a foreign crate. /// Fetch a DefId from a DefPathHash for a foreign crate.
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> Option<DefId>; fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId; fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId;
// utility functions // utility functions

View File

@ -83,11 +83,7 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation /// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
/// session, if it still exists. This is used during incremental compilation to /// session, if it still exists. This is used during incremental compilation to
/// turn a deserialized `DefPathHash` into its current `DefId`. /// turn a deserialized `DefPathHash` into its current `DefId`.
fn def_path_hash_to_def_id( fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, def_path_hash: DefPathHash) -> DefId;
&self,
tcx: TyCtxt<'tcx>,
def_path_hash: DefPathHash,
) -> Option<DefId>;
fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>); fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>);

View File

@ -361,7 +361,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
}) })
} }
fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, hash: DefPathHash) -> Option<DefId> { fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, hash: DefPathHash) -> DefId {
debug!("def_path_hash_to_def_id({:?})", hash); debug!("def_path_hash_to_def_id({:?})", hash);
let stable_crate_id = hash.stable_crate_id(); let stable_crate_id = hash.stable_crate_id();
@ -369,9 +369,7 @@ impl<'sess> rustc_middle::ty::OnDiskCache<'sess> for OnDiskCache<'sess> {
// If this is a DefPathHash from the local crate, we can look up the // If this is a DefPathHash from the local crate, we can look up the
// DefId in the tcx's `Definitions`. // DefId in the tcx's `Definitions`.
if stable_crate_id == tcx.sess.local_stable_crate_id() { if stable_crate_id == tcx.sess.local_stable_crate_id() {
tcx.definitions_untracked() tcx.definitions_untracked().local_def_path_hash_to_def_id(hash).to_def_id()
.local_def_path_hash_to_def_id(hash)
.map(LocalDefId::to_def_id)
} else { } else {
// If this is a DefPathHash from an upstream crate, let the CrateStore map // If this is a DefPathHash from an upstream crate, let the CrateStore map
// it to a DefId. // it to a DefId.
@ -779,12 +777,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for DefId {
// If we get to this point, then all of the query inputs were green, // 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 // which means that the definition with this hash is guaranteed to
// still exist in the current compilation session. // still exist in the current compilation session.
Ok(d.tcx() Ok(d.tcx().on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(d.tcx(), def_path_hash))
.on_disk_cache
.as_ref()
.unwrap()
.def_path_hash_to_def_id(d.tcx(), def_path_hash)
.unwrap())
} }
} }