]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_impl/src/plumbing.rs
Rollup merge of #100200 - petrochenkov:zgccld2, r=lqd,Mark-Simulacrum
[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 fn try_load_from_on_disk_cache<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode)
305 where
306     Q: QueryDescription<QueryCtxt<'tcx>>,
307     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
308 {
309     debug_assert!(tcx.dep_graph.is_green(&dep_node));
310
311     let key = Q::Key::recover(tcx, &dep_node).unwrap_or_else(|| {
312         panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
313     });
314     if Q::cache_on_disk(tcx, &key) {
315         let _ = Q::execute_query(tcx, key);
316     }
317 }
318
319 fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
320 where
321     Q: QueryDescription<QueryCtxt<'tcx>>,
322     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
323 {
324     if let Some(key) = Q::Key::recover(tcx, &dep_node) {
325         #[cfg(debug_assertions)]
326         let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
327         let tcx = QueryCtxt::from_tcx(tcx);
328         force_query::<Q, _>(tcx, key, dep_node);
329         true
330     } else {
331         false
332     }
333 }
334
335 pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
336     is_anon: bool,
337     is_eval_always: bool,
338 ) -> DepKindStruct<'tcx>
339 where
340     Q: QueryDescription<QueryCtxt<'tcx>>,
341     Q::Key: DepNodeParams<TyCtxt<'tcx>>,
342 {
343     let fingerprint_style = Q::Key::fingerprint_style();
344
345     if is_anon || !fingerprint_style.reconstructible() {
346         return DepKindStruct {
347             is_anon,
348             is_eval_always,
349             fingerprint_style,
350             force_from_dep_node: None,
351             try_load_from_on_disk_cache: None,
352         };
353     }
354
355     DepKindStruct {
356         is_anon,
357         is_eval_always,
358         fingerprint_style,
359         force_from_dep_node: Some(force_from_dep_node::<Q>),
360         try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache::<Q>),
361     }
362 }
363
364 // NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
365 // invoked by `rustc_query_append`.
366 macro_rules! define_queries {
367     (
368      $($(#[$attr:meta])*
369         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
370         define_queries_struct! {
371             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
372         }
373
374         #[allow(nonstandard_style)]
375         mod queries {
376             use std::marker::PhantomData;
377
378             $(pub struct $name<'tcx> {
379                 data: PhantomData<&'tcx ()>
380             })*
381         }
382
383         $(impl<'tcx> QueryConfig for queries::$name<'tcx> {
384             type Key = query_keys::$name<'tcx>;
385             type Value = query_values::$name<'tcx>;
386             type Stored = query_stored::$name<'tcx>;
387             const NAME: &'static str = stringify!($name);
388         }
389
390         impl<'tcx> QueryDescription<QueryCtxt<'tcx>> for queries::$name<'tcx> {
391             rustc_query_description! { $name }
392
393             type Cache = query_storage::$name<'tcx>;
394
395             #[inline(always)]
396             fn query_state<'a>(tcx: QueryCtxt<'tcx>) -> &'a QueryState<Self::Key>
397                 where QueryCtxt<'tcx>: 'a
398             {
399                 &tcx.queries.$name
400             }
401
402             #[inline(always)]
403             fn query_cache<'a>(tcx: QueryCtxt<'tcx>) -> &'a Self::Cache
404                 where 'tcx:'a
405             {
406                 &tcx.query_caches.$name
407             }
408
409             #[inline]
410             fn make_vtable(tcx: QueryCtxt<'tcx>, key: &Self::Key) ->
411                 QueryVTable<QueryCtxt<'tcx>, Self::Key, Self::Value>
412             {
413                 let compute = get_provider!([$($modifiers)*][tcx, $name, key]);
414                 let cache_on_disk = Self::cache_on_disk(tcx.tcx, key);
415                 QueryVTable {
416                     anon: is_anon!([$($modifiers)*]),
417                     eval_always: is_eval_always!([$($modifiers)*]),
418                     depth_limit: depth_limit!([$($modifiers)*]),
419                     dep_kind: dep_graph::DepKind::$name,
420                     hash_result: hash_result!([$($modifiers)*]),
421                     handle_cycle_error: |tcx, mut error| handle_cycle_error!([$($modifiers)*][tcx, error]),
422                     compute,
423                     cache_on_disk,
424                     try_load_from_disk: Self::TRY_LOAD_FROM_DISK,
425                 }
426             }
427
428             fn execute_query(tcx: TyCtxt<'tcx>, k: Self::Key) -> Self::Stored {
429                 tcx.$name(k)
430             }
431         })*
432
433         #[allow(nonstandard_style)]
434         mod query_callbacks {
435             use super::*;
436             use rustc_query_system::dep_graph::FingerprintStyle;
437
438             // We use this for most things when incr. comp. is turned off.
439             pub fn Null<'tcx>() -> DepKindStruct<'tcx> {
440                 DepKindStruct {
441                     is_anon: false,
442                     is_eval_always: false,
443                     fingerprint_style: FingerprintStyle::Unit,
444                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
445                     try_load_from_on_disk_cache: None,
446                 }
447             }
448
449             // We use this for the forever-red node.
450             pub fn Red<'tcx>() -> DepKindStruct<'tcx> {
451                 DepKindStruct {
452                     is_anon: false,
453                     is_eval_always: false,
454                     fingerprint_style: FingerprintStyle::Unit,
455                     force_from_dep_node: Some(|_, dep_node| bug!("force_from_dep_node: encountered {:?}", dep_node)),
456                     try_load_from_on_disk_cache: None,
457                 }
458             }
459
460             pub fn TraitSelect<'tcx>() -> DepKindStruct<'tcx> {
461                 DepKindStruct {
462                     is_anon: true,
463                     is_eval_always: false,
464                     fingerprint_style: FingerprintStyle::Unit,
465                     force_from_dep_node: None,
466                     try_load_from_on_disk_cache: None,
467                 }
468             }
469
470             pub fn CompileCodegenUnit<'tcx>() -> DepKindStruct<'tcx> {
471                 DepKindStruct {
472                     is_anon: false,
473                     is_eval_always: false,
474                     fingerprint_style: FingerprintStyle::Opaque,
475                     force_from_dep_node: None,
476                     try_load_from_on_disk_cache: None,
477                 }
478             }
479
480             pub fn CompileMonoItem<'tcx>() -> DepKindStruct<'tcx> {
481                 DepKindStruct {
482                     is_anon: false,
483                     is_eval_always: false,
484                     fingerprint_style: FingerprintStyle::Opaque,
485                     force_from_dep_node: None,
486                     try_load_from_on_disk_cache: None,
487                 }
488             }
489
490             $(pub(crate) fn $name<'tcx>()-> DepKindStruct<'tcx> {
491                 $crate::plumbing::query_callback::<queries::$name<'tcx>>(
492                     is_anon!([$($modifiers)*]),
493                     is_eval_always!([$($modifiers)*]),
494                 )
495             })*
496         }
497
498         pub fn query_callbacks<'tcx>(arena: &'tcx Arena<'tcx>) -> &'tcx [DepKindStruct<'tcx>] {
499             arena.alloc_from_iter(make_dep_kind_array!(query_callbacks))
500         }
501     }
502 }
503
504 macro_rules! define_queries_struct {
505     (
506      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
507         pub struct Queries<'tcx> {
508             local_providers: Box<Providers>,
509             extern_providers: Box<ExternProviders>,
510
511             pub on_disk_cache: Option<OnDiskCache<'tcx>>,
512
513             jobs: AtomicU64,
514
515             $($(#[$attr])*  $name: QueryState<<queries::$name<'tcx> as QueryConfig>::Key>,)*
516         }
517
518         impl<'tcx> Queries<'tcx> {
519             pub fn new(
520                 local_providers: Providers,
521                 extern_providers: ExternProviders,
522                 on_disk_cache: Option<OnDiskCache<'tcx>>,
523             ) -> Self {
524                 Queries {
525                     local_providers: Box::new(local_providers),
526                     extern_providers: Box::new(extern_providers),
527                     on_disk_cache,
528                     jobs: AtomicU64::new(1),
529                     $($name: Default::default()),*
530                 }
531             }
532
533             pub(crate) fn try_collect_active_jobs(
534                 &'tcx self,
535                 tcx: TyCtxt<'tcx>,
536             ) -> Option<QueryMap> {
537                 let tcx = QueryCtxt { tcx, queries: self };
538                 let mut jobs = QueryMap::default();
539
540                 $(
541                     let make_query = |tcx, key| {
542                         let kind = dep_graph::DepKind::$name;
543                         let name = stringify!($name);
544                         $crate::plumbing::create_query_frame(tcx, queries::$name::describe, key, kind, name)
545                     };
546                     self.$name.try_collect_active_jobs(
547                         tcx,
548                         make_query,
549                         &mut jobs,
550                     )?;
551                 )*
552
553                 Some(jobs)
554             }
555         }
556
557         impl<'tcx> QueryEngine<'tcx> for Queries<'tcx> {
558             fn as_any(&'tcx self) -> &'tcx dyn std::any::Any {
559                 let this = unsafe { std::mem::transmute::<&Queries<'_>, &Queries<'_>>(self) };
560                 this as _
561             }
562
563             fn try_mark_green(&'tcx self, tcx: TyCtxt<'tcx>, dep_node: &dep_graph::DepNode) -> bool {
564                 let qcx = QueryCtxt { tcx, queries: self };
565                 tcx.dep_graph.try_mark_green(qcx, dep_node).is_some()
566             }
567
568             $($(#[$attr])*
569             #[inline(always)]
570             #[tracing::instrument(level = "trace", skip(self, tcx), ret)]
571             fn $name(
572                 &'tcx self,
573                 tcx: TyCtxt<'tcx>,
574                 span: Span,
575                 key: <queries::$name<'tcx> as QueryConfig>::Key,
576                 mode: QueryMode,
577             ) -> Option<query_stored::$name<'tcx>> {
578                 let qcx = QueryCtxt { tcx, queries: self };
579                 get_query::<queries::$name<'tcx>, _>(qcx, span, key, mode)
580             })*
581         }
582     };
583 }