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