]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
88441af674d33c0f151ffa6266e1bb8581be19b8
[rust.git] / compiler / rustc_middle / src / dep_graph / mod.rs
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;
8
9 mod dep_node;
10
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,
15 };
16
17 pub use dep_node::{label_strs, DepConstructor, DepKind, DepNode, DepNodeExt};
18
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>;
24
25 impl rustc_query_system::dep_graph::DepKind for DepKind {
26     const NULL: Self = DepKind::Null;
27
28     #[inline(always)]
29     fn can_reconstruct_query_key(&self) -> bool {
30         DepKind::can_reconstruct_query_key(self)
31     }
32
33     #[inline(always)]
34     fn is_eval_always(&self) -> bool {
35         self.is_eval_always
36     }
37
38     #[inline(always)]
39     fn has_params(&self) -> bool {
40         self.has_params
41     }
42
43     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44         write!(f, "{:?}", node.kind)?;
45
46         if !node.kind.has_params && !node.kind.is_anon {
47             return Ok(());
48         }
49
50         write!(f, "(")?;
51
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) {
57                     write!(f, "{}", s)?;
58                 } else {
59                     write!(f, "{}", node.hash)?;
60                 }
61             } else {
62                 write!(f, "{}", node.hash)?;
63             }
64             Ok(())
65         })?;
66
67         write!(f, ")")
68     }
69
70     fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
71     where
72         OP: FnOnce() -> R,
73     {
74         ty::tls::with_context(|icx| {
75             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
76
77             ty::tls::enter_context(&icx, |_| op())
78         })
79     }
80
81     fn read_deps<OP>(op: OP)
82     where
83         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>),
84     {
85         ty::tls::with_context_opt(|icx| {
86             let icx = if let Some(icx) = icx { icx } else { return };
87             op(icx.task_deps)
88         })
89     }
90 }
91
92 impl<'tcx> DepContext for TyCtxt<'tcx> {
93     type DepKind = DepKind;
94     type StableHashingContext = StableHashingContext<'tcx>;
95
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)
99         }
100     }
101
102     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
103         TyCtxt::create_stable_hashing_context(*self)
104     }
105
106     fn debug_dep_tasks(&self) -> bool {
107         self.sess.opts.debugging_opts.dep_tasks
108     }
109     fn debug_dep_node(&self) -> bool {
110         self.sess.opts.debugging_opts.incremental_info
111             || self.sess.opts.debugging_opts.query_dep_graph
112     }
113
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`.
126                             bug!(
127                                 "DepNode {:?} should have been \
128                              pre-marked as red or green but wasn't.",
129                                 dep_node
130                             );
131                         }
132                     } else {
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.
139                         //
140                         // Since the given `DefPath` does not
141                         // denote the item that previously
142                         // existed, we just fail to mark green.
143                         return false;
144                     }
145                 } else {
146                     // If the node does not exist anymore, we
147                     // just fail to mark green.
148                     return false;
149                 }
150             }
151             _ => {
152                 // For other kinds of nodes it's OK to be
153                 // forced.
154             }
155         }
156
157         debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
158         ty::query::force_from_dep_node(*self, dep_node)
159     }
160
161     fn has_errors_or_delayed_span_bugs(&self) -> bool {
162         self.sess.has_errors_or_delayed_span_bugs()
163     }
164
165     fn diagnostic(&self) -> &rustc_errors::Handler {
166         self.sess.diagnostic()
167     }
168
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)
172     }
173
174     fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
175         self.queries
176             .on_disk_cache
177             .as_ref()
178             .map(|c| c.load_diagnostics(*self, prev_dep_node_index))
179             .unwrap_or_default()
180     }
181
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)
185         }
186     }
187
188     fn store_diagnostics_for_anon_node(
189         &self,
190         dep_node_index: DepNodeIndex,
191         diagnostics: ThinVec<Diagnostic>,
192     ) {
193         if let Some(c) = self.queries.on_disk_cache.as_ref() {
194             c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
195         }
196     }
197
198     fn profiler(&self) -> &SelfProfilerRef {
199         &self.prof
200     }
201 }
202
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
206 }