Skip to content

Commit 5ebfe8a

Browse files
committed
Also store the DefPathHash as part of a DefId creation side effect, so we can reliably recreate the same DefId over and over again
1 parent 26edb9d commit 5ebfe8a

File tree

5 files changed

+48
-27
lines changed

5 files changed

+48
-27
lines changed

compiler/rustc_hir/src/definitions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,11 @@ impl Definitions {
346346
}
347347

348348
/// Adds a definition with a parent definition.
349-
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
349+
pub fn create_def(
350+
&mut self,
351+
parent: LocalDefId,
352+
data: DefPathData,
353+
) -> (LocalDefId, DefPathHash) {
350354
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
351355
// reference to `Definitions` and we're already holding a mutable reference.
352356
debug!(
@@ -375,7 +379,7 @@ impl Definitions {
375379
debug!("create_def: after disambiguation, key = {:?}", key);
376380

377381
// Create the definition.
378-
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
382+
(LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }, def_path_hash)
379383
}
380384

381385
#[inline(always)]

compiler/rustc_middle/src/ty/context.rs

+34-20
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4141
use rustc_query_system::cache::WithDepNode;
4242
use rustc_query_system::dep_graph::{DepNodeIndex, TaskDepsRef};
4343
use rustc_query_system::ich::StableHashingContext;
44-
use rustc_query_system::query::DefIdInfo;
4544
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
4645
use rustc_session::config::CrateType;
4746
use rustc_session::cstore::{CrateStoreDyn, Untracked};
@@ -51,10 +50,8 @@ use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId};
5150
use rustc_span::symbol::{Ident, Symbol, kw, sym};
5251
use rustc_span::{DUMMY_SP, Span};
5352
use rustc_type_ir::TyKind::*;
54-
use rustc_type_ir::fold::TypeFoldable;
55-
use rustc_type_ir::lang_items::TraitSolverLangItem;
56-
pub use rustc_type_ir::lift::Lift;
57-
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags, WithCachedTypeInfo, search_graph};
53+
use rustc_type_ir::WithCachedTypeInfo;
54+
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags};
5855
use tracing::{debug, instrument};
5956

