1 //! Query configuration and description traits.
3 use crate::dep_graph::{DepNode, DepNodeParams, SerializedDepNodeIndex};
4 use crate::error::HandleCycleError;
5 use crate::ich::StableHashingContext;
6 use crate::query::caches::QueryCache;
7 use crate::query::{QueryContext, QueryState};
9 use rustc_data_structures::fingerprint::Fingerprint;
13 pub type HashResult<Qcx, Q> =
14 Option<fn(&mut StableHashingContext<'_>, &<Q as QueryConfig<Qcx>>::Value) -> Fingerprint>;
16 pub type TryLoadFromDisk<Qcx, Q> =
17 Option<fn(Qcx, SerializedDepNodeIndex) -> Option<<Q as QueryConfig<Qcx>>::Value>>;
19 pub trait QueryConfig<Qcx: QueryContext> {
20 const NAME: &'static str;
22 type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Clone + Debug;
24 type Stored: Debug + Clone + std::borrow::Borrow<Self::Value>;
26 type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
28 // Don't use this method to access query results, instead use the methods on TyCtxt
29 fn query_state<'a>(tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::DepKind>
33 // Don't use this method to access query results, instead use the methods on TyCtxt
34 fn query_cache<'a>(tcx: Qcx) -> &'a Self::Cache
38 fn cache_on_disk(tcx: Qcx::DepContext, key: &Self::Key) -> bool;
40 // Don't use this method to compute query results, instead use the methods on TyCtxt
41 fn execute_query(tcx: Qcx::DepContext, k: Self::Key) -> Self::Stored;
43 fn compute(tcx: Qcx, key: &Self::Key) -> fn(Qcx::DepContext, Self::Key) -> Self::Value;
45 fn try_load_from_disk(qcx: Qcx, idx: &Self::Key) -> TryLoadFromDisk<Qcx, Self>;
48 const EVAL_ALWAYS: bool;
49 const DEPTH_LIMIT: bool;
52 const DEP_KIND: Qcx::DepKind;
53 const HANDLE_CYCLE_ERROR: HandleCycleError;
55 const HASH_RESULT: HashResult<Qcx, Self>;
57 // Just here for convernience and checking that the key matches the kind, don't override this.
58 fn construct_dep_node(tcx: Qcx::DepContext, key: &Self::Key) -> DepNode<Qcx::DepKind> {
59 DepNode::construct(tcx, Self::DEP_KIND, key)