rustc_query_system/query/
config.rs

1//! Query configuration and description traits.
2
3use std::fmt::Debug;
4use std::hash::Hash;
5
6use rustc_data_structures::fingerprint::Fingerprint;
7use rustc_span::ErrorGuaranteed;
8
9use super::QueryStackFrameExtra;
10use crate::dep_graph::{DepKind, DepNode, DepNodeParams, SerializedDepNodeIndex};
11use crate::error::HandleCycleError;
12use crate::ich::StableHashingContext;
13use crate::query::caches::QueryCache;
14use crate::query::{CycleError, DepNodeIndex, QueryContext, QueryState};
15
16pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
17
18pub trait QueryConfig<Qcx: QueryContext>: Copy {
19    fn name(self) -> &'static str;
20
21    // `Key` and `Value` are `Copy` instead of `Clone` to ensure copying them stays cheap,
22    // but it isn't necessary.
23    type Key: DepNodeParams<Qcx::DepContext> + Eq + Hash + Copy + Debug;
24    type Value: Copy;
25
26    type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
27
28    fn format_value(self) -> fn(&Self::Value) -> String;
29
30    // Don't use this method to access query results, instead use the methods on TyCtxt
31    fn query_state<'a>(self, tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::QueryInfo>
32    where
33        Qcx: 'a;
34
35    // Don't use this method to access query results, instead use the methods on TyCtxt
36    fn query_cache<'a>(self, tcx: Qcx) -> &'a Self::Cache
37    where
38        Qcx: 'a;
39
40    fn cache_on_disk(self, tcx: Qcx::DepContext, key: &Self::Key) -> bool;
41
42    // Don't use this method to compute query results, instead use the methods on TyCtxt
43    fn execute_query(self, tcx: Qcx::DepContext, k: Self::Key) -> Self::Value;
44
45    fn compute(self, tcx: Qcx, key: Self::Key) -> Self::Value;
46
47    fn try_load_from_disk(
48        self,
49        tcx: Qcx,
50        key: &Self::Key,
51        prev_index: SerializedDepNodeIndex,
52        index: DepNodeIndex,
53    ) -> Option<Self::Value>;
54
55    fn loadable_from_disk(self, qcx: Qcx, key: &Self::Key, idx: SerializedDepNodeIndex) -> bool;
56
57    /// Synthesize an error value to let compilation continue after a cycle.
58    fn value_from_cycle_error(
59        self,
60        tcx: Qcx::DepContext,
61        cycle_error: &CycleError<QueryStackFrameExtra>,
62        guar: ErrorGuaranteed,
63    ) -> Self::Value;
64
65    fn anon(self) -> bool;
66    fn eval_always(self) -> bool;
67    fn depth_limit(self) -> bool;
68    fn feedable(self) -> bool;
69
70    fn dep_kind(self) -> DepKind;
71    fn handle_cycle_error(self) -> HandleCycleError;
72    fn hash_result(self) -> HashResult<Self::Value>;
73
74    // Just here for convenience and checking that the key matches the kind, don't override this.
75    fn construct_dep_node(self, tcx: Qcx::DepContext, key: &Self::Key) -> DepNode {
76        DepNode::construct(tcx, self.dep_kind(), key)
77    }
78}