]> git.lizzy.rs Git - rust.git/blob - src/librustc/dep_graph/mod.rs
5ccc0d281db0a1f01ea7feb0bdd7c6f9675e5619
[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 with_deps<OP, R>(task_deps: Option<&Lock<TaskDeps>>, op: OP) -> R
66     where
67         OP: FnOnce() -> R,
68     {
69         ty::tls::with_context(|icx| {
70             let icx = ty::tls::ImplicitCtxt { task_deps, ..icx.clone() };
71
72             ty::tls::enter_context(&icx, |_| op())
73         })
74     }
75
76     fn read_deps<OP>(op: OP) -> ()
77     where
78         OP: for<'a> FnOnce(Option<&'a Lock<TaskDeps>>) -> (),
79     {
80         ty::tls::with_context_opt(|icx| {
81             let icx = if let Some(icx) = icx { icx } else { return };
82             op(icx.task_deps)
83         })
84     }
85 }
86
87 impl<'tcx> DepContext for TyCtxt<'tcx> {
88     type DepKind = DepKind;
89     type StableHashingContext = StableHashingContext<'tcx>;
90
91     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
92         TyCtxt::create_stable_hashing_context(*self)
93     }
94
95     /// Extracts the DefId corresponding to this DepNode. This will work
96     /// if two conditions are met:
97     ///
98     /// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
99     /// 2. the item that the DefPath refers to exists in the current tcx.
100     ///
101     /// Condition (1) is determined by the DepKind variant of the
102     /// DepNode. Condition (2) might not be fulfilled if a DepNode
103     /// refers to something from the previous compilation session that
104     /// has been removed.
105     fn extract_def_id(&self, node: &DepNode) -> Option<DefId> {
106         if node.kind.can_reconstruct_query_key() {
107             let def_path_hash = DefPathHash(node.hash);
108             self.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
109         } else {
110             None
111         }
112     }
113
114     fn try_force_previous_green(&self, dep_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_dep_node.kind {
119             DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
120                 if let Some(def_id) = self.extract_def_id(dep_dep_node) {
121                     if def_id_corresponds_to_hir_dep_node(*self, def_id) {
122                         if dep_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_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_previous_green({:?}) --- trying to force", dep_dep_node);
158         ty::query::force_from_dep_node(*self, dep_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         use crate::mir::interpret::GlobalId;
172         use crate::ty::query::queries;
173         use crate::ty::query::QueryDescription;
174         rustc_dep_node_try_load_from_on_disk_cache!(dep_node, *self)
175     }
176
177     fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
178         self.queries.on_disk_cache.load_diagnostics(*self, prev_dep_node_index)
179     }
180
181     fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
182         self.queries.on_disk_cache.store_diagnostics(dep_node_index, diagnostics)
183     }
184
185     fn profiler(&self) -> &SelfProfilerRef {
186         &self.prof
187     }
188 }
189
190 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
191     let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
192     def_id.index == hir_id.owner.local_def_index
193 }
194
195 impl rustc_query_system::HashStableContext for StableHashingContext<'_> {
196     fn debug_dep_tasks(&self) -> bool {
197         self.sess().opts.debugging_opts.dep_tasks
198     }
199 }
200
201 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'tcx>> for TyCtxt<'tcx> {
202     fn get_stable_hashing_context(&self) -> StableHashingContext<'tcx> {
203         self.create_stable_hashing_context()
204     }
205 }
206
207 impl rustc_query_system::HashStableContextProvider<StableHashingContext<'a>>
208     for StableHashingContext<'a>
209 {
210     fn get_stable_hashing_context(&self) -> Self {
211         self.clone()
212     }
213 }