6057
use crate::arena::Arena;
@@ -1819,14 +1816,15 @@ impl<'tcx> TyCtxtAt<'tcx> {
18191816

18201817
impl<'tcx> TyCtxt<'tcx> {
18211818
/// `tcx`-dependent operations performed for every created definition.
1819+
#[instrument(level = "trace", skip(self))]
18221820
pub fn create_def(
18231821
self,
18241822
parent: LocalDefId,
18251823
name: Symbol,
18261824
def_kind: DefKind,
18271825
) -> TyCtxtFeed<'tcx, LocalDefId> {
18281826
let data = def_kind.def_path_data(name);
1829-
// The following call has the side effect of modifying the tables inside `definitions`.
1827+
// The following create_def calls have the side effect of modifying the tables inside `definitions`.
18301828
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
18311829
// decode the on-disk cache.
18321830
//
@@ -1839,31 +1837,47 @@ impl<'tcx> TyCtxt<'tcx> {
18391837
// This is fine because:
18401838
// - those queries are `eval_always` so we won't miss their result changing;
18411839
// - this write will have happened before these queries are called.
1842-
let def_id = self.untracked.definitions.write().create_def(parent, data);
1843-
1844-
// This function modifies `self.definitions` using a side-effect.
1845-
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1846-
tls::with_context(|icx| {
1840+
let def_id = tls::with_context(|icx| {
18471841
match icx.task_deps {
18481842
// Always gets rerun anyway, so nothing to replay
1849-
TaskDepsRef::EvalAlways => {}
1843+
TaskDepsRef::EvalAlways => {
1844+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1845+
trace!(?def_id, "eval always");
1846+
def_id
1847+
}
18501848
// Top-level queries like the resolver get rerun every time anyway
1851-
TaskDepsRef::Ignore => {}
1849+
TaskDepsRef::Ignore => {
1850+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1851+
trace!(?def_id, "ignore");
1852+
def_id
1853+
}
18521854
TaskDepsRef::Forbid => bug!(
18531855
"cannot create definition {parent:?}, {name:?}, {def_kind:?} without being able to register task dependencies"
18541856
),
18551857
TaskDepsRef::Allow(_) => {
1856-
icx.side_effects
1857-
.as_ref()
1858-
.unwrap()
1859-
.lock()
1860-
.definitions
1861-
.push(DefIdInfo { parent, data });
1858+
let (def_id, hash) =
1859+
self.untracked.definitions.write().create_def(parent, data);
1860+
trace!(?def_id, "record side effects");
1861+
1862+
icx.side_effects.as_ref().unwrap().lock().definitions.push(DefIdInfo {
1863+
parent,
1864+
data,
1865+
hash,
1866+
});
1867+
def_id
18621868
}
18631869
TaskDepsRef::Replay { prev_side_effects, created_def_ids } => {
1870+
trace!(?created_def_ids, "replay side effects");
1871+
trace!("num_defs : {}", prev_side_effects.definitions.len());
18641872
let index = created_def_ids.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
18651873
let prev_info = &prev_side_effects.definitions[index];
1866-
assert_eq!(*prev_info, DefIdInfo { parent, data });
1874+
let def_id = self.untracked.definitions.read().local_def_path_hash_to_def_id(
1875+
prev_info.hash,
1876+
&"should have already recreated def id in try_mark_green",
1877+
);
1878+
assert_eq!(prev_info.data, data);
1879+
assert_eq!(prev_info.parent, parent);
1880+
def_id
18671881
}
18681882
}
18691883
});

compiler/rustc_query_impl/src/plumbing.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl QueryContext for QueryCtxt<'_> {
173173
});
174174
}
175175

176-
#[tracing::instrument(level = "trace", skip(self))]
176+
#[tracing::instrument(level = "trace", skip(self, side_effects))]
177177
fn apply_side_effects(self, side_effects: QuerySideEffects) {
178178
let dcx = self.dep_context().sess().dcx();
179179
let QuerySideEffects { diagnostics, definitions } = side_effects;
@@ -182,8 +182,9 @@ impl QueryContext for QueryCtxt<'_> {
182182
dcx.emit_diagnostic(diagnostic);
183183
}
184184

185-
for DefIdInfo { parent, data } in definitions {
186-
self.tcx.untracked().definitions.write().create_def(parent, data);
185+
for DefIdInfo { parent, data, hash } in definitions {
186+
let (_def_id, h) = self.tcx.untracked().definitions.write().create_def(parent, data);
187+
debug_assert_eq!(h, hash);
187188
}
188189
}
189190
}

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ impl<D: Deps> DepGraphData<D> {
909909
Some(dep_node_index)
910910
}
911911

912-
/// Atomically emits some loaded diagnostics.
912+
/// Atomically emits some loaded side effects.
913913
/// This may be called concurrently on multiple threads for the same dep node.
914914
#[cold]
915915
#[inline(never)]
@@ -925,7 +925,7 @@ impl<D: Deps> DepGraphData<D> {
925925
// We were the first to insert the node in the set so this thread
926926
// must process side effects
927927

928-
// Promote the previous diagnostics to the current session.
928+
// Promote the previous side effects to the current session.
929929
qcx.store_side_effects(dep_node_index, side_effects.clone());
930930

931931
qcx.apply_side_effects(side_effects);

compiler/rustc_query_system/src/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::stable_hasher::Hash64;
1515
use rustc_data_structures::sync::Lock;
1616
use rustc_errors::DiagInner;
1717
use rustc_hir::def::DefKind;
18+
use rustc_hir::def_id::DefPathHash;
1819
use rustc_macros::{Decodable, Encodable};
1920
use rustc_span::Span;
2021
use rustc_span::def_id::{DefId, LocalDefId};
@@ -85,6 +86,7 @@ pub struct QuerySideEffects {
8586
pub struct DefIdInfo {
8687
pub parent: LocalDefId,
8788
pub data: rustc_hir::definitions::DefPathData,
89+
pub hash: DefPathHash,
8890
}
8991

9092
impl QuerySideEffects {

0 commit comments

Comments
 (0)