Skip to content

Commit 63a9f90

Browse files
committed
Make create_def invoke a helper query if used from a recorded query
1 parent e490865 commit 63a9f90

File tree

6 files changed

+53
-24
lines changed

6 files changed

+53
-24
lines changed

compiler/rustc_hir/src/definitions.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::hash::Hash;
1010
use rustc_data_structures::stable_hasher::StableHasher;
1111
use rustc_data_structures::unord::UnordMap;
1212
use rustc_hashes::Hash64;
13-
use rustc_index::IndexVec;
14-
use rustc_macros::{Decodable, Encodable};
13+
use rustc_index::{IndexVec, static_assert_size};
14+
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1515
use rustc_span::{Symbol, kw, sym};
1616
use tracing::{debug, instrument};
1717

@@ -281,7 +281,7 @@ impl DefPath {
281281
}
282282

283283
/// New variants should only be added in synchronization with `enum DefKind`.
284-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable)]
284+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Encodable, Decodable, HashStable_Generic)]
285285
pub enum DefPathData {
286286
// Root: these should only be used for the root nodes, because
287287
// they are treated specially by the `def_path` function.
@@ -325,6 +325,8 @@ pub enum DefPathData {
325325
NestedStatic,
326326
}
327327

328+
static_assert_size!(DefPathData, 8);
329+
328330
impl Definitions {
329331
pub fn def_path_table(&self) -> &DefPathTable {
330332
&self.table
@@ -386,20 +388,18 @@ impl Definitions {
386388
&mut self,
387389
parent: LocalDefId,
388390
data: DefPathData,
389-
disambiguator: &mut DisambiguatorState,
391+
disambiguator: u32,
390392
) -> LocalDefId {
391393
// We can't use `Debug` implementation for `LocalDefId` here, since it tries to acquire a
392394
// reference to `Definitions` and we're already holding a mutable reference.
393395
debug!(
394-
"create_def(parent={}, data={data:?})",
396+
"create_def(parent={}, data={data:?}, disambiguator: {disambiguator})",
395397
self.def_path(parent).to_string_no_crate_verbose(),
396398
);
397399

398400
// The root node must be created in `new()`.
399401
assert!(data != DefPathData::CrateRoot);
400402

401-
// Find the next free disambiguator for this key.
402-
let disambiguator = disambiguator.next(parent, data);
403403
let key = DefKey {
404404
parent: Some(parent.local_def_index),
405405
disambiguated_data: DisambiguatedDefPathData { data, disambiguator },

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,7 @@ pub fn provide(providers: &mut Providers) {
239239
providers.in_scope_traits_map = |tcx, id| {
240240
tcx.hir_crate(()).owners[id.def_id].as_owner().map(|owner_info| &owner_info.trait_map)
241241
};
242+
providers.create_def_raw = |tcx, (parent, data, disambiguator)| {
243+
tcx.untracked().definitions.write().create_def(parent, data, disambiguator)
244+
}
242245
}

compiler/rustc_middle/src/query/keys.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::ffi::OsStr;
44

55
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId, LocalModDefId, ModDefId};
6+
use rustc_hir::definitions::DefPathData;
67
use rustc_hir::hir_id::{HirId, OwnerId};
78
use rustc_query_system::dep_graph::DepNodeIndex;
89
use rustc_query_system::query::{DefIdCache, DefaultCache, SingleCache, VecCache};
@@ -265,6 +266,14 @@ impl Key for (LocalDefId, LocalDefId) {
265266
}
266267
}
267268

269+
impl Key for (LocalDefId, DefPathData, u32) {
270+
type Cache<V> = DefaultCache<Self, V>;
271+
272+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
273+
self.0.default_span(tcx)
274+
}
275+
}
276+
268277
impl Key for (DefId, Ident) {
269278
type Cache<V> = DefaultCache<Self, V>;
270279

compiler/rustc_middle/src/query/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ rustc_queries! {
159159
desc { "getting the source span" }
160160
}
161161

162+
/// Used to handle incremental replays of [`TyCtxt::create_def`] invocations from tracked queries.
163+
query create_def_raw(key: (LocalDefId, rustc_hir::definitions::DefPathData, u32)) -> LocalDefId {
164+
desc { "generating a new def id" }
165+
}
166+
162167
/// Represents crate as a whole (as distinct from the top-level crate module).
163168
///
164169
/// If you call `tcx.hir_crate(())` we will have to assume that any change

compiler/rustc_middle/src/ty/context.rs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_hir::{self as hir, Attribute, HirId, Node, TraitCandidate};
4141
use rustc_index::IndexVec;
4242
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
4343
use rustc_query_system::cache::WithDepNode;
44-
use rustc_query_system::dep_graph::DepNodeIndex;
44+
use rustc_query_system::dep_graph::{DepNodeIndex, TaskDepsRef};
4545
use rustc_query_system::ich::StableHashingContext;
4646
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
4747
use rustc_session::config::CrateType;
@@ -1999,22 +1999,32 @@ impl<'tcx> TyCtxt<'tcx> {
19991999
disambiguator: &mut DisambiguatorState,
20002000
) -> TyCtxtFeed<'tcx, LocalDefId> {
20012001
let data = override_def_path_data.unwrap_or_else(|| def_kind.def_path_data(name));
2002-
// The following call has the side effect of modifying the tables inside `definitions`.
2003-
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
2004-
// decode the on-disk cache.
2005-
//
2006-
// Any LocalDefId which is used within queries, either as key or result, either:
2007-
// - has been created before the construction of the TyCtxt;
2008-
// - has been created by this call to `create_def`.
2009-
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
2010-
// comp. engine itself.
2011-
let def_id = self.untracked.definitions.write().create_def(parent, data, disambiguator);
2012-
2013-
// This function modifies `self.definitions` using a side-effect.
2014-
// We need to ensure that these side effects are re-run by the incr. comp. engine.
2015-
// Depending on the forever-red node will tell the graph that the calling query
2016-
// needs to be re-evaluated.
2017-
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
2002+
2003+
let def_id = tls::with_context(|icx| {
2004+
match icx.task_deps {
2005+
// Always gets rerun anyway, so nothing to replay
2006+
TaskDepsRef::EvalAlways |
2007+
// Top-level queries like the resolver get rerun every time anyway
2008+
TaskDepsRef::Ignore => {
2009+
// The following call has the side effect of modifying the tables inside `definitions`.
2010+
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
2011+
// decode the on-disk cache.
2012+
//
2013+
// Any LocalDefId which is used within queries, either as key or result, either:
2014+
// - has been created before the construction of the TyCtxt;
2015+
// - has been created by this call to `create_def`.
2016+
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
2017+
// comp. engine itself.
2018+
self.untracked.definitions.write().create_def(parent, data, disambiguator.next(parent, data))
2019+
}
2020+
TaskDepsRef::Forbid => bug!(
2021+
"cannot create definition {parent:?} {data:?} without being able to register task dependencies"
2022+
),
2023+
TaskDepsRef::Allow(_) => {
2024+
self.create_def_raw((parent, data, disambiguator.next(parent, data)))
2025+
}
2026+
}
2027+
});
20182028

20192029
let feed = TyCtxtFeed { tcx: self, key: def_id };
20202030
feed.def_kind(def_kind);

tests/run-make/rustc-crates-on-stable/rmake.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ fn main() {
88
cargo()
99
// Ensure `proc-macro2`'s nightly detection is disabled
1010
.env("RUSTC_STAGE", "0")
11+
// Avoid loading stale data from the stage 0 compiler in case that one was incremental
12+
.env("CARGO_INCREMENTAL", "0")
1113
.env("RUSTC", rustc_path())
1214
// We want to disallow all nightly features to simulate a stable build
1315
.env("RUSTFLAGS", "-Zallow-features=")

0 commit comments

Comments
 (0)