1 //! Query configuration and description traits.
3 use crate::dep_graph::SerializedDepNodeIndex;
4 use crate::dep_graph::{DepKind, DepNode};
5 use crate::ty::query::caches::QueryCache;
6 use crate::ty::query::plumbing::CycleError;
7 use crate::ty::query::QueryState;
9 use rustc_data_structures::profiling::ProfileCategory;
10 use rustc_hir::def_id::DefId;
12 use crate::ich::StableHashingContext;
13 use rustc_data_structures::fingerprint::Fingerprint;
18 pub trait QueryConfig<CTX> {
19 const NAME: &'static str;
20 const CATEGORY: ProfileCategory;
22 type Key: Eq + Hash + Clone + Debug;
26 pub trait QueryContext: Copy {
30 pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
32 const EVAL_ALWAYS: bool;
33 const DEP_KIND: DepKind;
35 type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
37 // Don't use this method to access query results, instead use the methods on TyCtxt
38 fn query_state<'a>(tcx: CTX) -> &'a QueryState<CTX, Self::Cache>;
40 fn to_dep_node(tcx: CTX, key: &Self::Key) -> DepNode;
42 // Don't use this method to compute query results, instead use the methods on TyCtxt
43 fn compute(tcx: CTX, key: Self::Key) -> Self::Value;
45 fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
46 -> Option<Fingerprint>;
48 fn handle_cycle_error(tcx: CTX, error: CycleError<CTX>) -> Self::Value;
51 pub(crate) trait QueryDescription<'tcx>: QueryAccessors<TyCtxt<'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> QueryDescription<'tcx> for M
66 M: QueryAccessors<TyCtxt<'tcx>, Key = DefId>,
67 //M::Cache: QueryCache<DefId, M::Value>,
69 default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
70 if !tcx.sess.verbose() {
71 format!("processing `{}`", tcx.def_path_str(def_id)).into()
73 let name = ::std::any::type_name::<M>();
74 format!("processing {:?} with query `{}`", def_id, name).into()
78 default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
82 default fn try_load_from_disk(
84 _: SerializedDepNodeIndex,
85 ) -> Option<Self::Value> {
86 bug!("QueryDescription::load_from_disk() called for an unsupported query.")