From 2b60338ee92fbae7e9414dd1bd6661ccd8a7b9da Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Tue, 20 Jul 2021 14:18:37 +0200 Subject: [PATCH] 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. --- compiler/rustc_hir/src/definitions.rs | 3 ++- compiler/rustc_metadata/src/rmeta/decoder.rs | 2 +- .../rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 7 +++---- .../rustc_metadata/src/rmeta/def_path_hash_map.rs | 4 ++-- compiler/rustc_middle/src/dep_graph/dep_node.rs | 6 +++++- compiler/rustc_middle/src/middle/cstore.rs | 2 +- compiler/rustc_middle/src/ty/context.rs | 6 +----- compiler/rustc_query_impl/src/on_disk_cache.rs | 13 +++---------- 8 files changed, 18 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 72a0318479f..ca29351455e 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -443,12 +443,13 @@ pub fn iter_local_def_id(&self) -> impl Iterator + '_ { } #[inline(always)] - pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option { + 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 { diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index d925e3102f2..51a8dcc9db4 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1622,7 +1622,7 @@ fn def_path_hash(&self, index: DefIndex) -> DefPathHash { } #[inline] - fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> Option { + fn def_path_hash_to_def_index(&self, hash: DefPathHash) -> DefIndex { self.def_path_hash_map.def_path_hash_to_def_index(&hash) } diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 4885de103a0..70952d388d5 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -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 { - 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 { diff --git a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs index 3f500fce334..e2095d553f5 100644 --- a/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs +++ b/compiler/rustc_metadata/src/rmeta/def_path_hash_map.rs @@ -15,9 +15,9 @@ impl DefPathHashMap<'tcx> { #[inline] - pub fn def_path_hash_to_def_index(&self, def_path_hash: &DefPathHash) -> Option { + 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") } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 3f28cd51af7..e788dd4be85 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -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 { 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 } diff --git a/compiler/rustc_middle/src/middle/cstore.rs b/compiler/rustc_middle/src/middle/cstore.rs index b66260c5de3..81c44b27033 100644 --- a/compiler/rustc_middle/src/middle/cstore.rs +++ b/compiler/rustc_middle/src/middle/cstore.rs @@ -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; + 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 diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index a13fe75fd4d..706c3959433 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -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; + fn def_path_hash_to_def_id(&self, tcx: TyCtxt<'tcx>, def_path_hash: DefPathHash) -> DefId; fn drop_serialized_data(&self, tcx: TyCtxt<'tcx>); diff --git a/compiler/rustc_query_impl/src/on_disk_cache.rs b/compiler/rustc_query_impl/src/on_disk_cache.rs index 82cc81ec4c3..f9456a6f2cd 100644 --- a/compiler/rustc_query_impl/src/on_disk_cache.rs +++ b/compiler/rustc_query_impl/src/on_disk_cache.rs @@ -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 { + 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 { // 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)) } }