1 use crate::ich::StableHashingContext;
2 use crate::ty::{self, TyCtxt};
3 use rustc_data_structures::profiling::SelfProfilerRef;
4 use rustc_data_structures::sync::Lock;
5 use rustc_data_structures::thin_vec::ThinVec;
6 use rustc_errors::Diagnostic;
7 use rustc_hir::def_id::LocalDefId;
11 pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
12 pub use rustc_query_system::dep_graph::{
13 debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
14 WorkProduct, WorkProductId,
17 pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
19 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
20 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
21 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
22 pub type PreviousDepGraph = rustc_query_system::dep_graph::PreviousDepGraph<DepKind>;
23 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
25 impl rustc_query_system::dep_graph::DepKind for DepKind {
26 const NULL: Self = DepKind::Null;
29 fn can_reconstruct_query_key(&self) -> bool {
30 DepKind::can_reconstruct_query_key(self)
34 fn is_eval_always(&self) -> bool {
39 fn has_params(&self) -> bool {
43 fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "{:?}", node.kind)?;
46 if !node.kind.has_params && !node.kind.is_anon {
52 ty::tls::with_opt(|opt_tcx| {
53 if let Some(tcx) = opt_tcx {
54 if let Some(def_id) = node.extract_def_id(tcx) {
55 write!(f, "{}", tcx.def_path_debug_str(def_id))?;
56 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
59 write!(f, "{}", node.hash)?;
62 write!(f, "{}", node.hash)?;
70 fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
74 ty::tls::with_context(|icx| {
75 let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
77 ty::tls::enter_context(&icx, |_| op())
81 fn read_deps<OP>(op: OP)
83 OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
85 ty::tls::with_context_opt(|icx| {
86 let icx = if let Some(icx) = icx { icx } else { return };
92 impl<'tcx> DepContext for TyCtxt<'tcx> {
93 type DepKind = DepKind;
94 type StableHashingContext = StableHashingContext<'tcx>;
96 fn register_reused_dep_node(&self, dep_node: &DepNode) {
97 if let Some(cache) = self.queries.on_disk_cache.as_ref() {
98 cache.register_reused_dep_node(*self, dep_node)
102 fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
103 TyCtxt::create_stable_hashing_context(*self)
106 fn debug_dep_tasks(&self) -> bool {
107 self.sess.opts.debugging_opts.dep_tasks
109 fn debug_dep_node(&self) -> bool {
110 self.sess.opts.debugging_opts.incremental_info
111 || self.sess.opts.debugging_opts.query_dep_graph
114 fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
115 // FIXME: This match is just a workaround for incremental bugs and should
116 // be removed. https://github.com/rust-lang/rust/issues/62649 is one such
117 // bug that must be fixed before removing this.
118 match dep_node.kind {
119 DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
120 if let Some(def_id) = dep_node.extract_def_id(*self) {
121 if def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) {
122 if dep_node.kind == DepKind::CrateMetadata {
123 // The `DefPath` has corresponding node,
124 // and that node should have been marked
125 // either red or green in `data.colors`.
127 "DepNode {:?} should have been \
128 pre-marked as red or green but wasn't.",
133 // This `DefPath` does not have a
134 // corresponding `DepNode` (e.g. a
135 // struct field), and the ` DefPath`
136 // collided with the `DefPath` of a
137 // proper item that existed in the
138 // previous compilation session.
140 // Since the given `DefPath` does not
141 // denote the item that previously
142 // existed, we just fail to mark green.
146 // If the node does not exist anymore, we
147 // just fail to mark green.
152 // For other kinds of nodes it's OK to be
157 debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
158 ty::query::force_from_dep_node(*self, dep_node)
161 fn has_errors_or_delayed_span_bugs(&self) -> bool {
162 self.sess.has_errors_or_delayed_span_bugs()
165 fn diagnostic(&self) -> &rustc_errors::Handler {
166 self.sess.diagnostic()
169 // Interactions with on_disk_cache
170 fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
171 (dep_node.kind.try_load_from_on_disk_cache)(*self, dep_node)
174 fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
178 .map(|c| c.load_diagnostics(*self, prev_dep_node_index))
182 fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
183 if let Some(c) = self.queries.on_disk_cache.as_ref() {
184 c.store_diagnostics(dep_node_index, diagnostics)
188 fn store_diagnostics_for_anon_node(
190 dep_node_index: DepNodeIndex,
191 diagnostics: ThinVec<Diagnostic>,
193 if let Some(c) = self.queries.on_disk_cache.as_ref() {
194 c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
198 fn profiler(&self) -> &SelfProfilerRef {
203 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
204 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
205 def_id == hir_id.owner