Skip to content

Commit 15530a1

Browse files
committed
Create a forever red node and use it to force side effects.
1 parent 682f576 commit 15530a1

File tree

5 files changed

+26
-37
lines changed

5 files changed

+26
-37
lines changed

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 6 deletions
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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
123123
type PlaceholderRegion = ty::PlaceholderRegion;
124124
}
125125

126-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
127-
pub struct RawLocalDefId(LocalDefId);
128-
129126
/// A type that is not publicly constructable. This prevents people from making [`TyKind::Error`]s
130127
/// except through the error-reporting functions on a [`tcx`][TyCtxt].
131128
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
@@ -1477,23 +1474,15 @@ impl<'tcx> TyCtxt<'tcx> {
14771474
let def_id = self.definitions.write().create_def(parent, data);
14781475

14791476
// We need to ensure that these side effects are re-run by the incr. comp. engine.
1480-
// When the incr. comp. engine considers marking this query as green, eval_always requires
1481-
// we run the function to run. To invoke it, the parameter cannot be reconstructed from
1482-
// the DepNode, so the caller query is run. Luckily, we are inside the caller query,
1483-
// therefore the definition is properly created.
1484-
debug_assert!({
1485-
use rustc_query_system::dep_graph::{DepContext, DepNodeParams};
1486-
self.is_eval_always(crate::dep_graph::DepKind::register_def)
1487-
&& !<RawLocalDefId as DepNodeParams<TyCtxt<'_>>>::fingerprint_style()
1488-
.reconstructible()
1489-
});
1477+
use rustc_query_system::dep_graph::DepNodeIndex;
1478+
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
14901479

14911480
// Any LocalDefId which is used within queries, either as key or result, either:
14921481
// - has been created before the construction of the TyCtxt;
1493-
// - has been created by this call to `register_def`.
1482+
// - has been created by this call to `create_def`.
14941483
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
14951484
// comp. engine itself.
1496-
self.register_def(RawLocalDefId(def_id))
1485+
def_id
14971486
}
14981487

14991488
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
@@ -3033,5 +3022,4 @@ pub fn provide(providers: &mut ty::query::Providers) {
30333022
// We want to check if the panic handler was defined in this crate
30343023
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
30353024
};
3036-
providers.register_def = |_, raw_id| raw_id.0;
30373025
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub use self::consts::{
7272
pub use self::context::{
7373
tls, CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
7474
CtxtInterners, DelaySpanBugEmitted, FreeRegionInfo, GeneratorDiagnosticData,
75-
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, RawLocalDefId, TyCtxt,
76-
TypeckResults, UserType, UserTypeAnnotationIndex,
75+
GeneratorInteriorTypeCause, GlobalCtxt, Lift, OnDiskCache, TyCtxt, TypeckResults, UserType,
76+
UserTypeAnnotationIndex,
7777
};
7878
pub use self::instance::{Instance, InstanceDef};
7979
pub use self::list::List;

compiler/rustc_query_impl/src/keys.rs

Lines changed: 0 additions & 11 deletions
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

Lines changed: 20 additions & 2 deletions
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)