From 12f8d12b418c9bbe4d2142f2c8e00a28a5b6dfe5 Mon Sep 17 00:00:00 2001 From: Michael Baikov Date: Thu, 13 Jun 2024 08:46:30 -0400 Subject: [PATCH] local_def_path_hash_to_def_id can fail local_def_path_hash_to_def_id is used by Debug impl for DepNode and it looks for DefPathHash inside the current compilation. During incremental compilation we are going through nodes that belong to a previous compilation and might not be present and a simple attempt to print such node with tracing::debug (try_mark_parent_green does it for example) results in a otherwise avoidable panic Panic was added in https://github.com/rust-lang/rust/pull/82183, specifically in 2b60338ee9, with a comment "We only use this mapping for cases where we know that it must succeed.", but I'm not sure if this property holds when we traverse nodes from the old compilation in order to figure out if they are valid or not --- compiler/rustc_hir/src/definitions.rs | 17 ++++++----------- compiler/rustc_middle/src/dep_graph/dep_node.rs | 12 ++---------- .../rustc_middle/src/query/on_disk_cache.rs | 8 ++++---- compiler/rustc_middle/src/ty/context.rs | 14 +++----------- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index 35833e258d5..5c86135ec8d 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -377,22 +377,17 @@ impl Definitions { } #[inline(always)] - pub fn local_def_path_hash_to_def_id( - &self, - hash: DefPathHash, - err_msg: &dyn std::fmt::Debug, - ) -> LocalDefId { + /// Returns `None` if the `DefPathHash` does not correspond to a `LocalDefId` + /// in the current compilation session. This can legitimately happen if the + /// `DefPathHash` is from a `DefId` in an upstream crate or, during incr. comp., + /// if the `DefPathHash` is from a previous compilation session and + /// the def-path does not exist anymore. + pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option { debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id); - #[cold] - #[inline(never)] - fn err(err_msg: &dyn std::fmt::Debug) -> ! { - panic!("{err_msg:?}") - } self.table .def_path_hash_to_index .get(&hash.local_hash()) .map(|local_def_index| LocalDefId { local_def_index }) - .unwrap_or_else(|| err(err_msg)) } pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap { diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 3c5bf6eb824..84b47a6ed44 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -194,10 +194,7 @@ impl DepNodeExt for DepNode { /// has been removed. fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option { if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash { - Some(tcx.def_path_hash_to_def_id( - DefPathHash(self.hash.into()), - &("Failed to extract DefId", self.kind, self.hash), - )) + tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into())) } else { None } @@ -390,12 +387,7 @@ impl<'tcx> DepNodeParams> for HirId { if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId { let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split(); let def_path_hash = DefPathHash::new(tcx.stable_crate_id(LOCAL_CRATE), local_hash); - let def_id = tcx - .def_path_hash_to_def_id( - def_path_hash, - &("Failed to extract HirId", dep_node.kind, dep_node.hash), - ) - .expect_local(); + let def_id = tcx.def_path_hash_to_def_id(def_path_hash)?.expect_local(); let local_id = local_id .as_u64() .try_into() diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index ccd0c7cb10c..924249bf37d 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -733,10 +733,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { // 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. - self.tcx.def_path_hash_to_def_id( - def_path_hash, - &("Failed to convert DefPathHash", def_path_hash), - ) + match self.tcx.def_path_hash_to_def_id(def_path_hash) { + Some(r) => r, + None => panic!("Failed to convert DefPathHash {def_path_hash:?}"), + } } fn decode_attr_id(&mut self) -> rustc_span::AttrId { diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 6d64c1d50ae..7848aa21eac 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -1677,11 +1677,7 @@ impl<'tcx> TyCtxt<'tcx> { /// 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`. - pub fn def_path_hash_to_def_id( - self, - hash: DefPathHash, - err_msg: &dyn std::fmt::Debug, - ) -> DefId { + pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> Option { debug!("def_path_hash_to_def_id({:?})", hash); let stable_crate_id = hash.stable_crate_id(); @@ -1689,13 +1685,9 @@ impl<'tcx> TyCtxt<'tcx> { // If this is a DefPathHash from the local crate, we can look up the // DefId in the tcx's `Definitions`. if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) { - self.untracked - .definitions - .read() - .local_def_path_hash_to_def_id(hash, err_msg) - .to_def_id() + Some(self.untracked.definitions.read().local_def_path_hash_to_def_id(hash)?.to_def_id()) } else { - self.def_path_hash_to_def_id_extern(hash, stable_crate_id) + Some(self.def_path_hash_to_def_id_extern(hash, stable_crate_id)) } }