Skip to content

Commit 50c56c5

Browse files
committed
Create a forever red node and use it to force side effects.
1 parent 9d6214f commit 50c56c5

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

compiler/rustc_middle/src/query/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ rustc_queries! {
2020
desc { "trigger a delay span bug" }
2121
}
2222

23-
/// Create a new definition within the incr. comp. engine.
24-
query register_def(_: ty::RawLocalDefId) -> LocalDefId {
25-
eval_always
26-
desc { "register a DefId with the incr. comp. engine" }
27-
}
28-
2923
query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
3024
eval_always
3125
no_hash

compiler/rustc_middle/src/ty/context.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
9090
fn serialize(&self, tcx: TyCtxt<'tcx>, encoder: &mut FileEncoder) -> FileEncodeResult;
9191
}
9292

93-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
94-
pub struct RawLocalDefId(LocalDefId);
95-
9693
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
9794
/// except through the error-reporting functions on a [`tcx`][TyCtxt].
9895
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
@@ -1405,23 +1402,15 @@ impl<'tcx> TyCtxt<'tcx> {
14051402
let def_id = self.definitions.write().create_def(parent, data, expn_id, span);
14061403

14071404
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1408-
// When the incr. comp. engine considers marking this query as green, eval_always requires
1409-
// we run the function to run. To invoke it, the parameter cannot be reconstructed from
1410-
// the DepNode, so the caller query is run. Luckily, we are inside the caller query,
1411-
// therefore the definition is properly created.
1412-
debug_assert!({
1413-
use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
1414-
self.is_eval_always(crate::dep_graph::DepKind::register_def)
1415-
&& !<RawLocalDefId as DepNodeParams<TyCtxt<'_>>>::fingerprint_style()
1416-
.reconstructible()
1417-
});
1405+
use rustc_query_system::dep_graph::DepNodeIndex;
1406+
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
14181407

14191408
// Any LocalDefId which is used within queries, either as key or result, either:
14201409
// - has been created before the construction of the TyCtxt;
1421-
// - has been created by this call to `register_def`.
1410+
// - has been created by this call to `create_def`.
14221411
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
14231412
// comp. engine itself.
1424-
self.register_def(RawLocalDefId(def_id))
1413+
def_id
14251414
}
14261415

14271416
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
@@ -3021,5 +3010,4 @@ pub fn provide(providers: &mut ty::query::Providers) {
30213010
// We want to check if the panic handler was defined in this crate
30223011
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
30233012
};
3024-
providers.register_def = |_, raw_id| raw_id.0;
30253013
}

compiler/rustc_middle/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub use self::consts::{
7070
pub use self::context::{
7171
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
7272
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData,
73-
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, RawLocalDefId, TyCtxt,
74-
TypeckResults, UserType, UserTypeAnnotationIndex,
73+
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType,
74+
UserTypeAnnotationIndex,
7575
};
7676
pub use self::instance::{Instance, InstanceDef};
7777
pub use self::list::List;

compiler/rustc_query_impl/src/keys.rs

-11
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,6 @@ impl Key for () {
3939
}
4040
}
4141

42-
impl Key for ty::RawLocalDefId {
43-
#[inline(always)]
44-
fn query_crate_is_local(&self) -> bool {
45-
true
46-
}
47-
48-
fn default_span(&self, _: TyCtxt<'_>) -> Span {
49-
DUMMY_SP
50-
}
51-
}
52-
5342
impl<'tcx> Key for ty::InstanceDef<'tcx> {
5443
#[inline(always)]
5544
fn query_crate_is_local(&self) -> bool {

compiler/rustc_query_system/src/dep_graph/graph.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ rustc_index::newtype_index! {
4343
impl DepNodeIndex {
4444
pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
4545
pub const SINGLETON_DEPENDENCYLESS_ANON_NODE: DepNodeIndex = DepNodeIndex::from_u32(0);
46+
pub const FOREVER_RED_NODE: DepNodeIndex = DepNodeIndex::from_u32(1);
4647
}
4748

4849
impl std::convert::From<DepNodeIndex> for QueryInvocationId {
@@ -124,6 +125,8 @@ impl<K: DepKind> DepGraph<K> {
124125
record_stats,
125126
);
126127

128+
let colors = DepNodeColorMap::new(prev_graph_node_count);
129+
127130
// Instantiate a dependy-less node only once for anonymous queries.
128131
let _green_node_index = current.intern_new_node(
129132
profiler,
@@ -133,14 +136,26 @@ impl<K: DepKind> DepGraph<K> {
133136
);
134137
debug_assert_eq!(_green_node_index, DepNodeIndex::SINGLETON_DEPENDENCYLESS_ANON_NODE);
135138

139+
// Instantiate a dependy-less red node only once for anonymous queries.
140+
let (_red_node_index, _prev_and_index) = current.intern_node(
141+
profiler,
142+
&prev_graph,
143+
DepNode { kind: DepKind::NULL, hash: Fingerprint::ZERO.into() },
144+
smallvec![],
145+
None,
146+
false,
147+
);
148+
debug_assert_eq!(_red_node_index, DepNodeIndex::FOREVER_RED_NODE);
149+
debug_assert!(matches!(_prev_and_index, None | Some((_, DepNodeColor::Red))));
150+
136151
DepGraph {
137152
data: Some(Lrc::new(DepGraphData {
138153
previous_work_products: prev_work_products,
139154
dep_node_debug: Default::default(),
140155
current,
141156
processed_side_effects: Default::default(),
142157
previous: prev_graph,
143-
colors: DepNodeColorMap::new(prev_graph_node_count),
158+
colors,
144159
debug_loaded_from_disk: Default::default(),
145160
})),
146161
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
@@ -965,6 +980,9 @@ impl<K: DepKind> CurrentDepGraph<K> {
965980
let nanos = duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64;
966981
let mut stable_hasher = StableHasher::new();
967982
nanos.hash(&mut stable_hasher);
983+
let anon_id_seed = stable_hasher.finish();
984+
// We rely on the fact that `anon_id_seed` is not zero when creating static nodes.
985+
debug_assert_ne!(anon_id_seed, Fingerprint::ZERO);
968986

969987
#[cfg(debug_assertions)]
970988
let forbidden_edge = match env::var("RUST_FORBID_DEP_GRAPH_EDGE") {
@@ -1000,7 +1018,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
10001018
)
10011019
}),
10021020
prev_index_to_index: Lock::new(IndexVec::from_elem_n(None, prev_graph_node_count)),
1003-
anon_id_seed: stable_hasher.finish(),
1021+
anon_id_seed,
10041022
#[cfg(debug_assertions)]
10051023
forbidden_edge,
10061024
total_read_count: AtomicU64::new(0),

0 commit comments

Comments
 (0)