]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_query_system/src/query/config.rs
Rollup merge of #106093 - notriddle:notriddle/docblock-short-overflow, r=GuillaumeGomez
[rust.git] / compiler / rustc_query_system / src / query / config.rs
1 //! Query configuration and description traits.
2
3 use crate::dep_graph::DepNode;
4 use crate::dep_graph::SerializedDepNodeIndex;
5 use crate::error::HandleCycleError;
6 use crate::ich::StableHashingContext;
7 use crate::query::caches::QueryCache;
8 use crate::query::{QueryContext, QueryState};
9
10 use rustc_data_structures::fingerprint::Fingerprint;
11 use std::fmt::Debug;
12 use std::hash::Hash;
13
14 pub trait QueryConfig<Qcx: QueryContext> {
15     const NAME: &'static str;
16
17     type Key: Eq + Hash + Clone + Debug;
18     type Value: Debug;
19     type Stored: Debug + Clone + std::borrow::Borrow<Self::Value>;
20
21     type Cache: QueryCache<Key = Self::Key, Stored = Self::Stored, Value = Self::Value>;
22
23     // Don't use this method to access query results, instead use the methods on TyCtxt
24     fn query_state<'a>(tcx: Qcx) -> &'a QueryState<Self::Key, Qcx::DepKind>
25     where
26         Qcx: 'a;
27
28     // Don't use this method to access query results, instead use the methods on TyCtxt
29     fn query_cache<'a>(tcx: Qcx) -> &'a Self::Cache
30     where
31         Qcx: 'a;
32
33     // Don't use this method to compute query results, instead use the methods on TyCtxt
34     fn make_vtable(tcx: Qcx, key: &Self::Key) -> QueryVTable<Qcx, Self::Key, Self::Value>;
35
36     fn cache_on_disk(tcx: Qcx::DepContext, key: &Self::Key) -> bool;
37
38     // Don't use this method to compute query results, instead use the methods on TyCtxt
39     fn execute_query(tcx: Qcx::DepContext, k: Self::Key) -> Self::Stored;
40 }
41
42 #[derive(Copy, Clone)]
43 pub struct QueryVTable<Qcx: QueryContext, K, V> {
44     pub anon: bool,
45     pub dep_kind: Qcx::DepKind,
46     pub eval_always: bool,
47     pub depth_limit: bool,
48     pub feedable: bool,
49
50     pub compute: fn(Qcx::DepContext, K) -> V,
51     pub hash_result: Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>,
52     pub handle_cycle_error: HandleCycleError,
53     // NOTE: this is also `None` if `cache_on_disk()` returns false, not just if it's unsupported by the query
54     pub try_load_from_disk: Option<fn(Qcx, SerializedDepNodeIndex) -> Option<V>>,
55 }
56
57 impl<Qcx: QueryContext, K, V> QueryVTable<Qcx, K, V> {
58     pub(crate) fn to_dep_node(&self, tcx: Qcx::DepContext, key: &K) -> DepNode<Qcx::DepKind>
59     where
60         K: crate::dep_graph::DepNodeParams<Qcx::DepContext>,
61     {
62         DepNode::construct(tcx, self.dep_kind, key)
63     }
64
65     pub(crate) fn compute(&self, tcx: Qcx::DepContext, key: K) -> V {
66         (self.compute)(tcx, key)
67     }
68 }