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