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