1 use crate::dep_graph::SerializedDepNodeIndex;
2 use crate::dep_graph::DepNode;
3 use crate::hir::def_id::{CrateNum, DefId};
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;
14 use rustc_data_structures::sync::Lock;
15 use rustc_data_structures::fingerprint::Fingerprint;
16 use crate::ich::StableHashingContext;
18 // Query configuration and description traits.
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;
26 type Key: Eq + Hash + Clone + Debug;
30 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
31 fn query(key: Self::Key) -> Query<'tcx>;
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>>;
36 fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
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;
42 hcx: &mut StableHashingContext<'_>,
44 ) -> Option<Fingerprint>;
46 fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
49 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
50 fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
53 fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key) -> bool {
57 fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
58 bug!("QueryDescription::load_from_disk() called for an unsupported query.")
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()
67 let name = unsafe { ::std::intrinsics::type_name::<M>() };
68 format!("processing {:?} with query `{}`", def_id, name).into()
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()
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> {
83 fn cache_on_disk($tcx: TyCtxt<'tcx>, $key: Self::Key) -> bool {
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)
97 impl_disk_cacheable_query!(mir_borrowck, |tcx, def_id| {
98 def_id.is_local() && tcx.is_closure(def_id)
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);