Skip to content

Commit 33e27c7

Browse files
committed
Streamline cache-related query functions.
There are three query vtable functions related to the `cache_on_disk_if` modifier: - `will_cache_on_disk_for_key_fn` - `try_load_from_disk_fn` - `is_loadable_from_disk_fn` These are all function ptrs within an `Option`. They each have a wrapper that returns `false`/`None` if the function ptr is missing. This commit removes the `Option` wrappers. In the `None` case we now set the function ptr to a trivial closure that returns `false`/`None`. The commit also removes some typedefs that each have a single use. All this is a bit simpler, with less indirection. More importantly, it enables the following commit, which will make more related changes.
1 parent b3bbeb4 commit 33e27c7

2 files changed

Lines changed: 22 additions & 29 deletions

File tree

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ pub enum CycleErrorHandling {
6363
Stash,
6464
}
6565

66-
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
67-
68-
pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
69-
tcx: TyCtxt<'tcx>,
70-
key: &Key,
71-
prev_index: SerializedDepNodeIndex,
72-
index: DepNodeIndex,
73-
) -> Option<Value>;
74-
75-
pub type IsLoadableFromDiskFn<'tcx, Key> =
76-
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
77-
7866
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
7967

8068
#[derive(Clone, Debug)]
@@ -129,7 +117,7 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
129117
pub cycle_error_handling: CycleErrorHandling,
130118
pub state: QueryState<'tcx, C::Key>,
131119
pub cache: C,
132-
pub will_cache_on_disk_for_key_fn: Option<WillCacheOnDiskForKeyFn<'tcx, C::Key>>,
120+
pub will_cache_on_disk_for_key_fn: fn(tcx: TyCtxt<'tcx>, key: &C::Key) -> bool,
133121

134122
/// Function pointer that calls `tcx.$query(key)` for this query and
135123
/// discards the returned value.
@@ -145,8 +133,14 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
145133
/// This should be the only code that calls the provider function.
146134
pub invoke_provider_fn: fn(tcx: TyCtxt<'tcx>, key: C::Key) -> C::Value,
147135

148-
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
149-
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
136+
pub try_load_from_disk_fn: fn(
137+
tcx: TyCtxt<'tcx>,
138+
key: &C::Key,
139+
prev_index: SerializedDepNodeIndex,
140+
index: DepNodeIndex,
141+
) -> Option<C::Value>,
142+
pub is_loadable_from_disk_fn:
143+
fn(tcx: TyCtxt<'tcx>, key: &C::Key, index: SerializedDepNodeIndex) -> bool,
150144
pub hash_result: HashResult<C::Value>,
151145
pub value_from_cycle_error:
152146
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
@@ -176,7 +170,7 @@ impl<'tcx, C: QueryCache> fmt::Debug for QueryVTable<'tcx, C> {
176170
impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
177171
#[inline(always)]
178172
pub fn will_cache_on_disk_for_key(&self, tcx: TyCtxt<'tcx>, key: &C::Key) -> bool {
179-
self.will_cache_on_disk_for_key_fn.map_or(false, |f| f(tcx, key))
173+
(self.will_cache_on_disk_for_key_fn)(tcx, key)
180174
}
181175

182176
#[inline(always)]
@@ -187,8 +181,7 @@ impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
187181
prev_index: SerializedDepNodeIndex,
188182
index: DepNodeIndex,
189183
) -> Option<C::Value> {
190-
// `?` will return None immediately for queries that never cache to disk.
191-
self.try_load_from_disk_fn?(tcx, key, prev_index, index)
184+
(self.try_load_from_disk_fn)(tcx, key, prev_index, index)
192185
}
193186

194187
#[inline]
@@ -198,7 +191,7 @@ impl<'tcx, C: QueryCache> QueryVTable<'tcx, C> {
198191
key: &C::Key,
199192
index: SerializedDepNodeIndex,
200193
) -> bool {
201-
self.is_loadable_from_disk_fn.map_or(false, |f| f(tcx, key, index))
194+
(self.is_loadable_from_disk_fn)(tcx, key, index)
202195
}
203196

204197
/// Synthesize an error value to let compilation continue after a cycle.

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -569,9 +569,9 @@ macro_rules! define_queries {
569569
state: Default::default(),
570570
cache: Default::default(),
571571
will_cache_on_disk_for_key_fn: if_cache_on_disk!([$($modifiers)*] {
572-
Some(::rustc_middle::queries::_cache_on_disk_if_fns::$name)
572+
rustc_middle::queries::_cache_on_disk_if_fns::$name
573573
} {
574-
None
574+
|_tcx, _key| false
575575
}),
576576
call_query_method_fn: |tcx, key| {
577577
// Call the query method for its side-effect of loading a value
@@ -580,9 +580,9 @@ macro_rules! define_queries {
580580
},
581581
invoke_provider_fn: self::invoke_provider_fn::__rust_begin_short_backtrace,
582582
try_load_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
583-
Some(|tcx, key, prev_index, index| {
583+
|tcx, key, prev_index, index| {
584584
// Check the `cache_on_disk_if` condition for this key.
585-
if !::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) {
585+
if !::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, &key) {
586586
return None;
587587
}
588588

@@ -591,17 +591,17 @@ macro_rules! define_queries {
591591

592592
// Arena-alloc the value if appropriate, and erase it.
593593
Some(queries::$name::provided_to_erased(tcx, value))
594-
})
594+
}
595595
} {
596-
None
596+
|_tcx, _key, _prev_index, _index| None
597597
}),
598598
is_loadable_from_disk_fn: if_cache_on_disk!([$($modifiers)*] {
599-
Some(|tcx, key, index| -> bool {
600-
::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, key) &&
599+
|tcx, key, index| -> bool {
600+
::rustc_middle::queries::_cache_on_disk_if_fns::$name(tcx, &key) &&
601601
$crate::plumbing::loadable_from_disk(tcx, index)
602-
})
602+
}
603603
} {
604-
None
604+
|_tcx, _key, _index| false
605605
}),
606606
value_from_cycle_error: |tcx, cycle, guar| {
607607
let result: queries::$name::Value<'tcx> =

0 commit comments

Comments
 (0)