]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/mod.rs
79295b2f8276759202eab7cf9703c002a9b70549
[rust.git] / src / librustc / dep_graph / mod.rs
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;
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     fn is_eval_always(&self) -> bool {
31         DepKind::is_eval_always(self)
32     }
33
34     fn has_params(&self) -> bool {
35         DepKind::has_params(self)
36     }
37
38     fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39         write!(f, "{:?}", node.kind)?;
40
41         if !node.kind.has_params() && !node.kind.is_anon() {
42             return Ok(());
43         }
44
45         write!(f, "(")?;
46
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) {
52                     write!(f, "{}", s)?;
53                 } else {
54                     write!(f, "{}", node.hash)?;
55                 }
56             } else {
57                 write!(f, "{}", node.hash)?;
58             }
59             Ok(())
60         })?;
61
62         write!(f, ")")
63     }
64
65     fn assert_ignored() {
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");
69         })
70     }
71
72     fn with_ignore_deps<OP, R>(op: OP) -> R
73     where
74         OP: FnOnce() -> R,
75     {
76         ty::tls::with_context(|icx| {
77             let icx = ty::tls::ImplicitCtxt { task_deps: None, ..icx.clone() };
78
79             ty::tls::enter_context(&icx, |_| op())
80         })
81     }
82
83     fn with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
84     where
85         OP: FnOnce() -> R,
86     {
87         ty::tls::with_context(|icx| {
88             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
89
90             ty::tls::enter_context(&icx, |_| op())
91         })
92     }
93
94     fn read_deps<OP>(op: OP) -> ()
95     where
96         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>) -> (),
97     {
98         ty::tls::with_context_opt(|icx| {
99             let icx = if let Some(icx) = icx { icx } else { return };
100             op(icx.task_deps)
101         })
102     }
103 }
104
105 impl<'tcx> DepContext for TyCtxt<'tcx> {
106     type DepKind = DepKind;
107     type StableHashingContext = StableHashingContext<'tcx>;
108
109     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
110         TyCtxt::create_stable_hashing_context(*self)
111     }
112
113     fn force_from_dep_node(&self, node: &DepNode) -> bool {
114         ty::query::force_from_dep_node(*self, node)
115     }
116
117     /// Extracts the DefId corresponding to this DepNode. This will work
118     /// if two conditions are met:
119     ///
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.
122     ///
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()
131         } else {
132             None
133         }
134     }
135
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`.
148                             bug!(
149                                 "DepNode {:?} should have been \
150                              pre-marked as red or green but wasn't.",
151                                 dep_dep_node
152                             );
153                         }
154                     } else {
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.
161                         //
162                         // Since the given `DefPath` does not
163                         // denote the item that previously
164                         // existed, we just fail to mark green.
165                         return None;
166                     }
167                 } else {
168                     // If the node does not exist anymore, we
169                     // just fail to mark green.
170                     return None;
171                 }
172             }
173             _ => {
174                 // For other kinds of nodes it's OK to be
175                 // forced.
176             }
177         }
178         Some(())
179     }
180
181     fn has_errors_or_delayed_span_bugs(&self) -> bool {
182         self.sess.has_errors_or_delayed_span_bugs()
183     }
184
185     fn diagnostic(&self) -> &rustc_errors::Handler {
186         self.sess.diagnostic()
187     }
188
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)
195     }
196
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)
199     }
200
201     fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
202         self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
203     }
204
205     fn profiler(&self) -> &SelfProfilerRef {
206         &self.prof
207     }
208 }
209
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
213 }
214
215 impl rustc_query_system::HashStableContext for StableHashingContext<'_> {
216     fn debug_dep_tasks(&self) -> bool {
217         self.sess().opts.debugging_opts.dep_tasks
218     }
219 }
220
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()
224     }
225 }
226
227 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'a>>
228     for StableHashingContext<'a>
229 {
230     fn get_stable_hashing_context(&self) -> Self {
231         self.clone()
232     }
233 }