Skip to content

Commit 06b420a

Browse files
committed
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 rust-lang#82183, specifically in 2b60338, 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
1 parent db5ed4b commit 06b420a

File tree

4 files changed

+10
-36
lines changed

4 files changed

+10
-36
lines changed

compiler/rustc_hir/src/definitions.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -377,22 +377,12 @@ impl Definitions {
377377
}
378378

379379
#[inline(always)]
380-
pub fn local_def_path_hash_to_def_id(
381-
&self,
382-
hash: DefPathHash,
383-
err_msg: &dyn std::fmt::Debug,
384-
) -> LocalDefId {
380+
pub fn local_def_path_hash_to_def_id(&self, hash: DefPathHash) -> Option<LocalDefId> {
385381
debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
386-
#[cold]
387-
#[inline(never)]
388-
fn err(err_msg: &dyn std::fmt::Debug) -> ! {
389-
panic!("{err_msg:?}")
390-
}
391382
self.table
392383
.def_path_hash_to_index
393384
.get(&hash.local_hash())
394385
.map(|local_def_index| LocalDefId { local_def_index })
395-
.unwrap_or_else(|| err(err_msg))
396386
}
397387

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

compiler/rustc_middle/src/dep_graph/dep_node.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,7 @@ impl DepNodeExt for DepNode {
194194
/// has been removed.
195195
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
196196
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
197-
Some(tcx.def_path_hash_to_def_id(
198-
DefPathHash(self.hash.into()),
199-
&("Failed to extract DefId", self.kind, self.hash),
200-
))
197+
tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()))
201198
} else {
202199
None
203200
}
@@ -390,12 +387,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
390387
if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId {
391388
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split();
392389
let def_path_hash = DefPathHash::new(tcx.stable_crate_id(LOCAL_CRATE), local_hash);
393-
let def_id = tcx
394-
.def_path_hash_to_def_id(
395-
def_path_hash,
396-
&("Failed to extract HirId", dep_node.kind, dep_node.hash),
397-
)
398-
.expect_local();
390+
let def_id = tcx.def_path_hash_to_def_id(def_path_hash)?.expect_local();
399391
let local_id = local_id
400392
.as_u64()
401393
.try_into()

compiler/rustc_middle/src/query/on_disk_cache.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
733733
// If we get to this point, then all of the query inputs were green,
734734
// which means that the definition with this hash is guaranteed to
735735
// still exist in the current compilation session.
736-
self.tcx.def_path_hash_to_def_id(
737-
def_path_hash,
738-
&("Failed to convert DefPathHash", def_path_hash),
739-
)
736+
match self.tcx.def_path_hash_to_def_id(def_path_hash) {
737+
Some(r) => r,
738+
None => panic!("Failed to convert DefPathHash {def_path_hash:?}"),
739+
}
740740
}
741741

742742
fn decode_attr_id(&mut self) -> rustc_span::AttrId {

compiler/rustc_middle/src/ty/context.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -1677,25 +1677,17 @@ impl<'tcx> TyCtxt<'tcx> {
16771677
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
16781678
/// session, if it still exists. This is used during incremental compilation to
16791679
/// turn a deserialized `DefPathHash` into its current `DefId`.
1680-
pub fn def_path_hash_to_def_id(
1681-
self,
1682-
hash: DefPathHash,
1683-
err_msg: &dyn std::fmt::Debug,
1684-
) -> DefId {
1680+
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> Option<DefId> {
16851681
debug!("def_path_hash_to_def_id({:?})", hash);
16861682

16871683
let stable_crate_id = hash.stable_crate_id();
16881684

16891685
// If this is a DefPathHash from the local crate, we can look up the
16901686
// DefId in the tcx's `Definitions`.
16911687
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
1692-
self.untracked
1693-
.definitions
1694-
.read()
1695-
.local_def_path_hash_to_def_id(hash, err_msg)
1696-
.to_def_id()
1688+
Some(self.untracked.definitions.read().local_def_path_hash_to_def_id(hash)?.to_def_id())
16971689
} else {
1698-
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
1690+
Some(self.def_path_hash_to_def_id_extern(hash, stable_crate_id))
16991691
}
17001692
}
17011693

0 commit comments

Comments
 (0)