]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_middle/src/ty/query/plumbing.rs
Remove unused ProfileCategory.
[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 reyling 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! define_queries {
246     (<$tcx:tt> $($category:tt {
247         $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*
248     },)*) => {
249         define_queries_inner! { <$tcx>
250             $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)*
251         }
252     }
253 }
254
255 macro_rules! query_helper_param_ty {
256     (DefId) => { impl IntoQueryParam<DefId> };
257     ($K:ty) => { $K };
258 }
259
260 macro_rules! define_queries_inner {
261     (<$tcx:tt>
262      $($(#[$attr:meta])* category<$category:tt>
263         [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
264
265         use std::mem;
266         use crate::{
267             rustc_data_structures::stable_hasher::HashStable,
268             rustc_data_structures::stable_hasher::StableHasher,
269             ich::StableHashingContext
270         };
271
272         define_queries_struct! {
273             tcx: $tcx,
274             input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
275         }
276
277         #[allow(nonstandard_style)]
278         #[derive(Clone, Debug)]
279         pub enum Query<$tcx> {
280             $($(#[$attr])* $name($($K)*)),*
281         }
282
283         impl<$tcx> Query<$tcx> {
284             pub fn name(&self) -> &'static str {
285                 match *self {
286                     $(Query::$name(_) => stringify!($name),)*
287                 }
288             }
289
290             pub fn describe(&self, tcx: TyCtxt<$tcx>) -> Cow<'static, str> {
291                 let (r, name) = match *self {
292                     $(Query::$name(key) => {
293                         (queries::$name::describe(tcx, key), stringify!($name))
294                     })*
295                 };
296                 if tcx.sess.verbose() {
297                     format!("{} [{}]", r, name).into()
298                 } else {
299                     r
300                 }
301             }
302
303             // FIXME(eddyb) Get more valid `Span`s on queries.
304             pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span {
305                 if !span.is_dummy() {
306                     return span;
307                 }
308                 // The `def_span` query is used to calculate `default_span`,
309                 // so exit to avoid infinite recursion.
310                 if let Query::def_span(..) = *self {
311                     return span
312                 }
313                 match *self {
314                     $(Query::$name(key) => key.default_span(tcx),)*
315                 }
316             }
317         }
318
319         impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
320             fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
321                 mem::discriminant(self).hash_stable(hcx, hasher);
322                 match *self {
323                     $(Query::$name(key) => key.hash_stable(hcx, hasher),)*
324                 }
325             }
326         }
327
328         #[allow(nonstandard_style)]
329         pub mod queries {
330             use std::marker::PhantomData;
331
332             $(pub struct $name<$tcx> {
333                 data: PhantomData<&$tcx ()>
334             })*
335         }
336
337         // HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
338         // below, but using type aliases instead of associated types, to bypass
339         // the limitations around normalizing under HRTB - for example, this:
340         // `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
341         // doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
342         // This is primarily used by the `provide!` macro in `rustc_metadata`.
343         #[allow(nonstandard_style, unused_lifetimes)]
344         pub mod query_keys {
345             use super::*;
346
347             $(pub type $name<$tcx> = $($K)*;)*
348         }
349         #[allow(nonstandard_style, unused_lifetimes)]
350         pub mod query_values {
351             use super::*;
352
353             $(pub type $name<$tcx> = $V;)*
354         }
355
356         $(impl<$tcx> QueryConfig for queries::$name<$tcx> {
357             type Key = $($K)*;
358             type Value = $V;
359             type Stored = <
360                 query_storage!([$($modifiers)*][$($K)*, $V])
361                 as QueryStorage
362             >::Stored;
363             const NAME: &'static str = stringify!($name);
364         }
365
366         impl<$tcx> QueryAccessors<TyCtxt<$tcx>> for queries::$name<$tcx> {
367             const ANON: bool = is_anon!([$($modifiers)*]);
368             const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
369             const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
370
371             type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
372
373             #[inline(always)]
374             fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query, Self::Cache> {
375                 &tcx.queries.$name
376             }
377
378             #[inline]
379             fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value {
380                 let provider = tcx.queries.providers.get(key.query_crate())
381                     // HACK(eddyb) it's possible crates may be loaded after
382                     // the query engine is created, and because crate loading
383                     // is not yet integrated with the query engine, such crates
384                     // would be missing appropriate entries in `providers`.
385                     .unwrap_or(&tcx.queries.fallback_extern_providers)
386                     .$name;
387                 provider(tcx, key)
388             }
389
390             fn hash_result(
391                 _hcx: &mut StableHashingContext<'_>,
392                 _result: &Self::Value
393             ) -> Option<Fingerprint> {
394                 hash_result!([$($modifiers)*][_hcx, _result])
395             }
396
397             fn handle_cycle_error(
398                 tcx: TyCtxt<'tcx>,
399                 error: CycleError<Query<'tcx>>
400             ) -> Self::Value {
401                 handle_cycle_error!([$($modifiers)*][tcx, error])
402             }
403         })*
404
405         #[derive(Copy, Clone)]
406         pub struct TyCtxtEnsure<'tcx> {
407             pub tcx: TyCtxt<'tcx>,
408         }
409
410         impl TyCtxtEnsure<$tcx> {
411             $($(#[$attr])*
412             #[inline(always)]
413             pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
414                 ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
415             })*
416         }
417
418         #[derive(Copy, Clone)]
419         pub struct TyCtxtAt<'tcx> {
420             pub tcx: TyCtxt<'tcx>,
421             pub span: Span,
422         }
423
424         impl Deref for TyCtxtAt<'tcx> {
425             type Target = TyCtxt<'tcx>;
426             #[inline(always)]
427             fn deref(&self) -> &Self::Target {
428                 &self.tcx
429             }
430         }
431
432         impl TyCtxt<$tcx> {
433             /// Returns a transparent wrapper for `TyCtxt`, which ensures queries
434             /// are executed instead of just returning their results.
435             #[inline(always)]
436             pub fn ensure(self) -> TyCtxtEnsure<$tcx> {
437                 TyCtxtEnsure {
438                     tcx: self,
439                 }
440             }
441
442             /// Returns a transparent wrapper for `TyCtxt` which uses
443             /// `span` as the location of queries performed through it.
444             #[inline(always)]
445             pub fn at(self, span: Span) -> TyCtxtAt<$tcx> {
446                 TyCtxtAt {
447                     tcx: self,
448                     span
449                 }
450             }
451
452             $($(#[$attr])*
453             #[inline(always)]
454             #[must_use]
455             pub fn $name(self, key: query_helper_param_ty!($($K)*))
456                 -> <queries::$name<$tcx> as QueryConfig>::Stored
457             {
458                 self.at(DUMMY_SP).$name(key.into_query_param())
459             })*
460
461             /// All self-profiling events generated by the query engine use
462             /// virtual `StringId`s for their `event_id`. This method makes all
463             /// those virtual `StringId`s point to actual strings.
464             ///
465             /// If we are recording only summary data, the ids will point to
466             /// just the query names. If we are recording query keys too, we
467             /// allocate the corresponding strings here.
468             pub fn alloc_self_profile_query_strings(self) {
469                 use crate::ty::query::profiling_support::{
470                     alloc_self_profile_query_strings_for_query_cache,
471                     QueryKeyStringCache,
472                 };
473
474                 if !self.prof.enabled() {
475                     return;
476                 }
477
478                 let mut string_cache = QueryKeyStringCache::new();
479
480                 $({
481                     alloc_self_profile_query_strings_for_query_cache(
482                         self,
483                         stringify!($name),
484                         &self.queries.$name,
485                         &mut string_cache,
486                     );
487                 })*
488             }
489         }
490
491         impl TyCtxtAt<$tcx> {
492             $($(#[$attr])*
493             #[inline(always)]
494             pub fn $name(self, key: query_helper_param_ty!($($K)*))
495                 -> <queries::$name<$tcx> as QueryConfig>::Stored
496             {
497                 get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
498             })*
499         }
500
501         define_provider_struct! {
502             tcx: $tcx,
503             input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
504         }
505
506         impl Copy for Providers {}
507         impl Clone for Providers {
508             fn clone(&self) -> Self { *self }
509         }
510     }
511 }
512
513 // FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
514 // We should either not take `$tcx` at all and use `'tcx` everywhere, or use
515 // `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
516 macro_rules! define_queries_struct {
517     (tcx: $tcx:tt,
518      input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
519         pub struct Queries<$tcx> {
520             /// This provides access to the incrimental comilation on-disk cache for query results.
521             /// Do not access this directly. It is only meant to be used by
522             /// `DepGraph::try_mark_green()` and the query infrastructure.
523             pub(crate) on_disk_cache: OnDiskCache<'tcx>,
524
525             providers: IndexVec<CrateNum, Providers>,
526             fallback_extern_providers: Box<Providers>,
527
528             $($(#[$attr])*  $name: QueryState<
529                 crate::dep_graph::DepKind,
530                 <TyCtxt<$tcx> as QueryContext>::Query,
531                 <queries::$name<$tcx> as QueryAccessors<TyCtxt<'tcx>>>::Cache,
532             >,)*
533         }
534
535         impl<$tcx> Queries<$tcx> {
536             pub(crate) fn new(
537                 providers: IndexVec<CrateNum, Providers>,
538                 fallback_extern_providers: Providers,
539                 on_disk_cache: OnDiskCache<'tcx>,
540             ) -> Self {
541                 Queries {
542                     providers,
543                     fallback_extern_providers: Box::new(fallback_extern_providers),
544                     on_disk_cache,
545                     $($name: Default::default()),*
546                 }
547             }
548
549             pub(crate) fn try_collect_active_jobs(
550                 &self
551             ) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, <TyCtxt<$tcx> as QueryContext>::Query>>> {
552                 let mut jobs = FxHashMap::default();
553
554                 $(
555                     self.$name.try_collect_active_jobs(
556                         <queries::$name<'tcx> as QueryAccessors<TyCtxt<'tcx>>>::DEP_KIND,
557                         Query::$name,
558                         &mut jobs,
559                     )?;
560                 )*
561
562                 Some(jobs)
563             }
564         }
565     };
566 }
567
568 macro_rules! define_provider_struct {
569     (tcx: $tcx:tt,
570      input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
571         pub struct Providers {
572             $(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
573         }
574
575         impl Default for Providers {
576             fn default() -> Self {
577                 $(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
578                     bug!("`tcx.{}({:?})` unsupported by its crate",
579                          stringify!($name), key);
580                 })*
581                 Providers { $($name),* }
582             }
583         }
584     };
585 }