1 use crate::dep_graph::SerializedDepNodeIndex;
2 use crate::dep_graph::{DepKind, DepNode};
3 use crate::ty::query::plumbing::CycleError;
4 use crate::ty::query::queries;
5 use crate::ty::query::{Query, QueryCache};
7 use rustc_data_structures::profiling::ProfileCategory;
8 use rustc_hir::def_id::{CrateNum, DefId};
10 use crate::ich::StableHashingContext;
11 use rustc_data_structures::fingerprint::Fingerprint;
12 use rustc_data_structures::sharded::Sharded;
17 // Query configuration and description traits.
19 // FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
20 #[allow(unused_lifetimes)]
21 pub trait QueryConfig<'tcx> {
22 const NAME: &'static str;
23 const CATEGORY: ProfileCategory;
25 type Key: Eq + Hash + Clone + Debug;
29 pub(crate) trait QueryAccessors<'tcx>: QueryConfig<'tcx> {
31 const EVAL_ALWAYS: bool;
33 fn query(key: Self::Key) -> Query<'tcx>;
35 // Don't use this method to access query results, instead use the methods on TyCtxt
36 fn query_cache<'a>(tcx: TyCtxt<'tcx>) -> &'a Sharded<QueryCache<'tcx, Self>>;
38 fn to_dep_node(tcx: TyCtxt<'tcx>, key: &Self::Key) -> DepNode;
40 fn dep_kind() -> DepKind;
42 // Don't use this method to compute query results, instead use the methods on TyCtxt
43 fn compute(tcx: TyCtxt<'tcx>, key: Self::Key) -> Self::Value;
45 fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
46 -> Option<Fingerprint>;
48 fn handle_cycle_error(tcx: TyCtxt<'tcx>, error: CycleError<'tcx>) -> Self::Value;
51 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<'tcx> {
52 fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
55 fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
59 fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
60 bug!("QueryDescription::load_from_disk() called for an unsupported query.")
64 impl<'tcx, M: QueryAccessors<'tcx, Key = DefId>> QueryDescription<'tcx> for M {
65 default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
66 if !tcx.sess.verbose() {
67 format!("processing `{}`", tcx.def_path_str(def_id)).into()
69 let name = ::std::any::type_name::<M>();
70 format!("processing {:?} with query `{}`", def_id, name).into()
74 default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
78 default fn try_load_from_disk(
80 _: SerializedDepNodeIndex,
81 ) -> Option<Self::Value> {
82 bug!("QueryDescription::load_from_disk() called for an unsupported query.")
86 impl<'tcx> QueryDescription<'tcx> for queries::analysis<'tcx> {
87 fn describe(_tcx: TyCtxt<'_>, _: CrateNum) -> Cow<'static, str> {
88 "running analysis passes on this crate".into()