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