Skip to content

Commit 2689fd2

Browse files
committed
Auto merge of #45228 - theotherjimmy:ensure-query, r=michaelwoerister
incr.comp.: Introduce `ensure` and `ensure` typeck_tables_of Resolves #45210 In this Pull Request we introduce the `ensure` query/function. `ensure` has the semantics and type of the function `Q1` below: ```rust fn Q1::ensure(K){ Q(K); } ``` Further, `ensure` avoids the need to load the result from disk (or execute the provider, if we are not storing the results of Q to disk). @nikomatsakis
2 parents df095ce + d0cb4d0 commit 2689fd2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/librustc/ty/maps/plumbing.rs

+46
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,52 @@ macro_rules! define_maps {
344344
}
345345
}
346346

347+
/// Ensure that either this query has all green inputs or been executed.
348+
/// Executing query::ensure(D) is considered a read of the dep-node D.
349+
///
350+
/// This function is particularly useful when executing passes for their
351+
/// side-effects -- e.g., in order to report errors for erroneous programs.
352+
///
353+
/// Note: The optimization is only available during incr. comp.
354+
pub fn ensure(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> () {
355+
let dep_node = Self::to_dep_node(tcx, &key);
356+
357+
// Ensuring an "input" or anonymous query makes no sense
358+
assert!(!dep_node.kind.is_anon());
359+
assert!(!dep_node.kind.is_input());
360+
use dep_graph::DepNodeColor;
361+
match tcx.dep_graph.node_color(&dep_node) {
362+
Some(DepNodeColor::Green(dep_node_index)) => {
363+
tcx.dep_graph.read_index(dep_node_index);
364+
}
365+
Some(DepNodeColor::Red) => {
366+
// A DepNodeColor::Red DepNode means that this query was executed
367+
// before. We can not call `dep_graph.read()` here as we don't have
368+
// the DepNodeIndex. Instead, We call the query again to issue the
369+
// appropriate `dep_graph.read()` call. The performance cost this
370+
// introduces should be negligible as we'll immediately hit the
371+
// in-memory cache.
372+
let _ = tcx.$name(key);
373+
}
374+
None => {
375+
// Huh
376+
if !tcx.dep_graph.is_fully_enabled() {
377+
let _ = tcx.$name(key);
378+
return;
379+
}
380+
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
381+
Some(dep_node_index) => {
382+
debug_assert!(tcx.dep_graph.is_green(dep_node_index));
383+
tcx.dep_graph.read_index(dep_node_index);
384+
}
385+
None => {
386+
let _ = tcx.$name(key);
387+
}
388+
}
389+
}
390+
}
391+
}
392+
347393
fn compute_result(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> $V {
348394
let provider = tcx.maps.providers[key.map_crate()].$name;
349395
provider(tcx.global_tcx(), key)

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ fn typeck_item_bodies<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum
728728
debug_assert!(crate_num == LOCAL_CRATE);
729729
Ok(tcx.sess.track_errors(|| {
730730
for body_owner_def_id in tcx.body_owners() {
731-
tcx.typeck_tables_of(body_owner_def_id);
731+
ty::maps::queries::typeck_tables_of::ensure(tcx, body_owner_def_id);
732732
}
733733
})?)
734734
}

0 commit comments

Comments
 (0)