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