]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/plumbing.rs
Move keys module.
[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::on_disk_cache::{CacheDecoder, CacheEncoder, EncodedDepNodeIndex};
6 use crate::profiling_support::QueryKeyStringCache;
7 use crate::{on_disk_cache, Queries};
8 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
9 use rustc_data_structures::sync::{AtomicU64, Lock};
10 use rustc_errors::{Diagnostic, Handler};
11 use rustc_middle::dep_graph::{
12     self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
13 };
14 use rustc_middle::query::Key;
15 use rustc_middle::ty::tls::{self, ImplicitCtxt};
16 use rustc_middle::ty::{self, TyCtxt};
17 use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
18 use rustc_query_system::ich::StableHashingContext;
19 use rustc_query_system::query::{
20     force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap,
21     QuerySideEffects, QueryStackFrame,
22 };
23 use rustc_query_system::{LayoutOfDepth, QueryOverflow, Value};
24 use rustc_serialize::Decodable;
25 use rustc_session::Limit;
26 use rustc_span::def_id::LOCAL_CRATE;
27 use std::any::Any;
28 use std::num::NonZeroU64;
29 use thin_vec::ThinVec;
30
31 #[derive(Copy, Clone)]
32 pub struct QueryCtxt<'tcx> {
33     pub tcx: TyCtxt<'tcx>,
34     pub queries: &'tcx Queries<'tcx>,
35 }
36
37 impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
38     type Target = TyCtxt<'tcx>;
39
40     #[inline]
41     fn deref(&self) -> &Self::Target {
42         &self.tcx
43     }
44 }
45
46 impl<'tcx> HasDepContext for QueryCtxt<'tcx> {
47     type DepKind = rustc_middle::dep_graph::DepKind;
48     type DepContext = TyCtxt<'tcx>;
49
50     #[inline]
51     fn dep_context(&self) -> &Self::DepContext {
52         &self.tcx
53     }
54 }
55
56 impl QueryContext for QueryCtxt<'_> {
57     fn next_job_id(&self) -> QueryJobId {
58         QueryJobId(
59             NonZeroU64::new(
60                 self.queries.jobs.fetch_add(1, rustc_data_structures::sync::Ordering::Relaxed),
61             )
62             .unwrap(),
63         )
64     }
65
66     fn current_query_job(&self) -> Option<QueryJobId> {
67         tls::with_related_context(**self, |icx| icx.query)
68     }
69
70     fn try_collect_active_jobs(&self) -> Option<QueryMap> {
71         self.queries.try_collect_active_jobs(**self)
72     }
73
74     // Interactions with on_disk_cache
75     fn load_side_effects(&self, prev_dep_node_index: SerializedDepNodeIndex) -> QuerySideEffects {
76         self.queries
77             .on_disk_cache
78             .as_ref()
79             .map(|c| c.load_side_effects(**self, prev_dep_node_index))
80             .unwrap_or_default()
81     }
82
83     fn store_side_effects(&self, dep_node_index: DepNodeIndex, side_effects: QuerySideEffects) {
84         if let Some(c) = self.queries.on_disk_cache.as_ref() {
85             c.store_side_effects(dep_node_index, side_effects)
86         }
87     }
88
89     fn store_side_effects_for_anon_node(
90         &self,
91         dep_node_index: DepNodeIndex,
92         side_effects: QuerySideEffects,
93     ) {
94         if let Some(c) = self.queries.on_disk_cache.as_ref() {
95             c.store_side_effects_for_anon_node(dep_node_index, side_effects)
96         }
97     }
98
99     /// Executes a job by changing the `ImplicitCtxt` to point to the
100     /// new query job while it executes. It returns the diagnostics
101     /// captured during execution and the actual result.
102     #[inline(always)]
103     fn start_query<R>(
104         &self,
105         token: QueryJobId,
106         depth_limit: bool,
107         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
108         compute: impl FnOnce() -> R,
109     ) -> R {
110         // The `TyCtxt` stored in TLS has the same global interner lifetime
111         // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
112         // when accessing the `ImplicitCtxt`.
113         tls::with_related_context(**self, move |current_icx| {
114             if depth_limit && !self.recursion_limit().value_within_limit(current_icx.query_depth) {
115                 self.depth_limit_error(token);
116             }
117
118             // Update the `ImplicitCtxt` to point to our new query job.
119             let new_icx = ImplicitCtxt {
120                 tcx: **self,
121                 query: Some(token),
122                 diagnostics,
123                 query_depth: current_icx.query_depth + depth_limit as usize,
124                 task_deps: current_icx.task_deps,
125             };
126
127             // Use the `ImplicitCtxt` while we execute the query.
128             tls::enter_context(&new_icx, |_| {
129                 rustc_data_structures::stack::ensure_sufficient_stack(compute)
130             })
131         })
132     }
133
134     fn depth_limit_error(&self, job: QueryJobId) {
135         let mut span = None;
136         let mut layout_of_depth = None;
137         if let Some(map) = self.try_collect_active_jobs() {
138             if let Some((info, depth)) = job.try_find_layout_root(map) {
139                 span = Some(info.job.span);
140                 layout_of_depth = Some(LayoutOfDepth { desc: info.query.description, depth });
141             }
142         }
143
144         let suggested_limit = match self.recursion_limit() {
145             Limit(0) => Limit(2),
146             limit => limit * 2,
147         };
148
149         self.sess.emit_fatal(QueryOverflow {
150             span,
151             layout_of_depth,
152             suggested_limit,
153             crate_name: self.crate_name(LOCAL_CRATE),
154         });
155     }
156 }
157
158 impl<'tcx> QueryCtxt<'tcx> {
159     #[inline]
160     pub fn from_tcx(tcx: TyCtxt<'tcx>) -> Self {
161         let queries = tcx.queries.as_any();
162         let queries = unsafe {
163             let queries = std::mem::transmute::<&dyn Any, &dyn Any>(queries);
164             let queries = queries.downcast_ref().unwrap();
165             let queries = std::mem::transmute::<&Queries<'_>, &Queries<'_>>(queries);
166             queries
167         };
168         QueryCtxt { tcx, queries }
169     }
170
171     pub(crate) fn on_disk_cache(self) -> Option<&'tcx on_disk_cache::OnDiskCache<'tcx>> {
172         self.queries.on_disk_cache.as_ref()
173     }
174
175     pub(super) fn encode_query_results(
176         self,
177         encoder: &mut CacheEncoder<'_, 'tcx>,
178         query_result_index: &mut EncodedDepNodeIndex,
179     ) {
180         for query in &self.queries.query_structs {
181             if let Some(encode) = query.encode_query_results {
182                 encode(self, encoder, query_result_index);
183             }
184         }
185     }
186
187     pub fn try_print_query_stack(
188         self,
189         query: Option<QueryJobId>,
190         handler: &Handler,
191         num_frames: Option<usize>,
192     ) -> usize {
193         rustc_query_system::query::print_query_stack(self, query, handler, num_frames)
194     }
195 }
196
197 #[derive(Clone, Copy)]
198 pub(crate) struct QueryStruct<'tcx> {
199     pub try_collect_active_jobs: fn(QueryCtxt<'tcx>, &mut QueryMap) -> Option<()>,
200     pub alloc_self_profile_query_strings: fn(TyCtxt<'tcx>, &mut QueryKeyStringCache),
201     pub encode_query_results:
202         Option<fn(QueryCtxt<'tcx>, &mut CacheEncoder<'_, 'tcx>, &mut EncodedDepNodeIndex)>,
203 }
204
205 macro_rules! handle_cycle_error {
206     ([]) => {{
207         rustc_query_system::HandleCycleError::Error
208     }};
209     ([(fatal_cycle) $($rest:tt)*]) => {{
210         rustc_query_system::HandleCycleError::Fatal
211     }};
212     ([(cycle_delay_bug) $($rest:tt)*]) => {{
213         rustc_query_system::HandleCycleError::DelayBug
214     }};
215     ([$other:tt $($modifiers:tt)*]) => {
216         handle_cycle_error!([$($modifiers)*])
217     };
218 }
219
220 macro_rules! is_anon {
221     ([]) => {{
222         false
223     }};
224     ([(anon) $($rest:tt)*]) => {{
225         true
226     }};
227     ([$other:tt $($modifiers:tt)*]) => {
228         is_anon!([$($modifiers)*])
229     };
230 }
231
232 macro_rules! is_eval_always {
233     ([]) => {{
234         false
235     }};
236     ([(eval_always) $($rest:tt)*]) => {{
237         true
238     }};
239     ([$other:tt $($modifiers:tt)*]) => {
240         is_eval_always!([$($modifiers)*])
241     };
242 }
243
244 macro_rules! depth_limit {
245     ([]) => {{
246         false
247     }};
248     ([(depth_limit) $($rest:tt)*]) => {{
249         true
250     }};
251     ([$other:tt $($modifiers:tt)*]) => {
252         depth_limit!([$($modifiers)*])
253     };
254 }
255
256 macro_rules! hash_result {
257     ([]) => {{
258         Some(dep_graph::hash_result)
259     }};
260     ([(no_hash) $($rest:tt)*]) => {{
261         None
262     }};
263     ([$other:tt $($modifiers:tt)*]) => {
264         hash_result!([$($modifiers)*])
265     };
266 }
267
268 macro_rules! get_provider {
269     ([][$tcx:expr, $name:ident, $key:expr]) => {{
270         $tcx.queries.local_providers.$name
271     }};
272     ([(separate_provide_extern) $($rest:tt)*][$tcx:expr, $name:ident, $key:expr]) => {{
273         if $key.query_crate_is_local() {
274             $tcx.queries.local_providers.$name
275         } else {
276             $tcx.queries.extern_providers.$name
277         }
278     }};
279     ([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
280         get_provider!([$($modifiers)*][$($args)*])
281     };
282 }
283
284 macro_rules! should_ever_cache_on_disk {
285     ([]) => {{
286         None
287     }};
288     ([(cache) $($rest:tt)*]) => {{
289         Some($crate::plumbing::try_load_from_disk::<Self::Value>)
290     }};
291     ([$other:tt $($modifiers:tt)*]) => {
292         should_ever_cache_on_disk!([$($modifiers)*])
293     };
294 }
295
296 pub(crate) fn create_query_frame<
297     'tcx,
298     K: Copy + Key + for<'a> HashStable<StableHashingContext<'a>>,
299 >(
300     tcx: QueryCtxt<'tcx>,
301     do_describe: fn(TyCtxt<'tcx>, K) -> String,
302     key: K,
303     kind: DepKind,
304     name: &'static str,
305 ) -> QueryStackFrame {
306     // Disable visible paths printing for performance reasons.
307     // Showing visible path instead of any path is not that important in production.
308     let description = ty::print::with_no_visible_paths!(
309         // Force filename-line mode to avoid invoking `type_of` query.
310         ty::print::with_forced_impl_filename_line!(do_describe(tcx.tcx, key))
311     );
312     let description =
313         if tcx.sess.verbose() { format!("{} [{}]", description, name) } else { description };
314     let span = if kind == dep_graph::DepKind::def_span {
315         // The `def_span` query is used to calculate `default_span`,
316         // so exit to avoid infinite recursion.
317         None
318     } else {
319         Some(key.default_span(*tcx))
320     };
321     let def_id = key.key_as_def_id();
322     let def_kind = if kind == dep_graph::DepKind::opt_def_kind {
323         // Try to avoid infinite recursion.
324         None
325     } else {
326         def_id.and_then(|def_id| def_id.as_local()).and_then(|def_id| tcx.opt_def_kind(def_id))
327     };
328     let hash = || {
329         tcx.with_stable_hashing_context(|mut hcx| {
330             let mut hasher = StableHasher::new();
331             std::mem::discriminant(&kind).hash_stable(&mut hcx, &mut hasher);
332             key.hash_stable(&mut hcx, &mut hasher);
333             hasher.finish::<u64>()
334         })
335     };
336     let ty_adt_id = key.ty_adt_id();
337
338     QueryStackFrame::new(name, description, span, def_id, def_kind, ty_adt_id, hash)
339 }
340
341 fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
342 where
343     Q: QueryDescription<QueryCtxt<'tcx>>,
344     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
345 {
346     debug_assert!(tcx.dep_graph.is_green(&dep_node));
347
348     let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
349         panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
350     });
351     if Q::cache_on_disk(tcx, &key) {
352         let _ = Q::execute_query(tcx, key);
353     }
354 }
355
356 pub(crate) fn try_load_from_disk<'tcx, V>(
357     tcx: QueryCtxt<'tcx>,
358     id: SerializedDepNodeIndex,
359 ) -> Option<V>
360 where
361     V: for<'a> Decodable<CacheDecoder<'a, 'tcx>>,
362 {
363     tcx.on_disk_cache().as_ref()?.try_load_query_result(*tcx, id)
364 }
365
366 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
367 where
368     Q: QueryDescription<QueryCtxt<'tcx>>,
369     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
370     Q::Value: Value<TyCtxt<'tcx>>,
371 {
372     // We must avoid ever having to call `force_from_dep_node()` for a
373     // `DepNode::codegen_unit`:
374     // Since we cannot reconstruct the query key of a `DepNode::codegen_unit`, we
375     // would always end up having to evaluate the first caller of the
376     // `codegen_unit` query that *is* reconstructible. This might very well be
377     // the `compile_codegen_unit` query, thus re-codegenning the whole CGU just
378     // to re-trigger calling the `codegen_unit` query with the right key. At
379     // that point we would already have re-done all the work we are trying to
380     // avoid doing in the first place.
381     // The solution is simple: Just explicitly call the `codegen_unit` query for
382     // each CGU, right after partitioning. This way `try_mark_green` will always
383     // hit the cache instead of having to go through `force_from_dep_node`.
384     // This assertion makes sure, we actually keep applying the solution above.
385     debug_assert!(
386         dep_node.kind != DepKind::codegen_unit,
387         "calling force_from_dep_node() on DepKind::codegen_unit"
388     );
389
390     if let Some(key) = Q::Key::recover(tcx, &dep_node) {
391         #[cfg(debug_assertions)]
392         let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
393         let tcx = QueryCtxt::from_tcx(tcx);
394         force_query::<Q, _>(tcx, key, dep_node);
395         true
396     } else {
397         false
398     }
399 }
400
401 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
402     is_anon: bool,
403     is_eval_always: bool,
404 ) -> DepKindStruct<'tcx>
405 where
406     Q: QueryDescription<QueryCtxt<'tcx>>,
407     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
408 {
409     let fingerprint_style = Q::Key::fingerprint_style();
410
411     if is_anon || !fingerprint_style.reconstructible() {
412         return DepKindStruct {
413             is_anon,
414             is_eval_always,
415             fingerprint_style,
416             force_from_dep_node: None,
417             try_load_from_on_disk_cache: None,
418         };
419     }
420
421     DepKindStruct {
422         is_anon,
423         is_eval_always,
424         fingerprint_style,
425         force_from_dep_node: Some(force_from_dep_node::<Q>),
426         try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
427     }
428 }
429
430 macro_rules! expand_if_cached {
431     ([], $tokens:expr) => {{
432         None
433     }};
434     ([(cache) $($rest:tt)*], $tokens:expr) => {{
435         Some($tokens)
436     }};
437     ([$other:tt $($modifiers:tt)*], $tokens:expr) => {
438         expand_if_cached!([$($modifiers)*], $tokens)
439     };
440 }
441
442 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
443 // invoked by `rustc_query_append`.
444 macro_rules! define_queries {
445     (
446      $($(#[$attr:meta])*
447         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
448         define_queries_struct! {
449             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
450         }
451
452         #[allow(nonstandard_style)]
453         mod queries {
454             use std::marker::PhantomData;
455
456             $(pub struct $name<'tcx> {
457                 data: PhantomData<&'tcx ()>
458             })*
459         }
460
461         $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
462             type Key = query_keys::$name<'tcx>;
463             type Value = query_values::$name<'tcx>;
464             type Stored = query_stored::$name<'tcx>;
465             const NAME: &'static str = stringify!($name);
466         }
467
468         impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
469             #[inline]
470             fn cache_on_disk(tcx: TyCtxt<'tcx>, key: &Self::Key) -> bool {
471                 ::rustc_middle::query::cached::$name(tcx, key)
472             }
473
474             type Cache = query_storage::$name<'tcx>;
475
476             #[inline(always)]
477             fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
478                 where QueryCtxt<'tcx>: 'a
479             {
480                 &tcx.queries.$name
481             }
482
483             #[inline(always)]
484             fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
485                 where 'tcx:'a
486             {
487                 &tcx.query_caches.$name
488             }
489
490             #[inline]
491             fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
492                 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
493             {
494                 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
495                 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
496                 QueryVTable {
497                     anon: is_anon!([$($modifiers)*]),
498                     eval_always: is_eval_always!([$($modifiers)*]),
499                     depth_limit: depth_limit!([$($modifiers)*]),
500                     dep_kind: dep_graph::DepKind::$name,
501                     hash_result: hash_result!([$($modifiers)*]),
502                     handle_cycle_error: handle_cycle_error!([$($modifiers)*]),
503                     compute,
504                     try_load_from_disk: if cache_on_disk { should_ever_cache_on_disk!([$($modifiers)*]) } else { None },
505                 }
506             }
507
508             fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
509                 tcx.$name(k)
510             }
511         })*
512
513         #[allow(nonstandard_style)]
514         mod query_callbacks {
515             use super::*;
516             use rustc_query_system::dep_graph::FingerprintStyle;
517
518             // We use this for most things when incr. comp. is turned off.
519             pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
520                 DepKindStruct {
521                     is_anon: false,
522                     is_eval_always: false,
523                     fingerprint_style: FingerprintStyle::Unit,
524                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
525                     try_load_from_on_disk_cache: None,
526                 }
527             }
528
529             // We use this for the forever-red node.
530             pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
531                 DepKindStruct {
532                     is_anon: false,
533                     is_eval_always: false,
534                     fingerprint_style: FingerprintStyle::Unit,
535                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
536                     try_load_from_on_disk_cache: None,
537                 }
538             }
539
540             pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
541                 DepKindStruct {
542                     is_anon: true,
543                     is_eval_always: false,
544                     fingerprint_style: FingerprintStyle::Unit,
545                     force_from_dep_node: None,
546                     try_load_from_on_disk_cache: None,
547                 }
548             }
549
550             pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
551                 DepKindStruct {
552                     is_anon: false,
553                     is_eval_always: false,
554                     fingerprint_style: FingerprintStyle::Opaque,
555                     force_from_dep_node: None,
556                     try_load_from_on_disk_cache: None,
557                 }
558             }
559
560             pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
561                 DepKindStruct {
562                     is_anon: false,
563                     is_eval_always: false,
564                     fingerprint_style: FingerprintStyle::Opaque,
565                     force_from_dep_node: None,
566                     try_load_from_on_disk_cache: None,
567                 }
568             }
569
570             $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
571                 $crate::plumbing::query_callback::<queries::$name<'tcx>>(
572                     is_anon!([$($modifiers)*]),
573                     is_eval_always!([$($modifiers)*]),
574                 )
575             })*
576         }
577
578         mod query_structs {
579             use rustc_middle::ty::TyCtxt;
580             use $crate::plumbing::{QueryStruct, QueryCtxt};
581             use $crate::profiling_support::QueryKeyStringCache;
582             use rustc_query_system::query::QueryMap;
583
584             pub(super) const fn dummy_query_struct<'tcx>() -> QueryStruct<'tcx> {
585                 fn noop_try_collect_active_jobs(_: QueryCtxt<'_>, _: &mut QueryMap) -> Option<()> {
586                     None
587                 }
588                 fn noop_alloc_self_profile_query_strings(_: TyCtxt<'_>, _: &mut QueryKeyStringCache) {}
589
590                 QueryStruct {
591                     try_collect_active_jobs: noop_try_collect_active_jobs,
592                     alloc_self_profile_query_strings: noop_alloc_self_profile_query_strings,
593                     encode_query_results: None,
594                 }
595             }
596
597             pub(super) use dummy_query_struct as Null;
598             pub(super) use dummy_query_struct as Red;
599             pub(super) use dummy_query_struct as TraitSelect;
600             pub(super) use dummy_query_struct as CompileCodegenUnit;
601             pub(super) use dummy_query_struct as CompileMonoItem;
602
603             $(
604             pub(super) const fn $name<'tcx>() -> QueryStruct<'tcx> { QueryStruct {
605                 try_collect_active_jobs: |tcx, qmap| {
606                     let make_query = |tcx, key| {
607                         let kind = rustc_middle::dep_graph::DepKind::$name;
608                         let name = stringify!($name);
609                         $crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
610                     };
611                     tcx.queries.$name.try_collect_active_jobs(
612                         tcx,
613                         make_query,
614                         qmap,
615                     )
616                 },
617                 alloc_self_profile_query_strings: |tcx, string_cache| {
618                     $crate::profiling_support::alloc_self_profile_query_strings_for_query_cache(
619                         tcx,
620                         stringify!($name),
621                         &tcx.query_caches.$name,
622                         string_cache,
623                     )
624                 },
625                 encode_query_results: expand_if_cached!([$($modifiers)*], |tcx, encoder, query_result_index|
626                     $crate::on_disk_cache::encode_query_results::<_, super::queries::$name<'_>>(tcx, encoder, query_result_index)
627                 ),
628             }})*
629         }
630
631         pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
632             arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
633         }
634     }
635 }
636
637 use crate::{ExternProviders, OnDiskCache, Providers};
638
639 impl<'tcx> Queries<'tcx> {
640     pub fn new(
641         local_providers: Providers,
642         extern_providers: ExternProviders,
643         on_disk_cache: Option<OnDiskCache<'tcx>>,
644     ) -> Self {
645         use crate::query_structs;
646         Queries {
647             local_providers: Box::new(local_providers),
648             extern_providers: Box::new(extern_providers),
649             query_structs: make_dep_kind_array!(query_structs).to_vec(),
650             on_disk_cache,
651             jobs: AtomicU64::new(1),
652             ..Queries::default()
653         }
654     }
655 }
656
657 macro_rules! define_queries_struct {
658     (
659      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
660         #[derive(Default)]
661         pub struct Queries<'tcx> {
662             local_providers: Box<Providers>,
663             extern_providers: Box<ExternProviders>,
664             query_structs: Vec<$crate::plumbing::QueryStruct<'tcx>>,
665
666             pub on_disk_cache: Option<OnDiskCache<'tcx>>,
667
668             jobs: AtomicU64,
669
670             $($(#[$attr])*  $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
671         }
672
673         impl<'tcx> Queries<'tcx> {
674             pub(crate) fn try_collect_active_jobs(
675                 &'tcx self,
676                 tcx: TyCtxt<'tcx>,
677             ) -> Option<QueryMap> {
678                 let tcx = QueryCtxt { tcx, queries: self };
679                 let mut jobs = QueryMap::default();
680
681                 for query in &self.query_structs {
682                     (query.try_collect_active_jobs)(tcx, &mut jobs);
683                 }
684
685                 Some(jobs)
686             }
687         }
688
689         impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
690             fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
691                 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
692                 this as _
693             }
694
695             fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
696                 let qcx = QueryCtxt { tcx, queries: self };
697                 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
698             }
699
700             $($(#[$attr])*
701             #[inline(always)]
702             #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
703             fn $name(
704                 &'tcx self,
705                 tcx: TyCtxt<'tcx>,
706                 span: Span,
707                 key: <queries::$name<'tcx> as QueryConfig>::Key,
708                 mode: QueryMode,
709             ) -> Option<query_stored::$name<'tcx>> {
710                 let qcx = QueryCtxt { tcx, queries: self };
711                 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)
712             })*
713         }
714     };
715 }