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 @@ pub fn iter_local_def_id(&self) -> impl Iterator<Item = LocalDefId> + '_ {
}
#[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);
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 {

View File

@ -1622,7 +1622,7 @@ fn def_path_hash(&self, index: DefIndex) -> DefPathHash {
}
#[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)
}

View File

@ -517,10 +517,9 @@ fn def_path_hash(&self, def: DefId) -> DefPathHash {
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> {
self.get_crate_data(cnum)
.def_path_hash_to_def_index(hash)
.map(|index| DefId { krate: cnum, 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 }
}
fn expn_hash_to_expn_id(&self, cnum: CrateNum, index_guess: u32, hash: ExpnHash) -> ExpnId {

View File

@ -15,9 +15,9 @@
impl DefPathHashMap<'tcx> {
#[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 {
DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash),
DefPathHashMap::OwnedFromMetadata(ref map) => map.get(def_path_hash).unwrap(),
DefPathHashMap::BorrowedFromTcx(_) => {
panic!("DefPathHashMap::BorrowedFromTcx variant only exists for serialization")
}

View File

@ -336,7 +336,11 @@ fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode {
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
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 {
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;
/// 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;
// utility functions

View File

@ -83,11 +83,7 @@ fn new_empty(source_map: &'tcx SourceMap) -> Self
/// 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,
) -> Option<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>);

View File

@ -361,7 +361,7 @@ fn serialize<'tcx>(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileE
})
}
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);
let stable_crate_id = hash.stable_crate_id();
@ -369,9 +369,7 @@ fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, hash: DefPathHash) -> Optio
// 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)
.map(LocalDefId::to_def_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.
@ -779,12 +777,7 @@ fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Result<Self, String> {
// 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)
.unwrap())
Ok(d.tcx().on_disk_cache.as_ref().unwrap().def_path_hash_to_def_id(d.tcx(), def_path_hash))
}
}