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