|
1 | 1 | use crate::dep_graph::DepNodeIndex; |
2 | | -use crate::ty::query::config::QueryAccessors; |
3 | 2 | use crate::ty::query::plumbing::{QueryLookup, QueryState, QueryStateShard}; |
4 | 3 | use crate::ty::TyCtxt; |
5 | 4 |
|
6 | 5 | use rustc_data_structures::fx::FxHashMap; |
7 | 6 | use rustc_data_structures::sharded::Sharded; |
8 | 7 | use std::default::Default; |
9 | 8 | use std::hash::Hash; |
| 9 | +use std::marker::PhantomData; |
10 | 10 |
|
11 | 11 | pub(crate) trait CacheSelector<K, V> { |
12 | | - type Cache: QueryCache<K, V>; |
| 12 | + type Cache: QueryCache<Key = K, Value = V>; |
13 | 13 | } |
14 | 14 |
|
15 | | -pub(crate) trait QueryCache<K, V>: Default { |
| 15 | +pub(crate) trait QueryCache: Default { |
| 16 | + type Key; |
| 17 | + type Value; |
16 | 18 | type Sharded: Default; |
17 | 19 |
|
18 | 20 | /// Checks if the query is already computed and in the cache. |
19 | 21 | /// It returns the shard index and a lock guard to the shard, |
20 | 22 | /// which will be used if the query is not in the cache and we need |
21 | 23 | /// to compute it. |
22 | | - fn lookup<'tcx, R, GetCache, OnHit, OnMiss, Q>( |
| 24 | + fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( |
23 | 25 | &self, |
24 | | - state: &'tcx QueryState<'tcx, Q>, |
| 26 | + state: &'tcx QueryState<'tcx, Self>, |
25 | 27 | get_cache: GetCache, |
26 | | - key: K, |
| 28 | + key: Self::Key, |
27 | 29 | // `on_hit` can be called while holding a lock to the query state shard. |
28 | 30 | on_hit: OnHit, |
29 | 31 | on_miss: OnMiss, |
30 | 32 | ) -> R |
31 | 33 | where |
32 | | - Q: QueryAccessors<'tcx>, |
33 | | - GetCache: for<'a> Fn(&'a mut QueryStateShard<'tcx, Q>) -> &'a mut Self::Sharded, |
34 | | - OnHit: FnOnce(&V, DepNodeIndex) -> R, |
35 | | - OnMiss: FnOnce(K, QueryLookup<'tcx, Q>) -> R; |
| 34 | + GetCache: for<'a> Fn( |
| 35 | + &'a mut QueryStateShard<'tcx, Self::Key, Self::Sharded>, |
| 36 | + ) -> &'a mut Self::Sharded, |
| 37 | + OnHit: FnOnce(&Self::Value, DepNodeIndex) -> R, |
| 38 | + OnMiss: FnOnce(Self::Key, QueryLookup<'tcx, Self::Key, Self::Sharded>) -> R; |
36 | 39 |
|
37 | 40 | fn complete( |
38 | 41 | &self, |
39 | 42 | tcx: TyCtxt<'tcx>, |
40 | 43 | lock_sharded_storage: &mut Self::Sharded, |
41 | | - key: K, |
42 | | - value: V, |
| 44 | + key: Self::Key, |
| 45 | + value: Self::Value, |
43 | 46 | index: DepNodeIndex, |
44 | 47 | ); |
45 | 48 |
|
46 | 49 | fn iter<R, L>( |
47 | 50 | &self, |
48 | 51 | shards: &Sharded<L>, |
49 | 52 | get_shard: impl Fn(&mut L) -> &mut Self::Sharded, |
50 | | - f: impl for<'a> FnOnce(Box<dyn Iterator<Item = (&'a K, &'a V, DepNodeIndex)> + 'a>) -> R, |
| 53 | + f: impl for<'a> FnOnce( |
| 54 | + Box<dyn Iterator<Item = (&'a Self::Key, &'a Self::Value, DepNodeIndex)> + 'a>, |
| 55 | + ) -> R, |
51 | 56 | ) -> R; |
52 | 57 | } |
53 | 58 |
|
54 | 59 | pub struct DefaultCacheSelector; |
55 | 60 |
|
56 | 61 | impl<K: Eq + Hash, V: Clone> CacheSelector<K, V> for DefaultCacheSelector { |
57 | | - type Cache = DefaultCache; |
| 62 | + type Cache = DefaultCache<K, V>; |
58 | 63 | } |
59 | 64 |
|
60 | | -#[derive(Default)] |
61 | | -pub struct DefaultCache; |
| 65 | +pub struct DefaultCache<K, V>(PhantomData<(K, V)>); |
| 66 | + |
| 67 | +impl<K, V> Default for DefaultCache<K, V> { |
| 68 | + fn default() -> Self { |
| 69 | + DefaultCache(PhantomData) |
| 70 | + } |
| 71 | +} |
62 | 72 |
|
63 | | -impl<K: Eq + Hash, V: Clone> QueryCache<K, V> for DefaultCache { |
| 73 | +impl<K: Eq + Hash, V: Clone> QueryCache for DefaultCache<K, V> { |
| 74 | + type Key = K; |
| 75 | + type Value = V; |
64 | 76 | type Sharded = FxHashMap<K, (V, DepNodeIndex)>; |
65 | 77 |
|
66 | 78 | #[inline(always)] |
67 | | - fn lookup<'tcx, R, GetCache, OnHit, OnMiss, Q>( |
| 79 | + fn lookup<'tcx, R, GetCache, OnHit, OnMiss>( |
68 | 80 | &self, |
69 | | - state: &'tcx QueryState<'tcx, Q>, |
| 81 | + state: &'tcx QueryState<'tcx, Self>, |
70 | 82 | get_cache: GetCache, |
71 | 83 | key: K, |
72 | 84 | on_hit: OnHit, |
73 | 85 | on_miss: OnMiss, |
74 | 86 | ) -> R |
75 | 87 | where |
76 | | - Q: QueryAccessors<'tcx>, |
77 | | - GetCache: for<'a> Fn(&'a mut QueryStateShard<'tcx, Q>) -> &'a mut Self::Sharded, |
| 88 | + GetCache: |
| 89 | + for<'a> Fn(&'a mut QueryStateShard<'tcx, K, Self::Sharded>) -> &'a mut Self::Sharded, |
78 | 90 | OnHit: FnOnce(&V, DepNodeIndex) -> R, |
79 | | - OnMiss: FnOnce(K, QueryLookup<'tcx, Q>) -> R, |
| 91 | + OnMiss: FnOnce(K, QueryLookup<'tcx, K, Self::Sharded>) -> R, |
80 | 92 | { |
81 | 93 | let mut lookup = state.get_lookup(&key); |
82 | 94 | let lock = &mut *lookup.lock; |
|
0 commit comments