Skip to content

Commit 46c6750

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 1cf55c1 commit 46c6750

File tree

5 files changed

+48
-27
lines changed

5 files changed

+48
-27
lines changed

Diff for: compiler/rustc_hir/src/definitions.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ impl Definitions {
344344
}
345345

346346
/// Adds a definition with a parent definition.
347-
pub fn create_def(&mut self, parent: LocalDefId, data: DefPathData) -> LocalDefId {
347+
pub fn create_def(
348+
&mut self,
349+
parent: LocalDefId,
350+
data: DefPathData,
351+
) -> (LocalDefId, DefPathHash) {
348352
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
349353
// reference to `Definitions` and we're already holding a mutable reference.
350354
debug!(
@@ -373,7 +377,7 @@ impl Definitions {
373377
debug!("create_def: after disambiguation, key = {:?}", key);
374378

375379
// Create the definition.
376-
LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }
380+
(LocalDefId { local_def_index: self.table.allocate(key, def_path_hash) }, def_path_hash)
377381
}
378382

379383
#[inline(always)]

Diff for: 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;
@@ -1796,14 +1793,15 @@ impl<'tcx> TyCtxtAt<'tcx> {
17961793

17971794
impl<'tcx> TyCtxt<'tcx> {
17981795
/// `tcx`-dependent operations performed for every created definition.
1796+
#[instrument(level = "trace", skip(self))]
17991797
pub fn create_def(
18001798
self,
18011799
parent: LocalDefId,
18021800
name: Symbol,
18031801
def_kind: DefKind,
18041802
) -> TyCtxtFeed<'tcx, LocalDefId> {
18051803
let data = def_kind.def_path_data(name);
1806-
// The following call has the side effect of modifying the tables inside `definitions`.
1804+
// The following create_def calls have the side effect of modifying the tables inside `definitions`.
18071805
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
18081806
// decode the on-disk cache.
18091807
//
@@ -1816,31 +1814,47 @@ impl<'tcx> TyCtxt<'tcx> {
18161814
// This is fine because:
18171815
// - those queries are `eval_always` so we won't miss their result changing;
18181816
// - this write will have happened before these queries are called.
1819-
let def_id = self.untracked.definitions.write().create_def(parent, data);
1820-
1821-
// This function modifies `self.definitions` using a side-effect.
1822-
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1823-
tls::with_context(|icx| {
1817+
let def_id = tls::with_context(|icx| {
18241818
match icx.task_deps {
18251819
// Always gets rerun anyway, so nothing to replay
1826-
TaskDepsRef::EvalAlways => {}
1820+
TaskDepsRef::EvalAlways => {
1821+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1822+
trace!(?def_id, "eval always");
1823+
def_id
1824+
}
18271825
// Top-level queries like the resolver get rerun every time anyway
1828-
TaskDepsRef::Ignore => {}
1826+
TaskDepsRef::Ignore => {
1827+
let def_id = self.untracked.definitions.write().create_def(parent, data).0;
1828+
trace!(?def_id, "ignore");
1829+
def_id
1830+
}
18291831
TaskDepsRef::Forbid => bug!(
18301832
"cannot create definition {parent:?}, {name:?}, {def_kind:?} without being able to register task dependencies"
18311833
),
18321834
TaskDepsRef::Allow(_) => {
1833-
icx.side_effects
1834-
.as_ref()
1835-
.unwrap()
1836-
.lock()
1837-
.definitions
1838-
.push(DefIdInfo { parent, data });
1835+
let (def_id, hash) =
1836+
self.untracked.definitions.write().create_def(parent, data);
1837+
trace!(?def_id, "record side effects");
1838+
1839+
icx.side_effects.as_ref().unwrap().lock().definitions.push(DefIdInfo {
1840+
parent,
1841+
data,
1842+
hash,
1843+
});
1844+
def_id
18391845
}
18401846
TaskDepsRef::Replay { prev_side_effects, created_def_ids } => {
1847+
trace!(?created_def_ids, "replay side effects");
1848+
trace!("num_defs : {}", prev_side_effects.definitions.len());
18411849
let index = created_def_ids.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
18421850
let prev_info = &prev_side_effects.definitions[index];
1843-
assert_eq!(*prev_info, DefIdInfo { parent, data });
1851+
let def_id = self.untracked.definitions.read().local_def_path_hash_to_def_id(
1852+
prev_info.hash,
1853+
&"should have already recreated def id in try_mark_green",
1854+
);
1855+
assert_eq!(prev_info.data, data);
1856+
assert_eq!(prev_info.parent, parent);
1857+
def_id
18441858
}
18451859
}
18461860
});

Diff for: 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
}

Diff for: 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);

Diff for: 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)