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