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)?;
66 ty::tls::with_context_opt(|icx| {
67 let icx = if let Some(icx) = icx { icx } else { return };
68 assert!(icx.task_deps.is_none(), "expected no task dependency tracking");
72 fn with_ignore_deps<OP, R>(op: OP) -> R
76 ty::tls::with_context(|icx| {
77 let icx = ty::tls::ImplicitCtxt { task_deps: None, ..icx.clone() };
79 ty::tls::enter_context(&icx, |_| op())
83 fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
87 ty::tls::with_context(|icx| {
88 let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
90 ty::tls::enter_context(&icx, |_| op())
94 fn read_deps<OP>(op: OP) -> ()
96 OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>) -> (),
98 ty::tls::with_context_opt(|icx| {
99 let icx = if let Some(icx) = icx { icx } else { return };
105 impl<'tcx> DepContext for TyCtxt<'tcx> {
106 type DepKind = DepKind;
107 type StableHashingContext = StableHashingContext<'tcx>;
109 fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
110 TyCtxt::create_stable_hashing_context(*self)
113 fn force_from_dep_node(&self, node: &DepNode) -> bool {
114 ty::query::force_from_dep_node(*self, node)
117 /// Extracts the DefId corresponding to this DepNode. This will work
118 /// if two conditions are met:
120 /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
121 /// 2. the item that the DefPath refers to exists in the current tcx.
123 /// Condition (1) is determined by the DepKind variant of the
124 /// DepNode. Condition (2) might not be fulfilled if a DepNode
125 /// refers to something from the previous compilation session that
126 /// has been removed.
127 fn extract_def_id(&self, node: &DepNode) -> Option<DefId> {
128 if node.kind.can_reconstruct_query_key() {
129 let def_path_hash = DefPathHash(node.hash);
130 self.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
136 fn ensure_node_can_be_forced(&self, dep_dep_node: &DepNode) -> Option<()> {
137 // FIXME: This match is just a workaround for incremental bugs and should
138 // be removed. https://github.com/rust-lang/rust/issues/62649 is one such
139 // bug that must be fixed before removing this.
140 match dep_dep_node.kind {
141 DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
142 if let Some(def_id) = self.extract_def_id(dep_dep_node) {
143 if def_id_corresponds_to_hir_dep_node(*self, def_id) {
144 if dep_dep_node.kind == DepKind::CrateMetadata {
145 // The `DefPath` has corresponding node,
146 // and that node should have been marked
147 // either red or green in `data.colors`.
149 "DepNode {:?} should have been \
150 pre-marked as red or green but wasn't.",
155 // This `DefPath` does not have a
156 // corresponding `DepNode` (e.g. a
157 // struct field), and the ` DefPath`
158 // collided with the `DefPath` of a
159 // proper item that existed in the
160 // previous compilation session.
162 // Since the given `DefPath` does not
163 // denote the item that previously
164 // existed, we just fail to mark green.
168 // If the node does not exist anymore, we
169 // just fail to mark green.
174 // For other kinds of nodes it's OK to be
181 fn has_errors_or_delayed_span_bugs(&self) -> bool {
182 self.sess.has_errors_or_delayed_span_bugs()
185 fn diagnostic(&self) -> &rustc_errors::Handler {
186 self.sess.diagnostic()
189 // Interactions with on_disk_cache
190 fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
191 use crate::mir::interpret::GlobalId;
192 use crate::ty::query::queries;
193 use crate::ty::query::QueryDescription;
194 rustc_dep_node_try_load_from_on_disk_cache!(dep_node, *self)
197 fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
198 self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
201 fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
202 self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
205 fn profiler(&self) -> &SelfProfilerRef {
210 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
211 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
212 def_id.index == hir_id.owner.local_def_index
215 impl rustc_query_system::HashStableContext for StableHashingContext<'_> {
216 fn debug_dep_tasks(&self) -> bool {
217 self.sess().opts.debugging_opts.dep_tasks
221 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'tcx>> for TyCtxt<'tcx> {
222 fn get_stable_hashing_context(&self) -> StableHashingContext<'tcx> {
223 self.create_stable_hashing_context()
227 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'a>>
228 for StableHashingContext<'a>
230 fn get_stable_hashing_context(&self) -> Self {