]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/plumbing.rs
Get rid of `fn recover`
[rust.git] / compiler / rustc_query_impl / src / plumbing.rs
1 //! The implementation of the query system itself. This defines the macros that
2 //! generate the actual methods on tcx which find and execute the provider,
3 //! manage the caches, and so forth.
4
5 use crate::keys::Key;
6 use crate::{on_disk_cache, Queries};
7 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
8 use rustc_data_structures::sync::Lock;
9 use rustc_errors::{Diagnostic, Handler};
10 use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
11 use rustc_middle::ty::tls::{self, ImplicitCtxt};
12 use rustc_middle::ty::{self, TyCtxt};
13 use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
14 use rustc_query_system::ich::StableHashingContext;
15 use rustc_query_system::query::{
16     force_query, QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects,
17     QueryStackFrame,
18 };
19 use std::any::Any;
20 use std::num::NonZeroU64;
21 use thin_vec::ThinVec;
22
23 #[derive(Copy, Clone)]
24 pub struct QueryCtxt<'tcx> {
25     pub tcx: TyCtxt<'tcx>,
26     pub queries: &'tcx Queries<'tcx>,
27 }
28
29 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
30     type Target = TyCtxt<'tcx>;
31
32     #[inline]
33     fn deref(&self) -> &Self::Target {
34         &self.tcx
35     }
36 }
37
38 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
39     type DepKind = rustc_middle::dep_graph::DepKind;
40     type DepContext = TyCtxt<'tcx>;
41
42     #[inline]
43     fn dep_context(&self) -> &Self::DepContext {
44         &self.tcx
45     }
46 }
47
48 impl QueryContext for QueryCtxt<'_> {
49     fn next_job_id(&self) -> QueryJobId {
50         QueryJobId(
51             NonZeroU64::new(
52                 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
53             )
54             .unwrap(),
55         )
56     }
57
58     fn current_query_job(&self) -> Option<QueryJobId> {
59         tls::with_related_context(**self, |icx| icx.query)
60     }
61
62     fn try_collect_active_jobs(&self) -> Option<QueryMap> {
63         self.queries.try_collect_active_jobs(**self)
64     }
65
66     // Interactions with on_disk_cache
67     fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
68         self.queries
69             .on_disk_cache
70             .as_ref()
71             .map(|c| c.load_side_effects(**self, prev_dep_node_index))
72             .unwrap_or_default()
73     }
74
75     fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
76         if let Some(c) = self.queries.on_disk_cache.as_ref() {
77             c.store_side_effects(dep_node_index, side_effects)
78         }
79     }
80
81     fn store_side_effects_for_anon_node(
82         &self,
83         dep_node_index: DepNodeIndex,
84         side_effects: QuerySideEffects,
85     ) {
86         if let Some(c) = self.queries.on_disk_cache.as_ref() {
87             c.store_side_effects_for_anon_node(dep_node_index, side_effects)
88         }
89     }
90
91     /// Executes a job by changing the `ImplicitCtxt` to point to the
92     /// new query job while it executes. It returns the diagnostics
93     /// captured during execution and the actual result.
94     #[inline(always)]
95     fn start_query<R>(
96         &self,
97         token: QueryJobId,
98         depth_limit: bool,
99         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
100         compute: impl FnOnce() -> R,
101     ) -> R {
102         // The `TyCtxt` stored in TLS has the same global interner lifetime
103         // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
104         // when accessing the `ImplicitCtxt`.
105         tls::with_related_context(**self, move |current_icx| {
106             if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
107                 self.depth_limit_error();
108             }
109
110             // Update the `ImplicitCtxt` to point to our new query job.
111             let new_icx = ImplicitCtxt {
112                 tcx: **self,
113                 query: Some(token),
114                 diagnostics,
115                 query_depth: current_icx.query_depth + depth_limit as usize,
116                 task_deps: current_icx.task_deps,
117             };
118
119             // Use the `ImplicitCtxt` while we execute the query.
120             tls::enter_context(&new_icx, |_| {
121                 rustc_data_structures::stack::ensure_sufficient_stack(compute)
122             })
123         })
124     }
125 }
126
127 impl<'tcx> QueryCtxt<'tcx> {
128     #[inline]
129     pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
130         let queries = tcx.queries.as_any();
131         let queries = unsafe {
132             let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
133             let queries = queries.downcast_ref().unwrap();
134             let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
135             queries
136         };
137         QueryCtxt { tcx, queries }
138     }
139
140     pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
141         self.queries.on_disk_cache.as_ref()
142     }
143
144     pub(super) fn encode_query_results(
145         self,
146         encoder: &mut on_disk_cache::CacheEncoder<'_, 'tcx>,
147         query_result_index: &mut on_disk_cache::EncodedDepNodeIndex,
148     ) {
149         macro_rules! encode_queries {
150             ($($query:ident,)*) => {
151                 $(
152                     on_disk_cache::encode_query_results::<_, super::queries::$query<'_>>(
153                         self,
154                         encoder,
155                         query_result_index
156                     );
157                 )*
158             }
159         }
160
161         rustc_cached_queries!(encode_queries!);
162     }
163
164     pub fn try_print_query_stack(
165         self,
166         query: Option<QueryJobId>,
167         handler: &Handler,
168         num_frames: Option<usize>,
169     ) -> usize {
170         rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
171     }
172 }
173
174 macro_rules! handle_cycle_error {
175     ([][$tcx: expr, $error:expr]) => {{
176         $error.emit();
177         Value::from_cycle_error($tcx)
178     }};
179     ([(fatal_cycle) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
180         $error.emit();
181         $tcx.sess.abort_if_errors();
182         unreachable!()
183     }};
184     ([(cycle_delay_bug) $($rest:tt)*][$tcx:expr, $error:expr]) => {{
185         $error.delay_as_bug();
186         Value::from_cycle_error($tcx)
187     }};
188     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
189         handle_cycle_error!([$($modifiers)*][$($args)*])
190     };
191 }
192
193 macro_rules! is_anon {
194     ([]) => {{
195         false
196     }};
197     ([(anon) $($rest:tt)*]) => {{
198         true
199     }};
200     ([$other:tt $($modifiers:tt)*]) => {
201         is_anon!([$($modifiers)*])
202     };
203 }
204
205 macro_rules! is_eval_always {
206     ([]) => {{
207         false
208     }};
209     ([(eval_always) $($rest:tt)*]) => {{
210         true
211     }};
212     ([$other:tt $($modifiers:tt)*]) => {
213         is_eval_always!([$($modifiers)*])
214     };
215 }
216
217 macro_rules! depth_limit {
218     ([]) => {{
219         false
220     }};
221     ([(depth_limit) $($rest:tt)*]) => {{
222         true
223     }};
224     ([$other:tt $($modifiers:tt)*]) => {
225         depth_limit!([$($modifiers)*])
226     };
227 }
228
229 macro_rules! hash_result {
230     ([]) => {{
231         Some(dep_graph::hash_result)
232     }};
233     ([(no_hash) $($rest:tt)*]) => {{
234         None
235     }};
236     ([$other:tt $($modifiers:tt)*]) => {
237         hash_result!([$($modifiers)*])
238     };
239 }
240
241 macro_rules! get_provider {
242     ([][$tcx:expr, $name:ident, $key:expr]) => {{
243         $tcx.queries.local_providers.$name
244     }};
245     ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
246         if $key.query_crate_is_local() {
247             $tcx.queries.local_providers.$name
248         } else {
249             $tcx.queries.extern_providers.$name
250         }
251     }};
252     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
253         get_provider!([$($modifiers)*][$($args)*])
254     };
255 }
256
257 pub(crate) fn create_query_frame<
258     'tcx,
259     K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
260 >(
261     tcx: QueryCtxt<'tcx>,
262     do_describe: fn(QueryCtxt<'tcx>, K) -> String,
263     key: K,
264     kind: DepKind,
265     name: &'static str,
266 ) -> QueryStackFrame {
267     // Disable visible paths printing for performance reasons.
268     // Showing visible path instead of any path is not that important in production.
269     let description = ty::print::with_no_visible_paths!(
270         // Force filename-line mode to avoid invoking `type_of` query.
271         ty::print::with_forced_impl_filename_line!(do_describe(tcx, key))
272     );
273     let description =
274         if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
275     let span = if kind == dep_graph::DepKind::def_span {
276         // The `def_span` query is used to calculate `default_span`,
277         // so exit to avoid infinite recursion.
278         None
279     } else {
280         Some(key.default_span(*tcx))
281     };
282     let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
283         // Try to avoid infinite recursion.
284         None
285     } else {
286         key.key_as_def_id()
287             .and_then(|def_id| def_id.as_local())
288             .and_then(|def_id| tcx.opt_def_kind(def_id))
289     };
290     let hash = || {
291         tcx.with_stable_hashing_context(|mut hcx| {
292             let mut hasher = StableHasher::new();
293             std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
294             key.hash_stable(&mut hcx, &mut hasher);
295             hasher.finish::<u64>()
296         })
297     };
298
299     QueryStackFrame::new(name, description, span, def_kind, hash)
300 }
301
302 pub(crate) fn try_load_from_on_disk_cache<'tcx, K: DepNodeParams<TyCtxt<'tcx>>, V>(
303     tcx: TyCtxt<'tcx>,
304     dep_node: DepNode,
305     cache_on_disk: fn(TyCtxt<'tcx>, &K) -> bool,
306     do_query: fn(TyCtxt<'tcx>, K) -> V,
307 ) {
308     debug_assert!(tcx.dep_graph.is_green(&dep_node));
309
310     let key = K::recover(tcx, &dep_node).unwrap_or_else(|| {
311         panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
312     });
313     if cache_on_disk(tcx, &key) {
314         let _ = do_query(tcx, key);
315     }
316 }
317
318 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
319 where
320     Q: QueryDescription<QueryCtxt<'tcx>>,
321     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
322 {
323     if let Some(key) = Q::Key::recover(tcx, &dep_node) {
324         #[cfg(debug_assertions)]
325         let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
326         let tcx = QueryCtxt::from_tcx(tcx);
327         force_query::<Q, _>(tcx, key, dep_node);
328         true
329     } else {
330         false
331     }
332 }
333
334 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
335 // invoked by `rustc_query_append`.
336 macro_rules! define_queries {
337     (
338      $($(#[$attr:meta])*
339         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
340         define_queries_struct! {
341             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
342         }
343
344         #[allow(nonstandard_style)]
345         mod queries {
346             use std::marker::PhantomData;
347
348             $(pub struct $name<'tcx> {
349                 data: PhantomData<&'tcx ()>
350             })*
351         }
352
353         $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
354             type Key = query_keys::$name<'tcx>;
355             type Value = query_values::$name<'tcx>;
356             type Stored = query_stored::$name<'tcx>;
357             const NAME: &'static str = stringify!($name);
358         }
359
360         impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
361             rustc_query_description! { $name }
362
363             type Cache = query_storage::$name<'tcx>;
364
365             #[inline(always)]
366             fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
367                 where QueryCtxt<'tcx>: 'a
368             {
369                 &tcx.queries.$name
370             }
371
372             #[inline(always)]
373             fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
374                 where 'tcx:'a
375             {
376                 &tcx.query_caches.$name
377             }
378
379             #[inline]
380             fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
381                 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
382             {
383                 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
384                 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
385                 QueryVTable {
386                     anon: is_anon!([$($modifiers)*]),
387                     eval_always: is_eval_always!([$($modifiers)*]),
388                     depth_limit: depth_limit!([$($modifiers)*]),
389                     dep_kind: dep_graph::DepKind::$name,
390                     hash_result: hash_result!([$($modifiers)*]),
391                     handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
392                     compute,
393                     cache_on_disk,
394                     try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
395                 }
396             }
397         })*
398
399         #[allow(nonstandard_style)]
400         mod query_callbacks {
401             use super::*;
402             use rustc_query_system::dep_graph::DepNodeParams;
403             use rustc_query_system::query::QueryDescription;
404             use rustc_query_system::dep_graph::FingerprintStyle;
405
406             // We use this for most things when incr. comp. is turned off.
407             pub fn Null() -> DepKindStruct {
408                 DepKindStruct {
409                     is_anon: false,
410                     is_eval_always: false,
411                     fingerprint_style: FingerprintStyle::Unit,
412                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
413                     try_load_from_on_disk_cache: None,
414                 }
415             }
416
417             // We use this for the forever-red node.
418             pub fn Red() -> DepKindStruct {
419                 DepKindStruct {
420                     is_anon: false,
421                     is_eval_always: false,
422                     fingerprint_style: FingerprintStyle::Unit,
423                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
424                     try_load_from_on_disk_cache: None,
425                 }
426             }
427
428             pub fn TraitSelect() -> DepKindStruct {
429                 DepKindStruct {
430                     is_anon: true,
431                     is_eval_always: false,
432                     fingerprint_style: FingerprintStyle::Unit,
433                     force_from_dep_node: None,
434                     try_load_from_on_disk_cache: None,
435                 }
436             }
437
438             pub fn CompileCodegenUnit() -> DepKindStruct {
439                 DepKindStruct {
440                     is_anon: false,
441                     is_eval_always: false,
442                     fingerprint_style: FingerprintStyle::Opaque,
443                     force_from_dep_node: None,
444                     try_load_from_on_disk_cache: None,
445                 }
446             }
447
448             pub fn CompileMonoItem() -> DepKindStruct {
449                 DepKindStruct {
450                     is_anon: false,
451                     is_eval_always: false,
452                     fingerprint_style: FingerprintStyle::Opaque,
453                     force_from_dep_node: None,
454                     try_load_from_on_disk_cache: None,
455                 }
456             }
457
458             $(pub(crate) fn $name()-> DepKindStruct {
459                 let is_anon = is_anon!([$($modifiers)*]);
460                 let is_eval_always = is_eval_always!([$($modifiers)*]);
461
462                 let fingerprint_style =
463                     <<queries::$name<'_> as QueryConfig>::Key as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
464
465                 if is_anon || !fingerprint_style.reconstructible() {
466                     return DepKindStruct {
467                         is_anon,
468                         is_eval_always,
469                         fingerprint_style,
470                         force_from_dep_node: None,
471                         try_load_from_on_disk_cache: None,
472                     }
473                 }
474
475                 DepKindStruct {
476                     is_anon,
477                     is_eval_always,
478                     fingerprint_style,
479                     force_from_dep_node: Some(|tcx, dep_node| $crate::plumbing::force_from_dep_node::<queries::$name<'_>>(tcx, dep_node)),
480                     try_load_from_on_disk_cache: Some(|tcx, key| $crate::plumbing::try_load_from_on_disk_cache::<
481                         <queries::$name<'_> as QueryConfig>::Key, _
482                     >(tcx, key, queries::$name::cache_on_disk, TyCtxt::$name)),
483                 }
484             })*
485         }
486
487         pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct] {
488             arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
489         }
490     }
491 }
492
493 macro_rules! define_queries_struct {
494     (
495      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
496         pub struct Queries<'tcx> {
497             local_providers: Box<Providers>,
498             extern_providers: Box<ExternProviders>,
499
500             pub on_disk_cache: Option<OnDiskCache<'tcx>>,
501
502             jobs: AtomicU64,
503
504             $($(#[$attr])*  $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
505         }
506
507         impl<'tcx> Queries<'tcx> {
508             pub fn new(
509                 local_providers: Providers,
510                 extern_providers: ExternProviders,
511                 on_disk_cache: Option<OnDiskCache<'tcx>>,
512             ) -> Self {
513                 Queries {
514                     local_providers: Box::new(local_providers),
515                     extern_providers: Box::new(extern_providers),
516                     on_disk_cache,
517                     jobs: AtomicU64::new(1),
518                     $($name: Default::default()),*
519                 }
520             }
521
522             pub(crate) fn try_collect_active_jobs(
523                 &'tcx self,
524                 tcx: TyCtxt<'tcx>,
525             ) -> Option<QueryMap> {
526                 let tcx = QueryCtxt { tcx, queries: self };
527                 let mut jobs = QueryMap::default();
528
529                 $(
530                     let make_query = |tcx, key| {
531                         let kind = dep_graph::DepKind::$name;
532                         let name = stringify!($name);
533                         $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
534                     };
535                     self.$name.try_collect_active_jobs(
536                         tcx,
537                         make_query,
538                         &mut jobs,
539                     )?;
540                 )*
541
542                 Some(jobs)
543             }
544         }
545
546         impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
547             fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
548                 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
549                 this as _
550             }
551
552             fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
553                 let qcx = QueryCtxt { tcx, queries: self };
554                 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
555             }
556
557             $($(#[$attr])*
558             #[inline(always)]
559             #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
560             fn $name(
561                 &'tcx self,
562                 tcx: TyCtxt<'tcx>,
563                 span: Span,
564                 key: <queries::$name<'tcx> as QueryConfig>::Key,
565                 mode: QueryMode,
566             ) -> Option<query_stored::$name<'tcx>> {
567                 let qcx = QueryCtxt { tcx, queries: self };
568                 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)
569             })*
570         }
571     };
572 }