Skip to content

Commit 19eb1c4

Browse files
committed
Auto merge of #79915 - Aaron1011:fix/fix-reuse-def-path-hash, r=petrochenkov
Use `def_path_hash_to_def_id` when re-using a `RawDefId` Fixes #79890 Previously, we just copied a `RawDefId` from the 'old' map to the 'new' map. However, the `RawDefId` for a given `DefPathHash` may be different in the current compilation session. Using `def_path_hash_to_def_id` ensures that the `RawDefId` we use is valid in the current session.
2 parents a2e29d6 + 3918b82 commit 19eb1c4

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

compiler/rustc_middle/src/dep_graph/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
9393

9494
fn register_reused_dep_path_hash(&self, hash: DefPathHash) {
9595
if let Some(cache) = self.queries.on_disk_cache.as_ref() {
96-
cache.register_reused_dep_path_hash(hash)
96+
cache.register_reused_dep_path_hash(*self, hash)
9797
}
9898
}
9999

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ impl<'sess> OnDiskCache<'sess> {
454454
fn try_remap_cnum(&self, tcx: TyCtxt<'_>, cnum: u32) -> Option<CrateNum> {
455455
let cnum_map =
456456
self.cnum_map.get_or_init(|| Self::compute_cnum_map(tcx, &self.prev_cnums[..]));
457+
debug!("try_remap_cnum({}): cnum_map={:?}", cnum, cnum_map);
457458

458459
cnum_map[CrateNum::from_u32(cnum)]
459460
}
@@ -466,9 +467,22 @@ impl<'sess> OnDiskCache<'sess> {
466467
.insert(hash, RawDefId { krate: def_id.krate.as_u32(), index: def_id.index.as_u32() });
467468
}
468469

469-
pub fn register_reused_dep_path_hash(&self, hash: DefPathHash) {
470-
if let Some(old_id) = self.foreign_def_path_hashes.get(&hash) {
471-
self.latest_foreign_def_path_hashes.lock().insert(hash, *old_id);
470+
/// If the given `hash` still exists in the current compilation,
471+
/// calls `store_foreign_def_id` with its current `DefId`.
472+
///
473+
/// Normally, `store_foreign_def_id_hash` can be called directly by
474+
/// the dependency graph when we construct a `DepNode`. However,
475+
/// when we re-use a deserialized `DepNode` from the previous compilation
476+
/// session, we only have the `DefPathHash` available. This method is used
477+
/// to that any `DepNode` that we re-use has a `DefPathHash` -> `RawId` written
478+
/// out for usage in the next compilation session.
479+
pub fn register_reused_dep_path_hash(&self, tcx: TyCtxt<'tcx>, hash: DefPathHash) {
480+
// We can't simply copy the `RawDefId` from `foreign_def_path_hashes` to
481+
// `latest_foreign_def_path_hashes`, since the `RawDefId` might have
482+
// changed in the current compilation session (e.g. we've added/removed crates,
483+
// or added/removed definitions before/after the target definition).
484+
if let Some(def_id) = self.def_path_hash_to_def_id(tcx, hash) {
485+
self.store_foreign_def_id_hash(def_id, hash);
472486
}
473487
}
474488

@@ -592,6 +606,7 @@ impl<'sess> OnDiskCache<'sess> {
592606
match cache.entry(hash) {
593607
Entry::Occupied(e) => *e.get(),
594608
Entry::Vacant(e) => {
609+
debug!("def_path_hash_to_def_id({:?})", hash);
595610
// Check if the `DefPathHash` corresponds to a definition in the current
596611
// crate
597612
if let Some(def_id) = self.local_def_path_hash_to_def_id.get(&hash).cloned() {
@@ -605,9 +620,11 @@ impl<'sess> OnDiskCache<'sess> {
605620
// current compilation session, the crate is guaranteed to be the same
606621
// (otherwise, we would compute a different `DefPathHash`).
607622
let raw_def_id = self.get_raw_def_id(&hash)?;
623+
debug!("def_path_hash_to_def_id({:?}): raw_def_id = {:?}", hash, raw_def_id);
608624
// If the owning crate no longer exists, the corresponding definition definitely
609625
// no longer exists.
610626
let krate = self.try_remap_cnum(tcx, raw_def_id.krate)?;
627+
debug!("def_path_hash_to_def_id({:?}): krate = {:?}", hash, krate);
611628
// If our `DefPathHash` corresponded to a definition in the local crate,
612629
// we should have either found it in `local_def_path_hash_to_def_id`, or
613630
// never attempted to load it in the first place. Any query result or `DepNode`
@@ -621,6 +638,7 @@ impl<'sess> OnDiskCache<'sess> {
621638
// Try to find a definition in the current session, using the previous `DefIndex`
622639
// as an initial guess.
623640
let opt_def_id = tcx.cstore.def_path_hash_to_def_id(krate, raw_def_id.index, hash);
641+
debug!("def_path_to_def_id({:?}): opt_def_id = {:?}", hash, opt_def_id);
624642
e.insert(opt_def_id);
625643
opt_def_id
626644
}

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -596,9 +596,9 @@ impl<K: DepKind> DepGraph<K> {
596596
// an eval_always node, let's try to mark it green recursively.
597597
if !dep_dep_node.kind.is_eval_always() {
598598
debug!(
599-
"try_mark_previous_green({:?}) --- state of dependency {:?} \
599+
"try_mark_previous_green({:?}) --- state of dependency {:?} ({}) \
600600
is unknown, trying to mark it green",
601-
dep_node, dep_dep_node
601+
dep_node, dep_dep_node, dep_dep_node.hash,
602602
);
603603

604604
let node_index = self.try_mark_previous_green(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub trait MyTrait {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// aux-build:issue-79890.rs
2+
// revisions:rpass1 rpass2 rpass3
3+
// compile-flags:--extern issue_79890 --test
4+
// edition:2018
5+
6+
// Tests that we don't ICE when the set of imported crates changes
7+
#[cfg(rpass2)] use issue_79890::MyTrait;

0 commit comments

Comments
 (0)