Skip to content

Commit bc9a202

Browse files
committed
Use Key impl to select cache.
1 parent ade5cff commit bc9a202

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

compiler/rustc_middle/src/query/keys.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ use crate::ty::subst::{GenericArg, SubstsRef};
88
use crate::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
99
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1010
use rustc_hir::hir_id::{HirId, OwnerId};
11+
use rustc_query_system::query::{DefaultCacheSelector, VecCacheSelector};
1112
use rustc_span::symbol::{Ident, Symbol};
1213
use rustc_span::{Span, DUMMY_SP};
1314

1415
/// The `Key` trait controls what types can legally be used as the key
1516
/// for a query.
16-
pub trait Key {
17+
pub trait Key: Sized {
18+
type CacheSelector = DefaultCacheSelector<Self>;
19+
1720
/// Given an instance of this key, what crate is it referring to?
1821
/// This is used to find the provider.
1922
fn query_crate_is_local(&self) -> bool;
@@ -100,6 +103,8 @@ impl<'tcx> Key for mir::interpret::LitToConstInput<'tcx> {
100103
}
101104

102105
impl Key for CrateNum {
106+
type CacheSelector = VecCacheSelector<Self>;
107+
103108
#[inline(always)]
104109
fn query_crate_is_local(&self) -> bool {
105110
*self == LOCAL_CRATE
@@ -110,6 +115,8 @@ impl Key for CrateNum {
110115
}
111116

112117
impl Key for OwnerId {
118+
type CacheSelector = VecCacheSelector<Self>;
119+
113120
#[inline(always)]
114121
fn query_crate_is_local(&self) -> bool {
115122
true
@@ -123,6 +130,8 @@ impl Key for OwnerId {
123130
}
124131

125132
impl Key for LocalDefId {
133+
type CacheSelector = VecCacheSelector<Self>;
134+
126135
#[inline(always)]
127136
fn query_crate_is_local(&self) -> bool {
128137
true

compiler/rustc_middle/src/ty/query.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::mir::interpret::{
1515
};
1616
use crate::mir::interpret::{LitToConstError, LitToConstInput};
1717
use crate::mir::mono::CodegenUnit;
18+
use crate::query::Key;
1819
use crate::thir;
1920
use crate::traits::query::{
2021
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal,
@@ -120,17 +121,15 @@ macro_rules! query_helper_param_ty {
120121
}
121122

122123
macro_rules! query_storage {
123-
// FIXME(cjgillot) this macro-based way to perform type-based dispatch is clearly brittle.
124-
// It should probably be replaced by an associated type on the `Key` trait.
125-
([][CrateNum, $V:ty]) => { VecCache<CrateNum, $V> };
126-
([(arena_cache) $($rest:tt)*][CrateNum, $V:ty]) => { VecArenaCache<'tcx, CrateNum, $V> };
127-
([][LocalDefId, $V:ty]) => { VecCache<LocalDefId, $V> };
128-
([(arena_cache) $($rest:tt)*][LocalDefId, $V:ty]) => { VecArenaCache<'tcx, LocalDefId, $V> };
129-
([][hir::OwnerId, $V:ty]) => { VecCache<hir::OwnerId, $V> };
130-
([(arena_cache) $($rest:tt)*][hir::OwnerId, $V:ty]) => { VecArenaCache<'tcx, hir::OwnerId, $V> };
131-
([][$K:ty, $V:ty]) => { DefaultCache<$K, $V> };
132-
([(arena_cache) $($rest:tt)*][$K:ty, $V:ty]) => { ArenaCache<'tcx, $K, $V> };
133-
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => { query_storage!([$($modifiers)*][$($args)*]) };
124+
([][$K:ty, $V:ty]) => {
125+
<<$K as Key>::CacheSelector as CacheSelector<'tcx, $V>>::Cache
126+
};
127+
([(arena_cache) $($rest:tt)*][$K:ty, $V:ty]) => {
128+
<<$K as Key>::CacheSelector as CacheSelector<'tcx, $V>>::ArenaCache
129+
};
130+
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
131+
query_storage!([$($modifiers)*][$($args)*])
132+
};
134133
}
135134

136135
macro_rules! separate_provide_extern_decl {

compiler/rustc_query_system/src/query/caches.rs

+26
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ use rustc_index::vec::{Idx, IndexVec};
1212
use std::default::Default;
1313
use std::fmt::Debug;
1414
use std::hash::Hash;
15+
use std::marker::PhantomData;
16+
17+
pub trait CacheSelector<'tcx, V> {
18+
type Cache
19+
where
20+
V: Clone;
21+
type ArenaCache;
22+
}
1523

1624
pub trait QueryStorage {
1725
type Value: Debug;
@@ -43,6 +51,15 @@ pub trait QueryCache: QueryStorage + Sized {
4351
fn iter(&self, f: &mut dyn FnMut(&Self::Key, &Self::Value, DepNodeIndex));
4452
}
4553

54+
pub struct DefaultCacheSelector<K>(PhantomData<K>);
55+
56+
impl<'tcx, K: Eq + Hash, V: 'tcx> CacheSelector<'tcx, V> for DefaultCacheSelector<K> {
57+
type Cache = DefaultCache<K, V>
58+
where
59+
V: Clone;
60+
type ArenaCache = ArenaCache<'tcx, K, V>;
61+
}
62+
4663
pub struct DefaultCache<K, V> {
4764
#[cfg(parallel_compiler)]
4865
cache: Sharded<FxHashMap<K, (V, DepNodeIndex)>>,
@@ -209,6 +226,15 @@ where
209226
}
210227
}
211228

229+
pub struct VecCacheSelector<K>(PhantomData<K>);
230+
231+
impl<'tcx, K: Idx, V: 'tcx> CacheSelector<'tcx, V> for VecCacheSelector<K> {
232+
type Cache = VecCache<K, V>
233+
where
234+
V: Clone;
235+
type ArenaCache = VecArenaCache<'tcx, K, V>;
236+
}
237+
212238
pub struct VecCache<K: Idx, V> {
213239
#[cfg(parallel_compiler)]
214240
cache: Sharded<IndexVec<K, Option<(V, DepNodeIndex)>>>,

compiler/rustc_query_system/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use self::job::{print_query_stack, QueryInfo, QueryJob, QueryJobId, QueryJob
88

99
mod caches;
1010
pub use self::caches::{
11-
ArenaCache, DefaultCache, QueryCache, QueryStorage, VecArenaCache, VecCache,
11+
CacheSelector, DefaultCacheSelector, QueryCache, QueryStorage, VecCacheSelector,
1212
};
1313

1414
mod config;

0 commit comments

Comments
 (0)