]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Make can_reconstruct_query_key a function pointer.
[rust.git] / compiler / rustc_middle / src / dep_graph / mod.rs
1 use crate::ich::StableHashingContext;
2 use crate::ty::query::try_load_from_on_disk_cache;
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::LocalDefId;
9
10 mod dep_node;
11
12 pub(crate) use rustc_query_system::dep_graph::DepNodeParams;
13 pub use rustc_query_system::dep_graph::{
14     debug, hash_result, DepContext, DepNodeColor, DepNodeIndex, SerializedDepNodeIndex,
15     WorkProduct, WorkProductId,
16 };
17
18 pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
19
20 pub type DepGraph = rustc_query_system::dep_graph::DepGraph<DepKind>;
21 pub type TaskDeps = rustc_query_system::dep_graph::TaskDeps<DepKind>;
22 pub type DepGraphQuery = rustc_query_system::dep_graph::DepGraphQuery<DepKind>;
23 pub type PreviousDepGraph = rustc_query_system::dep_graph::PreviousDepGraph<DepKind>;
24 pub type SerializedDepGraph = rustc_query_system::dep_graph::SerializedDepGraph<DepKind>;
25
26 impl rustc_query_system::dep_graph::DepKind for DepKind {
27     const NULL: Self = DepKind::Null;
28
29     #[inline(always)]
30     fn can_reconstruct_query_key(&self) -> bool {
31         DepKind::can_reconstruct_query_key(self)
32     }
33
34     #[inline(always)]
35     fn is_eval_always(&self) -> bool {
36         self.is_eval_always
37     }
38
39     #[inline(always)]
40     fn has_params(&self) -> bool {
41         self.has_params
42     }
43
44     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45         write!(f, "{:?}", node.kind)?;
46
47         if !node.kind.has_params && !node.kind.is_anon {
48             return Ok(());
49         }
50
51         write!(f, "(")?;
52
53         ty::tls::with_opt(|opt_tcx| {
54             if let Some(tcx) = opt_tcx {
55                 if let Some(def_id) = node.extract_def_id(tcx) {
56                     write!(f, "{}", tcx.def_path_debug_str(def_id))?;
57                 } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
58                     write!(f, "{}", s)?;
59                 } else {
60                     write!(f, "{}", node.hash)?;
61                 }
62             } else {
63                 write!(f, "{}", node.hash)?;
64             }
65             Ok(())
66         })?;
67
68         write!(f, ")")
69     }
70
71     fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
72     where
73         OP: FnOnce() -> R,
74     {
75         ty::tls::with_context(|icx| {
76             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
77
78             ty::tls::enter_context(&icx, |_| op())
79         })
80     }
81
82     fn read_deps<OP>(op: OP)
83     where
84         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
85     {
86         ty::tls::with_context_opt(|icx| {
87             let icx = if let Some(icx) = icx { icx } else { return };
88             op(icx.task_deps)
89         })
90     }
91 }
92
93 impl<'tcx> DepContext for TyCtxt<'tcx> {
94     type DepKind = DepKind;
95     type StableHashingContext = StableHashingContext<'tcx>;
96
97     fn register_reused_dep_node(&self, dep_node: &DepNode) {
98         if let Some(cache) = self.queries.on_disk_cache.as_ref() {
99             cache.register_reused_dep_node(*self, dep_node)
100         }
101     }
102
103     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
104         TyCtxt::create_stable_hashing_context(*self)
105     }
106
107     fn debug_dep_tasks(&self) -> bool {
108         self.sess.opts.debugging_opts.dep_tasks
109     }
110     fn debug_dep_node(&self) -> bool {
111         self.sess.opts.debugging_opts.incremental_info
112             || self.sess.opts.debugging_opts.query_dep_graph
113     }
114
115     fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
116         // FIXME: This match is just a workaround for incremental bugs and should
117         // be removed. https://github.com/rust-lang/rust/issues/62649 is one such
118         // bug that must be fixed before removing this.
119         match dep_node.kind {
120             DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
121                 if let Some(def_id) = dep_node.extract_def_id(*self) {
122                     if def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) {
123                         if dep_node.kind == DepKind::CrateMetadata {
124                             // The `DefPath` has corresponding node,
125                             // and that node should have been marked
126                             // either red or green in `data.colors`.
127                             bug!(
128                                 "DepNode {:?} should have been \
129                              pre-marked as red or green but wasn't.",
130                                 dep_node
131                             );
132                         }
133                     } else {
134                         // This `DefPath` does not have a
135                         // corresponding `DepNode` (e.g. a
136                         // struct field), and the ` DefPath`
137                         // collided with the `DefPath` of a
138                         // proper item that existed in the
139                         // previous compilation session.
140                         //
141                         // Since the given `DefPath` does not
142                         // denote the item that previously
143                         // existed, we just fail to mark green.
144                         return false;
145                     }
146                 } else {
147                     // If the node does not exist anymore, we
148                     // just fail to mark green.
149                     return false;
150                 }
151             }
152             _ => {
153                 // For other kinds of nodes it's OK to be
154                 // forced.
155             }
156         }
157
158         debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
159         ty::query::force_from_dep_node(*self, dep_node)
160     }
161
162     fn has_errors_or_delayed_span_bugs(&self) -> bool {
163         self.sess.has_errors_or_delayed_span_bugs()
164     }
165
166     fn diagnostic(&self) -> &rustc_errors::Handler {
167         self.sess.diagnostic()
168     }
169
170     // Interactions with on_disk_cache
171     fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
172         try_load_from_on_disk_cache(*self, dep_node)
173     }
174
175     fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
176         self.queries
177             .on_disk_cache
178             .as_ref()
179             .map(|c| c.load_diagnostics(*self, prev_dep_node_index))
180             .unwrap_or_default()
181     }
182
183     fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
184         if let Some(c) = self.queries.on_disk_cache.as_ref() {
185             c.store_diagnostics(dep_node_index, diagnostics)
186         }
187     }
188
189     fn store_diagnostics_for_anon_node(
190         &self,
191         dep_node_index: DepNodeIndex,
192         diagnostics: ThinVec<Diagnostic>,
193     ) {
194         if let Some(c) = self.queries.on_disk_cache.as_ref() {
195             c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
196         }
197     }
198
199     fn profiler(&self) -> &SelfProfilerRef {
200         &self.prof
201     }
202 }
203
204 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
205     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
206     def_id == hir_id.owner
207 }