Skip to content

Commit aa17145

Browse files
committed
Remove will_cache_on_disk_for_key_fn and AsLocalQueryKey
1 parent 3ebe60c commit aa17145

6 files changed

Lines changed: 33 additions & 52 deletions

File tree

compiler/rustc_middle/src/query/keys.rs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub trait QueryKey: Sized + QueryKeyBounds {
3636
/// [`QueryCache`]: rustc_middle::query::QueryCache
3737
type Cache<V> = DefaultCache<Self, V>;
3838

39+
type LocalQueryKey = !;
40+
3941
/// In the event that a cycle occurs, if no explicit span has been
4042
/// given for a query with key `self`, what span should we use?
4143
fn default_span(&self, tcx: TyCtxt<'_>) -> Span;
@@ -45,14 +47,12 @@ pub trait QueryKey: Sized + QueryKeyBounds {
4547
fn key_as_def_id(&self) -> Option<DefId> {
4648
None
4749
}
48-
}
49-
50-
pub trait AsLocalQueryKey: QueryKey {
51-
type LocalQueryKey;
5250

5351
/// Given an instance of this key, what crate is it referring to?
5452
/// This is used to find the provider.
55-
fn as_local_key(&self) -> Option<Self::LocalQueryKey>;
53+
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
54+
None
55+
}
5656
}
5757

5858
impl QueryKey for () {
@@ -96,13 +96,11 @@ impl<'tcx> QueryKey for ty::LitToConstInput<'tcx> {
9696
impl QueryKey for CrateNum {
9797
type Cache<V> = VecCache<Self, V, DepNodeIndex>;
9898

99+
type LocalQueryKey = LocalCrate;
100+
99101
fn default_span(&self, _: TyCtxt<'_>) -> Span {
100102
DUMMY_SP
101103
}
102-
}
103-
104-
impl AsLocalQueryKey for CrateNum {
105-
type LocalQueryKey = LocalCrate;
106104

107105
#[inline(always)]
108106
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -136,6 +134,7 @@ impl QueryKey for LocalDefId {
136134

137135
impl QueryKey for DefId {
138136
type Cache<V> = DefIdCache<V>;
137+
type LocalQueryKey = LocalDefId;
139138

140139
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
141140
tcx.def_span(*self)
@@ -145,10 +144,6 @@ impl QueryKey for DefId {
145144
fn key_as_def_id(&self) -> Option<DefId> {
146145
Some(*self)
147146
}
148-
}
149-
150-
impl AsLocalQueryKey for DefId {
151-
type LocalQueryKey = LocalDefId;
152147

153148
#[inline(always)]
154149
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -197,13 +192,11 @@ impl QueryKey for (LocalDefId, LocalDefId, Ident) {
197192
}
198193

199194
impl QueryKey for (CrateNum, DefId) {
195+
type LocalQueryKey = DefId;
196+
200197
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
201198
self.1.default_span(tcx)
202199
}
203-
}
204-
205-
impl AsLocalQueryKey for (CrateNum, DefId) {
206-
type LocalQueryKey = DefId;
207200

208201
#[inline(always)]
209202
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {
@@ -212,13 +205,11 @@ impl AsLocalQueryKey for (CrateNum, DefId) {
212205
}
213206

214207
impl QueryKey for (CrateNum, SimplifiedType) {
208+
type LocalQueryKey = SimplifiedType;
209+
215210
fn default_span(&self, _: TyCtxt<'_>) -> Span {
216211
DUMMY_SP
217212
}
218-
}
219-
220-
impl AsLocalQueryKey for (CrateNum, SimplifiedType) {
221-
type LocalQueryKey = SimplifiedType;
222213

223214
#[inline(always)]
224215
fn as_local_key(&self) -> Option<Self::LocalQueryKey> {

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::LocalDefId;
33
pub use self::caches::{DefIdCache, DefaultCache, QueryCache, SingleCache, VecCache};
44
pub use self::into_query_key::IntoQueryKey;
55
pub use self::job::{QueryJob, QueryJobId, QueryLatch, QueryWaiter};
6-
pub use self::keys::{AsLocalQueryKey, LocalCrate, QueryKey};
6+
pub use self::keys::{LocalCrate, QueryKey};
77
pub use self::plumbing::{
88
ActiveKeyStatus, Cycle, EnsureMode, QueryMode, QueryState, QuerySystem, QueryVTable, TyCtxtAt,
99
TyCtxtEnsureDone, TyCtxtEnsureOk, TyCtxtEnsureResult,

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::dep_graph::{DepKind, DepNodeIndex, QuerySideEffect, SerializedDepNode
1414
use crate::ich::StableHashingContext;
1515
use crate::queries::{ExternProviders, Providers, QueryArenas, QueryVTables, TaggedQueryKey};
1616
use crate::query::on_disk_cache::OnDiskCache;
17-
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryStackFrame};
17+
use crate::query::{IntoQueryKey, QueryCache, QueryJob, QueryKey, QueryStackFrame};
1818
use crate::ty::{self, TyCtxt};
1919

2020
/// For a particular query, keeps track of "active" keys, i.e. keys whose
@@ -86,6 +86,9 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
8686
/// True if this query has the `feedable` modifier.
8787
pub feedable: bool,
8888

89+
pub cache_on_disk_local: bool,
90+
pub separate_provide_extern: bool,
91+
8992
pub dep_kind: DepKind,
9093
pub state: QueryState<'tcx, C::Key>,
9194
pub cache: C,
@@ -97,8 +100,6 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
97100
/// This should be the only code that calls the provider function.
98101
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
99102

100-
pub will_cache_on_disk_for_key_fn: fn(key: C::Key) -> bool,
101-
102103
pub try_load_from_disk_fn: fn(
103104
tcx: TyCtxt<'tcx>,
104105
key: C::Key,
@@ -134,6 +135,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
134135
pub execute_query_fn: fn(TyCtxt<'tcx>, Span, C::Key, QueryMode) -> Option<C::Value>,
135136
}
136137

138+
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
139+
pub fn cache_on_disk(&self, key: C::Key) -> bool {
140+
self.cache_on_disk_local && (!self.separate_provide_extern || key.as_local_key().is_some())
141+
}
142+
}
143+
137144
impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
138145
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139146
// When debug-printing a query vtable (e.g. for ICE or tracing),
@@ -325,7 +332,7 @@ macro_rules! define_callbacks {
325332
/// This query has the `separate_provide_extern` modifier.
326333
#[cfg($separate_provide_extern)]
327334
pub type LocalKey<'tcx> =
328-
<Key<'tcx> as $crate::query::AsLocalQueryKey>::LocalQueryKey;
335+
<Key<'tcx> as $crate::query::QueryKey>::LocalQueryKey;
329336
/// Key type used by provider functions in `local_providers`.
330337
#[cfg(not($separate_provide_extern))]
331338
pub type LocalKey<'tcx> = Key<'tcx>;

compiler/rustc_query_impl/src/execution.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,7 @@ fn ensure_can_skip_execution<'tcx, C: QueryCache>(
576576
// needed, which guarantees the query provider will never run
577577
// for this key.
578578
EnsureMode::Done => {
579-
(query.will_cache_on_disk_for_key_fn)(key)
580-
&& loadable_from_disk(tcx, serialized_dep_node_index)
579+
query.cache_on_disk(key) && loadable_from_disk(tcx, serialized_dep_node_index)
581580
}
582581
}
583582
}

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn encode_query_values_inner<'a, 'tcx, C, V>(
9393

9494
assert!(all_inactive(&query.state));
9595
query.cache.for_each(&mut |key, value, dep_node| {
96-
if (query.will_cache_on_disk_for_key_fn)(*key) {
96+
if query.cache_on_disk(*key) {
9797
encoder.encode_query_value::<V>(dep_node, &erase::restore_val::<V>(*value));
9898
}
9999
});
@@ -152,7 +152,7 @@ pub(crate) fn promote_from_disk_inner<'tcx, C: QueryCache>(
152152

153153
// If the recovered key isn't eligible for cache-on-disk, then there's no
154154
// value on disk to promote.
155-
if !(query.will_cache_on_disk_for_key_fn)(key) {
155+
if !query.cache_on_disk(key) {
156156
return;
157157
}
158158

compiler/rustc_query_impl/src/query_impl.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_middle::queries::TaggedQueryKey;
22
use rustc_middle::query::erase::{self, Erased};
3-
use rustc_middle::query::{AsLocalQueryKey, QueryMode, QueryVTable};
3+
use rustc_middle::query::{QueryKey, QueryMode, QueryVTable};
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66

@@ -125,20 +125,6 @@ macro_rules! define_queries {
125125
}
126126
}
127127

128-
fn will_cache_on_disk_for_key<'tcx>(
129-
_key: rustc_middle::queries::$name::Key<'tcx>,
130-
) -> bool {
131-
cfg_select! {
132-
// If a query has both `cache_on_disk` and `separate_provide_extern`, only
133-
// disk-cache values for "local" keys, i.e. things in the current crate.
134-
all($cache_on_disk, $separate_provide_extern) => {
135-
AsLocalQueryKey::as_local_key(&_key).is_some()
136-
}
137-
all($cache_on_disk, not($separate_provide_extern)) => true,
138-
not($cache_on_disk) => false,
139-
}
140-
}
141-
142128
pub(crate) fn make_query_vtable<'tcx>(incremental: bool)
143129
-> QueryVTable<'tcx, rustc_middle::queries::$name::Cache<'tcx>>
144130
{
@@ -152,20 +138,18 @@ macro_rules! define_queries {
152138
dep_kind: rustc_middle::dep_graph::DepKind::$name,
153139
state: Default::default(),
154140
cache: Default::default(),
141+
cache_on_disk_local: $cache_on_disk,
142+
separate_provide_extern: $separate_provide_extern,
155143

156144
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
157145

158-
will_cache_on_disk_for_key_fn:
159-
$crate::query_impl::$name::will_cache_on_disk_for_key,
160-
161146
#[cfg($cache_on_disk)]
162-
try_load_from_disk_fn: |tcx, key, prev_index, index| {
147+
try_load_from_disk_fn: |tcx, _key, prev_index, index| {
163148
use rustc_middle::queries::$name::{ProvidedValue, provided_to_erased};
164149

165150
// Check the cache-on-disk condition for this key.
166-
if !$crate::query_impl::$name::will_cache_on_disk_for_key(key) {
167-
return None;
168-
}
151+
#[cfg($separate_provide_extern)]
152+
QueryKey::as_local_key(&_key)?;
169153

170154
let loaded_value: ProvidedValue<'tcx> =
171155
$crate::plumbing::try_load_from_disk(tcx, prev_index, index)?;

0 commit comments

Comments
 (0)