Skip to content

Commit 67b3e7c

Browse files
committed
Implement query ensure
`$query::ensure()` guarantees that one of two things is true after it returns: - The query has all green inputs. - The query has been executed. and counts as a read from the source query
1 parent d6d711d commit 67b3e7c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/librustc/ty/maps/plumbing.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,48 @@ 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+
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
364+
tcx.dep_graph.read_index(dep_node_index);
365+
}
366+
Some(DepNodeColor::Red) => {
367+
let _ = tcx.$name(key);
368+
}
369+
None => {
370+
// Huh
371+
if !tcx.dep_graph.is_fully_enabled() {
372+
let _ = tcx.$name(key);
373+
return;
374+
}
375+
match tcx.dep_graph.try_mark_green(tcx, &dep_node) {
376+
Some(dep_node_index) => {
377+
debug_assert!(tcx.dep_graph.is_green(dep_node_index));
378+
profq_msg!(tcx, ProfileQueriesMsg::CacheHit);
379+
tcx.dep_graph.read_index(dep_node_index);
380+
}
381+
None => {
382+
let _ = tcx.$name(key);
383+
}
384+
}
385+
}
386+
}
387+
}
388+
347389
fn compute_result(tcx: TyCtxt<'a, $tcx, 'lcx>, key: $K) -> $V {
348390
let provider = tcx.maps.providers[key.map_crate()].$name;
349391
provider(tcx.global_tcx(), key)

0 commit comments

Comments
 (0)