]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/dep_graph/mod.rs
Use a field for is_eval_always.
[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::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     #[inline(always)]
30     fn is_eval_always(&self) -> bool {
31         self.is_eval_always
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) = node.extract_def_id(tcx) {
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     fn can_reconstruct_query_key(&self) -> bool {
87         DepKind::can_reconstruct_query_key(self)
88     }
89 }
90
91 impl<'tcx> DepContext for TyCtxt<'tcx> {
92     type DepKind = DepKind;
93     type StableHashingContext = StableHashingContext<'tcx>;
94
95     fn register_reused_dep_node(&self, dep_node: &DepNode) {
96         if let Some(cache) = self.queries.on_disk_cache.as_ref() {
97             cache.register_reused_dep_node(*self, dep_node)
98         }
99     }
100
101     fn create_stable_hashing_context(&self) -> Self::StableHashingContext {
102         TyCtxt::create_stable_hashing_context(*self)
103     }
104
105     fn debug_dep_tasks(&self) -> bool {
106         self.sess.opts.debugging_opts.dep_tasks
107     }
108     fn debug_dep_node(&self) -> bool {
109         self.sess.opts.debugging_opts.incremental_info
110             || self.sess.opts.debugging_opts.query_dep_graph
111     }
112
113     fn try_force_from_dep_node(&self, dep_node: &DepNode) -> bool {
114         // FIXME: This match is just a workaround for incremental bugs and should
115         // be removed. https://github.com/rust-lang/rust/issues/62649 is one such
116         // bug that must be fixed before removing this.
117         match dep_node.kind {
118             DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
119                 if let Some(def_id) = dep_node.extract_def_id(*self) {
120                     if def_id_corresponds_to_hir_dep_node(*self, def_id.expect_local()) {
121                         if dep_node.kind == DepKind::CrateMetadata {
122                             // The `DefPath` has corresponding node,
123                             // and that node should have been marked
124                             // either red or green in `data.colors`.
125                             bug!(
126                                 "DepNode {:?} should have been \
127                              pre-marked as red or green but wasn't.",
128                                 dep_node
129                             );
130                         }
131                     } else {
132                         // This `DefPath` does not have a
133                         // corresponding `DepNode` (e.g. a
134                         // struct field), and the ` DefPath`
135                         // collided with the `DefPath` of a
136                         // proper item that existed in the
137                         // previous compilation session.
138                         //
139                         // Since the given `DefPath` does not
140                         // denote the item that previously
141                         // existed, we just fail to mark green.
142                         return false;
143                     }
144                 } else {
145                     // If the node does not exist anymore, we
146                     // just fail to mark green.
147                     return false;
148                 }
149             }
150             _ => {
151                 // For other kinds of nodes it's OK to be
152                 // forced.
153             }
154         }
155
156         debug!("try_force_from_dep_node({:?}) --- trying to force", dep_node);
157         ty::query::force_from_dep_node(*self, dep_node)
158     }
159
160     fn has_errors_or_delayed_span_bugs(&self) -> bool {
161         self.sess.has_errors_or_delayed_span_bugs()
162     }
163
164     fn diagnostic(&self) -> &rustc_errors::Handler {
165         self.sess.diagnostic()
166     }
167
168     // Interactions with on_disk_cache
169     fn try_load_from_on_disk_cache(&self, dep_node: &DepNode) {
170         try_load_from_on_disk_cache(*self, dep_node)
171     }
172
173     fn load_diagnostics(&self, prev_dep_node_index: SerializedDepNodeIndex) -> Vec<Diagnostic> {
174         self.queries
175             .on_disk_cache
176             .as_ref()
177             .map(|c| c.load_diagnostics(*self, prev_dep_node_index))
178             .unwrap_or_default()
179     }
180
181     fn store_diagnostics(&self, dep_node_index: DepNodeIndex, diagnostics: ThinVec<Diagnostic>) {
182         if let Some(c) = self.queries.on_disk_cache.as_ref() {
183             c.store_diagnostics(dep_node_index, diagnostics)
184         }
185     }
186
187     fn store_diagnostics_for_anon_node(
188         &self,
189         dep_node_index: DepNodeIndex,
190         diagnostics: ThinVec<Diagnostic>,
191     ) {
192         if let Some(c) = self.queries.on_disk_cache.as_ref() {
193             c.store_diagnostics_for_anon_node(dep_node_index, diagnostics)
194         }
195     }
196
197     fn profiler(&self) -> &SelfProfilerRef {
198         &self.prof
199     }
200 }
201
202 fn def_id_corresponds_to_hir_dep_node(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
203     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
204     def_id == hir_id.owner
205 }