rustc_query_system/query/
dispatcher.rs1use std::fmt::Debug;
2use std::hash::Hash;
3
4use rustc_data_structures::fingerprint::Fingerprint;
5use rustc_span::ErrorGuaranteed;
6
7use super::QueryStackFrameExtra;
8use crate::dep_graph::{DepKind, DepNode, DepNodeParams, HasDepContext, SerializedDepNodeIndex};
9use crate::ich::StableHashingContext;
10use crate::query::caches::QueryCache;
11use crate::query::{CycleError, CycleErrorHandling, DepNodeIndex, QueryContext, QueryState};
12
13pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
14
15#[expect(type_alias_bounds)]
17type DepContextOf<'tcx, This: QueryDispatcher<'tcx>> =
18 <<This as QueryDispatcher<'tcx>>::Qcx as HasDepContext>::DepContext;
19
20pub trait QueryDispatcher<'tcx>: Copy {
29 fn name(self) -> &'static str;
30
31 type Qcx: QueryContext<'tcx>;
33
34 type Key: DepNodeParams<DepContextOf<'tcx, Self>> + Eq + Hash + Copy + Debug;
37 type Value: Copy;
38
39 type Cache: QueryCache<Key = Self::Key, Value = Self::Value>;
40
41 fn format_value(self) -> fn(&Self::Value) -> String;
42
43 fn query_state<'a>(self, tcx: Self::Qcx) -> &'a QueryState<'tcx, Self::Key>;
45
46 fn query_cache<'a>(self, tcx: Self::Qcx) -> &'a Self::Cache;
48
49 fn will_cache_on_disk_for_key(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> bool;
50
51 fn execute_query(self, tcx: DepContextOf<'tcx, Self>, k: Self::Key) -> Self::Value;
53
54 fn compute(self, tcx: Self::Qcx, key: Self::Key) -> Self::Value;
55
56 fn try_load_from_disk(
57 self,
58 tcx: Self::Qcx,
59 key: &Self::Key,
60 prev_index: SerializedDepNodeIndex,
61 index: DepNodeIndex,
62 ) -> Option<Self::Value>;
63
64 fn is_loadable_from_disk(
65 self,
66 qcx: Self::Qcx,
67 key: &Self::Key,
68 idx: SerializedDepNodeIndex,
69 ) -> bool;
70
71 fn value_from_cycle_error(
73 self,
74 tcx: DepContextOf<'tcx, Self>,
75 cycle_error: &CycleError<QueryStackFrameExtra>,
76 guar: ErrorGuaranteed,
77 ) -> Self::Value;
78
79 fn anon(self) -> bool;
80 fn eval_always(self) -> bool;
81 fn depth_limit(self) -> bool;
82 fn feedable(self) -> bool;
83
84 fn dep_kind(self) -> DepKind;
85 fn cycle_error_handling(self) -> CycleErrorHandling;
86 fn hash_result(self) -> HashResult<Self::Value>;
87
88 fn construct_dep_node(self, tcx: DepContextOf<'tcx, Self>, key: &Self::Key) -> DepNode {
90 DepNode::construct(tcx, self.dep_kind(), key)
91 }
92}