]> git.lizzy.rs Git - rust.git/blob - src/librustc/ty/query/config.rs
Rollup merge of #60772 - timvermeulen:slice_iter_nth_back, r=scottmcm
[rust.git] / src / librustc / ty / query / config.rs
1 use crate::dep_graph::SerializedDepNodeIndex;
2 use crate::dep_graph::DepNode;
3 use crate::hir::def_id::{CrateNum, DefId};
4 use crate::ty::TyCtxt;
5 use crate::ty::query::queries;
6 use crate::ty::query::{Query, QueryName};
7 use crate::ty::query::QueryCache;
8 use crate::ty::query::plumbing::CycleError;
9 use crate::util::profiling::ProfileCategory;
10
11 use std::borrow::Cow;
12 use std::hash::Hash;
13 use std::fmt::Debug;
14 use rustc_data_structures::sync::Lock;
15 use rustc_data_structures::fingerprint::Fingerprint;
16 use crate::ich::StableHashingContext;
17
18 // Query configuration and description traits.
19
20 // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
21 #[allow(unused_lifetimes)]
22 pub trait QueryConfig<'tcx> {
23     const NAME: QueryName;
24     const CATEGORY: ProfileCategory;
25
26     type Key: Eq + Hash + Clone + Debug;
27     type Value: Clone;
28 }
29
30 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
31     fn query(key: Self::Key) -> Query<'tcx>;
32
33     // Don't use this method to access query results, instead use the methods on TyCtxt
34     fn query_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Lock<QueryCache<'tcx, Self>>;
35
36     fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
37
38     // Don't use this method to compute query results, instead use the methods on TyCtxt
39     fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
40
41     fn hash_result(
42         hcx: &mut StableHashingContext<'_>,
43         result: &Self::Value
44     ) -> Option<Fingerprint>;
45
46     fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
47 }
48
49 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
50     fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
51
52     #[inline]
53     fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key) -> bool {
54         false
55     }
56
57     fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
58         bug!("QueryDescription::load_from_disk() called for an unsupported query.")
59     }
60 }
61
62 impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
63     default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
64         if !tcx.sess.verbose() {
65             format!("processing `{}`", tcx.def_path_str(def_id)).into()
66         } else {
67             let name = unsafe { ::std::intrinsics::type_name::<M>() };
68             format!("processing {:?} with query `{}`", def_id, name).into()
69         }
70     }
71 }
72
73 impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
74     fn describe(_tcx: TyCtxt<'_>, _: CrateNum) -> Cow<'static, str> {
75         "running analysis passes on this crate".into()
76     }
77 }
78
79 macro_rules! impl_disk_cacheable_query(
80     ($query_name:ident, |$tcx:tt, $key:tt| $cond:expr) => {
81         impl<'tcx> QueryDescription<'tcx> for queries::$query_name<'tcx> {
82             #[inline]
83             fn cache_on_disk($tcx: TyCtxt<'tcx>, $key: Self::Key) -> bool {
84                 $cond
85             }
86
87             #[inline]
88             fn try_load_from_disk(tcx: TyCtxt<'tcx>,
89                                       id: SerializedDepNodeIndex)
90                                       -> Option<Self::Value> {
91                 tcx.queries.on_disk_cache.try_load_query_result(tcx, id)
92             }
93         }
94     }
95 );
96
97 impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
98     def_id.is_local() && tcx.is_closure(def_id)
99 });
100
101 impl_disk_cacheable_query!(unsafety_check_result, |_, def_id| def_id.is_local());
102 impl_disk_cacheable_query!(borrowck, |_, def_id| def_id.is_local());
103 impl_disk_cacheable_query!(check_match, |_, def_id| def_id.is_local());
104 impl_disk_cacheable_query!(predicates_of, |_, def_id| def_id.is_local());
105 impl_disk_cacheable_query!(used_trait_imports, |_, def_id| def_id.is_local());
106 impl_disk_cacheable_query!(codegen_fn_attrs, |_, _| true);
107 impl_disk_cacheable_query!(specialization_graph_of, |_, _| true);