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