]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/query/plumbing.rs
Separate the query cache from the query state.
[rust.git] / compiler / rustc_middle / src / ty / query / 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::dep_graph::DepGraph;
6 use crate::ty::query::Query;
7 use crate::ty::tls::{self, ImplicitCtxt};
8 use crate::ty::{self, TyCtxt};
9 use rustc_query_system::query::QueryContext;
10 use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo};
11
12 use rustc_data_structures::fx::FxHashMap;
13 use rustc_data_structures::sync::Lock;
14 use rustc_data_structures::thin_vec::ThinVec;
15 use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level};
16 use rustc_span::def_id::DefId;
17 use rustc_span::Span;
18
19 impl QueryContext for TyCtxt<'tcx> {
20     type Query = Query<'tcx>;
21
22     fn incremental_verify_ich(&self) -> bool {
23         self.sess.opts.debugging_opts.incremental_verify_ich
24     }
25     fn verbose(&self) -> bool {
26         self.sess.verbose()
27     }
28
29     fn def_path_str(&self, def_id: DefId) -> String {
30         TyCtxt::def_path_str(*self, def_id)
31     }
32
33     fn dep_graph(&self) -> &DepGraph {
34         &self.dep_graph
35     }
36
37     fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
38         tls::with_related_context(*self, |icx| icx.query)
39     }
40
41     fn try_collect_active_jobs(
42         &self,
43     ) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
44     {
45         self.queries.try_collect_active_jobs()
46     }
47
48     /// Executes a job by changing the `ImplicitCtxt` to point to the
49     /// new query job while it executes. It returns the diagnostics
50     /// captured during execution and the actual result.
51     #[inline(always)]
52     fn start_query<R>(
53         &self,
54         token: QueryJobId<Self::DepKind>,
55         diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
56         compute: impl FnOnce(Self) -> R,
57     ) -> R {
58         // The `TyCtxt` stored in TLS has the same global interner lifetime
59         // as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
60         // when accessing the `ImplicitCtxt`.
61         tls::with_related_context(*self, move |current_icx| {
62             // Update the `ImplicitCtxt` to point to our new query job.
63             let new_icx = ImplicitCtxt {
64                 tcx: *self,
65                 query: Some(token),
66                 diagnostics,
67                 layout_depth: current_icx.layout_depth,
68                 task_deps: current_icx.task_deps,
69             };
70
71             // Use the `ImplicitCtxt` while we execute the query.
72             tls::enter_context(&new_icx, |_| {
73                 rustc_data_structures::stack::ensure_sufficient_stack(|| compute(*self))
74             })
75         })
76     }
77 }
78
79 impl<'tcx> TyCtxt<'tcx> {
80     #[inline(never)]
81     #[cold]
82     pub(super) fn report_cycle(
83         self,
84         CycleError { usage, cycle: stack }: CycleError<Query<'tcx>>,
85     ) -> DiagnosticBuilder<'tcx> {
86         assert!(!stack.is_empty());
87
88         let fix_span = |span: Span, query: &Query<'tcx>| {
89             self.sess.source_map().guess_head_span(query.default_span(self, span))
90         };
91
92         // Disable naming impls with types in this path, since that
93         // sometimes cycles itself, leading to extra cycle errors.
94         // (And cycle errors around impls tend to occur during the
95         // collect/coherence phases anyhow.)
96         ty::print::with_forced_impl_filename_line(|| {
97             let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
98             let mut err = struct_span_err!(
99                 self.sess,
100                 span,
101                 E0391,
102                 "cycle detected when {}",
103                 stack[0].query.describe(self)
104             );
105
106             for i in 1..stack.len() {
107                 let query = &stack[i].query;
108                 let span = fix_span(stack[(i + 1) % stack.len()].span, query);
109                 err.span_note(span, &format!("...which requires {}...", query.describe(self)));
110             }
111
112             err.note(&format!(
113                 "...which again requires {}, completing the cycle",
114                 stack[0].query.describe(self)
115             ));
116
117             if let Some((span, query)) = usage {
118                 err.span_note(
119                     fix_span(span, &query),
120                     &format!("cycle used when {}", query.describe(self)),
121                 );
122             }
123
124             err
125         })
126     }
127
128     pub fn try_print_query_stack(handler: &Handler, num_frames: Option<usize>) {
129         eprintln!("query stack during panic:");
130
131         // Be careful relying on global state here: this code is called from
132         // a panic hook, which means that the global `Handler` may be in a weird
133         // state if it was responsible for triggering the panic.
134         let mut i = 0;
135         ty::tls::with_context_opt(|icx| {
136             if let Some(icx) = icx {
137                 let query_map = icx.tcx.queries.try_collect_active_jobs();
138
139                 let mut current_query = icx.query;
140
141                 while let Some(query) = current_query {
142                     if Some(i) == num_frames {
143                         break;
144                     }
145                     let query_info =
146                         if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query)) {
147                             info
148                         } else {
149                             break;
150                         };
151                     let mut diag = Diagnostic::new(
152                         Level::FailureNote,
153                         &format!(
154                             "#{} [{}] {}",
155                             i,
156                             query_info.info.query.name(),
157                             query_info.info.query.describe(icx.tcx)
158                         ),
159                     );
160                     diag.span =
161                         icx.tcx.sess.source_map().guess_head_span(query_info.info.span).into();
162                     handler.force_print_diagnostic(diag);
163
164                     current_query = query_info.job.parent;
165                     i += 1;
166                 }
167             }
168         });
169
170         if num_frames == None || num_frames >= Some(i) {
171             eprintln!("end of query stack");
172         } else {
173             eprintln!("we're just showing a limited slice of the query stack");
174         }
175     }
176 }
177
178 macro_rules! handle_cycle_error {
179     ([][$tcx: expr, $error:expr]) => {{
180         $tcx.report_cycle($error).emit();
181         Value::from_cycle_error($tcx)
182     }};
183     ([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
184         $tcx.report_cycle($error).emit();
185         $tcx.sess.abort_if_errors();
186         unreachable!()
187     }};
188     ([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
189         $tcx.report_cycle($error).delay_as_bug();
190         Value::from_cycle_error($tcx)
191     }};
192     ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
193         handle_cycle_error!([$($($modifiers)*)*][$($args)*])
194     };
195 }
196
197 macro_rules! is_anon {
198     ([]) => {{
199         false
200     }};
201     ([anon $($rest:tt)*]) => {{
202         true
203     }};
204     ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
205         is_anon!([$($($modifiers)*)*])
206     };
207 }
208
209 macro_rules! is_eval_always {
210     ([]) => {{
211         false
212     }};
213     ([eval_always $($rest:tt)*]) => {{
214         true
215     }};
216     ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
217         is_eval_always!([$($($modifiers)*)*])
218     };
219 }
220
221 macro_rules! query_storage {
222     ([][$K:ty, $V:ty]) => {
223         <<$K as Key>::CacheSelector as CacheSelector<$K, $V>>::Cache
224     };
225     ([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
226         <$ty as CacheSelector<$K, $V>>::Cache
227     };
228     ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
229         query_storage!([$($($modifiers)*)*][$($args)*])
230     };
231 }
232
233 macro_rules! hash_result {
234     ([][$hcx:expr, $result:expr]) => {{
235         dep_graph::hash_result($hcx, &$result)
236     }};
237     ([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
238         None
239     }};
240     ([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
241         hash_result!([$($($modifiers)*)*][$($args)*])
242     };
243 }
244
245 macro_rules! query_helper_param_ty {
246     (DefId) => { impl IntoQueryParam<DefId> };
247     ($K:ty) => { $K };
248 }
249
250 macro_rules! define_queries {
251     (<$tcx:tt>
252      $($(#[$attr:meta])*
253         [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
254
255         use std::mem;
256         use crate::{
257             rustc_data_structures::stable_hasher::HashStable,
258             rustc_data_structures::stable_hasher::StableHasher,
259             ich::StableHashingContext
260         };
261
262         define_queries_struct! {
263             tcx: $tcx,
264             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
265         }
266
267         #[allow(nonstandard_style)]
268         #[derive(Clone, Debug)]
269         pub enum Query<$tcx> {
270             $($(#[$attr])* $name($($K)*)),*
271         }
272
273         impl<$tcx> Query<$tcx> {
274             pub fn name(&self) -> &'static str {
275                 match *self {
276                     $(Query::$name(_) => stringify!($name),)*
277                 }
278             }
279
280             pub fn describe(&self, tcx: TyCtxt<$tcx>) -> String {
281                 let (r, name) = match *self {
282                     $(Query::$name(key) => {
283                         (queries::$name::describe(tcx, key), stringify!($name))
284                     })*
285                 };
286                 if tcx.sess.verbose() {
287                     format!("{} [{}]", r, name)
288                 } else {
289                     r
290                 }
291             }
292
293             // FIXME(eddyb) Get more valid `Span`s on queries.
294             pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span {
295                 if !span.is_dummy() {
296                     return span;
297                 }
298                 // The `def_span` query is used to calculate `default_span`,
299                 // so exit to avoid infinite recursion.
300                 if let Query::def_span(..) = *self {
301                     return span
302                 }
303                 match *self {
304                     $(Query::$name(key) => key.default_span(tcx),)*
305                 }
306             }
307         }
308
309         impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
310             fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
311                 mem::discriminant(self).hash_stable(hcx, hasher);
312                 match *self {
313                     $(Query::$name(key) => key.hash_stable(hcx, hasher),)*
314                 }
315             }
316         }
317
318         #[allow(nonstandard_style)]
319         pub mod queries {
320             use std::marker::PhantomData;
321
322             $(pub struct $name<$tcx> {
323                 data: PhantomData<&$tcx ()>
324             })*
325         }
326
327         // HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
328         // below, but using type aliases instead of associated types, to bypass
329         // the limitations around normalizing under HRTB - for example, this:
330         // `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
331         // doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
332         // This is primarily used by the `provide!` macro in `rustc_metadata`.
333         #[allow(nonstandard_style, unused_lifetimes)]
334         pub mod query_keys {
335             use super::*;
336
337             $(pub type $name<$tcx> = $($K)*;)*
338         }
339         #[allow(nonstandard_style, unused_lifetimes)]
340         pub mod query_values {
341             use super::*;
342
343             $(pub type $name<$tcx> = $V;)*
344         }
345         #[allow(nonstandard_style, unused_lifetimes)]
346         pub mod query_storage {
347             use super::*;
348
349             $(pub type $name<$tcx> = query_storage!([$($modifiers)*][$($K)*, $V]);)*
350         }
351         #[allow(nonstandard_style, unused_lifetimes)]
352         pub mod query_stored {
353             use super::*;
354
355             $(pub type $name<$tcx> = <query_storage::$name<$tcx> as QueryStorage>::Stored;)*
356         }
357
358         #[derive(Default)]
359         pub struct QueryCaches<$tcx> {
360             $($(#[$attr])* $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
361         }
362
363         $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
364             type Key = $($K)*;
365             type Value = $V;
366             type Stored = query_stored::$name<$tcx>;
367             const NAME: &'static str = stringify!($name);
368         }
369
370         impl<$tcx> QueryAccessors<TyCtxt<$tcx>> for queries::$name<$tcx> {
371             const ANON: bool = is_anon!([$($modifiers)*]);
372             const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
373             const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
374
375             type Cache = query_storage::$name<$tcx>;
376
377             #[inline(always)]
378             fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> {
379                 &tcx.queries.$name
380             }
381
382             #[inline(always)]
383             fn query_cache<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
384                 where 'tcx:'a
385             {
386                 &tcx.query_caches.$name
387             }
388
389             #[inline]
390             fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
391                 let provider = tcx.queries.providers.get(key.query_crate())
392                     // HACK(eddyb) it's possible crates may be loaded after
393                     // the query engine is created, and because crate loading
394                     // is not yet integrated with the query engine, such crates
395                     // would be missing appropriate entries in `providers`.
396                     .unwrap_or(&tcx.queries.fallback_extern_providers)
397                     .$name;
398                 provider(tcx, key)
399             }
400
401             fn hash_result(
402                 _hcx: &mut StableHashingContext<'_>,
403                 _result: &Self::Value
404             ) -> Option<Fingerprint> {
405                 hash_result!([$($modifiers)*][_hcx, _result])
406             }
407
408             fn handle_cycle_error(
409                 tcx: TyCtxt<'tcx>,
410                 error: CycleError<Query<'tcx>>
411             ) -> Self::Value {
412                 handle_cycle_error!([$($modifiers)*][tcx, error])
413             }
414         })*
415
416         #[derive(Copy, Clone)]
417         pub struct TyCtxtEnsure<'tcx> {
418             pub tcx: TyCtxt<'tcx>,
419         }
420
421         impl TyCtxtEnsure<$tcx> {
422             $($(#[$attr])*
423             #[inline(always)]
424             pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
425                 get_query::<queries::$name<'_>, _>(self.tcx, DUMMY_SP, key.into_query_param(), QueryMode::Ensure);
426             })*
427         }
428
429         #[derive(Copy, Clone)]
430         pub struct TyCtxtAt<'tcx> {
431             pub tcx: TyCtxt<'tcx>,
432             pub span: Span,
433         }
434
435         impl Deref for TyCtxtAt<'tcx> {
436             type Target = TyCtxt<'tcx>;
437             #[inline(always)]
438             fn deref(&self) -> &Self::Target {
439                 &self.tcx
440             }
441         }
442
443         impl TyCtxt<$tcx> {
444             /// Returns a transparent wrapper for `TyCtxt`, which ensures queries
445             /// are executed instead of just returning their results.
446             #[inline(always)]
447             pub fn ensure(self) -> TyCtxtEnsure<$tcx> {
448                 TyCtxtEnsure {
449                     tcx: self,
450                 }
451             }
452
453             /// Returns a transparent wrapper for `TyCtxt` which uses
454             /// `span` as the location of queries performed through it.
455             #[inline(always)]
456             pub fn at(self, span: Span) -> TyCtxtAt<$tcx> {
457                 TyCtxtAt {
458                     tcx: self,
459                     span
460                 }
461             }
462
463             $($(#[$attr])*
464             #[inline(always)]
465             #[must_use]
466             pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
467             {
468                 self.at(DUMMY_SP).$name(key.into_query_param())
469             })*
470
471             /// All self-profiling events generated by the query engine use
472             /// virtual `StringId`s for their `event_id`. This method makes all
473             /// those virtual `StringId`s point to actual strings.
474             ///
475             /// If we are recording only summary data, the ids will point to
476             /// just the query names. If we are recording query keys too, we
477             /// allocate the corresponding strings here.
478             pub fn alloc_self_profile_query_strings(self) {
479                 use crate::ty::query::profiling_support::{
480                     alloc_self_profile_query_strings_for_query_cache,
481                     QueryKeyStringCache,
482                 };
483
484                 if !self.prof.enabled() {
485                     return;
486                 }
487
488                 let mut string_cache = QueryKeyStringCache::new();
489
490                 $({
491                     alloc_self_profile_query_strings_for_query_cache(
492                         self,
493                         stringify!($name),
494                         &self.query_caches.$name,
495                         &mut string_cache,
496                     );
497                 })*
498             }
499         }
500
501         impl TyCtxtAt<$tcx> {
502             $($(#[$attr])*
503             #[inline(always)]
504             pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> query_stored::$name<$tcx>
505             {
506                 get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param(), QueryMode::Get).unwrap()
507             })*
508         }
509
510         define_provider_struct! {
511             tcx: $tcx,
512             input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
513         }
514
515         impl Copy for Providers {}
516         impl Clone for Providers {
517             fn clone(&self) -> Self { *self }
518         }
519     }
520 }
521
522 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
523 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
524 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
525 macro_rules! define_queries_struct {
526     (tcx: $tcx:tt,
527      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
528         pub struct Queries<$tcx> {
529             /// This provides access to the incremental compilation on-disk cache for query results.
530             /// Do not access this directly. It is only meant to be used by
531             /// `DepGraph::try_mark_green()` and the query infrastructure.
532             /// This is `None` if we are not incremental compilation mode
533             pub(crate) on_disk_cache: Option<OnDiskCache<'tcx>>,
534
535             providers: IndexVec<CrateNum, Providers>,
536             fallback_extern_providers: Box<Providers>,
537
538             $($(#[$attr])*  $name: QueryState<
539                 crate::dep_graph::DepKind,
540                 Query<$tcx>,
541                 query_keys::$name<$tcx>,
542             >,)*
543         }
544
545         impl<$tcx> Queries<$tcx> {
546             pub(crate) fn new(
547                 providers: IndexVec<CrateNum, Providers>,
548                 fallback_extern_providers: Providers,
549                 on_disk_cache: Option<OnDiskCache<'tcx>>,
550             ) -> Self {
551                 Queries {
552                     providers,
553                     fallback_extern_providers: Box::new(fallback_extern_providers),
554                     on_disk_cache,
555                     $($name: Default::default()),*
556                 }
557             }
558
559             pub(crate) fn try_collect_active_jobs(
560                 &self
561             ) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query>>> {
562                 let mut jobs = FxHashMap::default();
563
564                 $(
565                     self.$name.try_collect_active_jobs(
566                         <queries::$name<'tcx> as QueryAccessors<TyCtxt<'tcx>>>::DEP_KIND,
567                         Query::$name,
568                         &mut jobs,
569                     )?;
570                 )*
571
572                 Some(jobs)
573             }
574         }
575     };
576 }
577
578 macro_rules! define_provider_struct {
579     (tcx: $tcx:tt,
580      input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
581         pub struct Providers {
582             $(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
583         }
584
585         impl Default for Providers {
586             fn default() -> Self {
587                 $(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
588                     bug!("`tcx.{}({:?})` unsupported by its crate",
589                          stringify!($name), key);
590                 })*
591                 Providers { $($name),* }
592             }
593         }
594     };
595 }