Skip to content

Commit 2c057bb

Browse files
committed
Clean up QueryVTable::hash_result into hash_value_fn
This commit: - Renames the query vtable field `hash_result` to `hash_value_fn` - Removes the unhelpful `HashResult` type alias, which was hiding an important layer of `Option` - Replaces the cryptic `hash_result!` helper macro with a more straightforward `if_no_hash!` helper, in line with other modifier-checking macros - Renames a few identifiers to refer to a query's return value as `value`
1 parent 38c0de8 commit 2c057bb

4 files changed

Lines changed: 42 additions & 36 deletions

File tree

compiler/rustc_middle/src/query/inner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ pub(crate) fn query_feed<'tcx, C>(
116116
// The query already has a cached value for this key.
117117
// That's OK if both values are the same, i.e. they have the same hash,
118118
// so now we check their hashes.
119-
if let Some(hasher_fn) = query_vtable.hash_result {
119+
if let Some(hash_value_fn) = query_vtable.hash_value_fn {
120120
let (old_hash, value_hash) = tcx.with_stable_hashing_context(|ref mut hcx| {
121-
(hasher_fn(hcx, &old), hasher_fn(hcx, &value))
121+
(hash_value_fn(hcx, &old), hash_value_fn(hcx, &value))
122122
});
123123
if old_hash != value_hash {
124124
// We have an inconsistency. This can happen if one of the two
@@ -151,7 +151,7 @@ pub(crate) fn query_feed<'tcx, C>(
151151
dep_node,
152152
tcx,
153153
&value,
154-
query_vtable.hash_result,
154+
query_vtable.hash_value_fn,
155155
query_vtable.format_value,
156156
);
157157
query_vtable.cache.complete(key, value, dep_node_index);

compiler/rustc_middle/src/query/plumbing.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ pub type TryLoadFromDiskFn<'tcx, Key, Value> = fn(
7474
pub type IsLoadableFromDiskFn<'tcx, Key> =
7575
fn(tcx: TyCtxt<'tcx>, key: &Key, index: SerializedDepNodeIndex) -> bool;
7676

77-
pub type HashResult<V> = Option<fn(&mut StableHashingContext<'_>, &V) -> Fingerprint>;
78-
7977
#[derive(Clone, Debug)]
8078
pub struct CycleError<I = QueryStackFrameExtra> {
8179
/// The query and related span that uses the cycle.
@@ -146,7 +144,12 @@ pub struct QueryVTable<'tcx, C: QueryCache> {
146144

147145
pub try_load_from_disk_fn: Option<TryLoadFromDiskFn<'tcx, C::Key, C::Value>>,
148146
pub is_loadable_from_disk_fn: Option<IsLoadableFromDiskFn<'tcx, C::Key>>,
149-
pub hash_result: HashResult<C::Value>,
147+
148+
/// Function pointer that hashes this query's result values.
149+
///
150+
/// For `no_hash` queries, this function pointer is None.
151+
pub hash_value_fn: Option<fn(&mut StableHashingContext<'_>, &C::Value) -> Fingerprint>,
152+
150153
pub value_from_cycle_error:
151154
fn(tcx: TyCtxt<'tcx>, cycle_error: &CycleError, guar: ErrorGuaranteed) -> C::Value,
152155
pub format_value: fn(&C::Value) -> String,

compiler/rustc_query_impl/src/execution.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
354354
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
355355

356356
// Delegate to another function to actually execute the query job.
357-
let (result, dep_node_index) = if INCR {
357+
let (value, dep_node_index) = if INCR {
358358
execute_job_incr(query, tcx, key, dep_node, id)
359359
} else {
360360
execute_job_non_incr(query, tcx, key, id)
@@ -366,18 +366,18 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
366366
// This can't happen, as query feeding adds the very dependencies to the fed query
367367
// as its feeding query had. So if the fed query is red, so is its feeder, which will
368368
// get evaluated first, and re-feed the query.
369-
if let Some((cached_result, _)) = cache.lookup(&key) {
370-
let Some(hasher) = query.hash_result else {
369+
if let Some((cached_value, _)) = cache.lookup(&key) {
370+
let Some(hash_value_fn) = query.hash_value_fn else {
371371
panic!(
372372
"no_hash fed query later has its value computed.\n\
373373
Remove `no_hash` modifier to allow recomputation.\n\
374374
The already cached value: {}",
375-
(query.format_value)(&cached_result)
375+
(query.format_value)(&cached_value)
376376
);
377377
};
378378

379379
let (old_hash, new_hash) = tcx.with_stable_hashing_context(|mut hcx| {
380-
(hasher(&mut hcx, &cached_result), hasher(&mut hcx, &result))
380+
(hash_value_fn(&mut hcx, &cached_value), hash_value_fn(&mut hcx, &value))
381381
});
382382
let formatter = query.format_value;
383383
if old_hash != new_hash {
@@ -389,17 +389,17 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
389389
computed={:#?}\nfed={:#?}",
390390
query.dep_kind,
391391
key,
392-
formatter(&result),
393-
formatter(&cached_result),
392+
formatter(&value),
393+
formatter(&cached_value),
394394
);
395395
}
396396
}
397397
}
398398

399399
// Tell the guard to perform completion bookkeeping, and also to not poison the query.
400-
job_guard.complete(cache, result, dep_node_index);
400+
job_guard.complete(cache, value, dep_node_index);
401401

402-
(result, Some(dep_node_index))
402+
(value, Some(dep_node_index))
403403
}
404404

405405
// Fast path for when incr. comp. is off.
@@ -420,22 +420,22 @@ fn execute_job_non_incr<'tcx, C: QueryCache>(
420420

421421
let prof_timer = tcx.prof.query_provider();
422422
// Call the query provider.
423-
let result =
423+
let value =
424424
start_query(tcx, job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
425425
let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
426426
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
427427

428428
// Similarly, fingerprint the result to assert that
429429
// it doesn't have anything not considered hashable.
430430
if cfg!(debug_assertions)
431-
&& let Some(hash_result) = query.hash_result
431+
&& let Some(hash_value_fn) = query.hash_value_fn
432432
{
433433
tcx.with_stable_hashing_context(|mut hcx| {
434-
hash_result(&mut hcx, &result);
434+
hash_value_fn(&mut hcx, &value);
435435
});
436436
}
437437

438-
(result, dep_node_index)
438+
(value, dep_node_index)
439439
}
440440

441441
#[inline(always)]
@@ -491,7 +491,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(
491491
tcx,
492492
(query, key),
493493
|tcx, (query, key)| (query.invoke_provider_fn)(tcx, key),
494-
query.hash_result,
494+
query.hash_value_fn,
495495
)
496496
});
497497

@@ -542,7 +542,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
542542
dep_graph_data,
543543
&value,
544544
prev_index,
545-
query.hash_result,
545+
query.hash_value_fn,
546546
query.format_value,
547547
);
548548
}
@@ -589,7 +589,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
589589
dep_graph_data,
590590
&value,
591591
prev_index,
592-
query.hash_result,
592+
query.hash_value_fn,
593593
query.format_value,
594594
);
595595

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,13 @@ macro_rules! is_feedable {
210210
};
211211
}
212212

213-
macro_rules! hash_result {
214-
([][$V:ty]) => {{
215-
Some(|hcx, result| {
216-
let result = rustc_middle::query::erase::restore_val::<$V>(*result);
217-
rustc_middle::dep_graph::hash_result(hcx, &result)
218-
})
219-
}};
220-
([(no_hash) $($rest:tt)*][$V:ty]) => {{
221-
None
222-
}};
223-
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
224-
hash_result!([$($modifiers)*][$($args)*])
225-
};
213+
/// Expands to `$yes` if the `no_hash` modifier is present, or `$no` otherwise.
214+
macro_rules! if_no_hash {
215+
([] $yes:tt $no:tt) => { $no };
216+
([(no_hash) $($modifiers:tt)*] $yes:tt $no:tt) => { $yes };
217+
([$other:tt $($modifiers:tt)*] $yes:tt $no:tt) => {
218+
if_no_hash!([$($modifiers)*] $yes $no)
219+
}
226220
}
227221

228222
macro_rules! call_provider {
@@ -606,7 +600,16 @@ macro_rules! define_queries {
606600
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
607601
erase::erase_val(result)
608602
},
609-
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
603+
hash_value_fn: if_no_hash!(
604+
[$($modifiers)*]
605+
None
606+
{
607+
Some(|hcx, erased_value: &erase::Erased<queries::$name::Value<'tcx>>| {
608+
let value = erase::restore_val(*erased_value);
609+
rustc_middle::dep_graph::hash_result(hcx, &value)
610+
})
611+
}
612+
),
610613
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
611614
description_fn: $crate::queries::_description_fns::$name,
612615
execute_query_fn: if incremental {

0 commit comments

Comments
 (0)