1 use crate::hir::map::definitions::DefPathHash;
2 use crate::ich::StableHashingContext;
3 use crate::ty::{self, TyCtxt};
4 use rustc_data_structures::profiling::SelfProfilerRef;
5 use rustc_data_structures::sync::Lock;
6 use rustc_data_structures::thin_vec::ThinVec;
7 use rustc_errors::Diagnostic;
8 use rustc_hir::def_id::DefId;
13 pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
14 pub use rustc_query_system::dep_graph::{
15 debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
16 WorkProduct, WorkProductFileKind, WorkProductId,
19 pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
20 pub use safe::AssertDepGraphSafe;
21 pub use safe::DepGraphSafe;
23 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
24 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
25 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
26 pub type PreviousDepGraph = rustc_query_system::dep_graph::PreviousDepGraph<DepKind>;
27 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
29 impl rustc_query_system::dep_graph::DepKind for DepKind {
30 fn is_eval_always(&self) -> bool {
31 DepKind::is_eval_always(self)
34 fn has_params(&self) -> bool {
35 DepKind::has_params(self)
38 fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 write!(f, "{:?}", node.kind)?;
41 if !node.kind.has_params() && !node.kind.is_anon() {
47 ty::tls::with_opt(|opt_tcx| {
48 if let Some(tcx) = opt_tcx {
49 if let Some(def_id) = tcx.extract_def_id(node) {
50 write!(f, "{}", tcx.def_path_debug_str(def_id))?;
51 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
54 write!(f, "{}", node.hash)?;
57 write!(f, "{}", node.hash)?;
65 fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
69 ty::tls::with_context(|icx| {
70 let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
72 ty::tls::enter_context(&icx, |_| op())
76 fn read_deps<OP>(op: OP) -> ()
78 OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>) -> (),
80 ty::tls::with_context_opt(|icx| {
81 let icx = if let Some(icx) = icx { icx } else { return };
87 impl<'tcx> DepContext for TyCtxt<'tcx> {
88 type DepKind = DepKind;
89 type StableHashingContext = StableHashingContext<'tcx>;
91 fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
92 TyCtxt::create_stable_hashing_context(*self)
95 /// Extracts the DefId corresponding to this DepNode. This will work
96 /// if two conditions are met:
98 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
99 /// 2. the item that the DefPath refers to exists in the current tcx.
101 /// Condition (1) is determined by the DepKind variant of the
102 /// DepNode. Condition (2) might not be fulfilled if a DepNode
103 /// refers to something from the previous compilation session that
104 /// has been removed.
105 fn extract_def_id(&self, node: &DepNode) -> Option<DefId> {
106 if node.kind.can_reconstruct_query_key() {
107 let def_path_hash = DefPathHash(node.hash);
108 self.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
114 fn try_force_previous_green(&self, dep_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_dep_node.kind {
119 DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
120 if let Some(def_id) = self.extract_def_id(dep_dep_node) {
121 if def_id_corresponds_to_hir_dep_node(*self, def_id) {
122 if dep_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_previous_green({:?}) --- trying to force", dep_dep_node);
158 ty::query::force_from_dep_node(*self, dep_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 use crate::mir::interpret::GlobalId;
172 use crate::ty::query::queries;
173 use crate::ty::query::QueryDescription;
174 rustc_dep_node_try_load_from_on_disk_cache!(dep_node, *self)
177 fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
178 self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
181 fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
182 self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
185 fn profiler(&self) -> &SelfProfilerRef {
190 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
191 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
192 def_id.index == hir_id.owner.local_def_index
195 impl rustc_query_system::HashStableContext for StableHashingContext<'_> {
196 fn debug_dep_tasks(&self) -> bool {
197 self.sess().opts.debugging_opts.dep_tasks
201 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'tcx>> for TyCtxt<'tcx> {
202 fn get_stable_hashing_context(&self) -> StableHashingContext<'tcx> {
203 self.create_stable_hashing_context()
207 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'a>>
208 for StableHashingContext<'a>
210 fn get_stable_hashing_context(&self) -> Self {