Skip to content

Commit 27503c4

Browse files
authored
Rollup merge of #153209 - Zalathar:hash-value-fn, r=nnethercote
Clean up `QueryVTable::hash_result` into `hash_value_fn` This PR: - 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` There should be no change to compiler behaviour. r? nnethercote (or compiler)
2 parents fbbb35d + 2c057bb commit 27503c4

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
@@ -372,7 +372,7 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
372372
debug_assert_eq!(tcx.dep_graph.is_fully_enabled(), INCR);
373373

374374
// Delegate to another function to actually execute the query job.
375-
let (result, dep_node_index) = if INCR {
375+
let (value, dep_node_index) = if INCR {
376376
execute_job_incr(query, tcx, key, dep_node, id)
377377
} else {
378378
execute_job_non_incr(query, tcx, key, id)
@@ -384,18 +384,18 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
384384
// This can't happen, as query feeding adds the very dependencies to the fed query
385385
// as its feeding query had. So if the fed query is red, so is its feeder, which will
386386
// get evaluated first, and re-feed the query.
387-
if let Some((cached_result, _)) = cache.lookup(&key) {
388-
let Some(hasher) = query.hash_result else {
387+
if let Some((cached_value, _)) = cache.lookup(&key) {
388+
let Some(hash_value_fn) = query.hash_value_fn else {
389389
panic!(
390390
"no_hash fed query later has its value computed.\n\
391391
Remove `no_hash` modifier to allow recomputation.\n\
392392
The already cached value: {}",
393-
(query.format_value)(&cached_result)
393+
(query.format_value)(&cached_value)
394394
);
395395
};
396396

397397
let (old_hash, new_hash) = tcx.with_stable_hashing_context(|mut hcx| {
398-
(hasher(&mut hcx, &cached_result), hasher(&mut hcx, &result))
398+
(hash_value_fn(&mut hcx, &cached_value), hash_value_fn(&mut hcx, &value))
399399
});
400400
let formatter = query.format_value;
401401
if old_hash != new_hash {
@@ -407,17 +407,17 @@ fn execute_job<'tcx, C: QueryCache, const INCR: bool>(
407407
computed={:#?}\nfed={:#?}",
408408
query.dep_kind,
409409
key,
410-
formatter(&result),
411-
formatter(&cached_result),
410+
formatter(&value),
411+
formatter(&cached_value),
412412
);
413413
}
414414
}
415415
}
416416

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

420-
(result, Some(dep_node_index))
420+
(value, Some(dep_node_index))
421421
}
422422

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

439439
let prof_timer = tcx.prof.query_provider();
440440
// Call the query provider.
441-
let result =
441+
let value =
442442
start_query(tcx, job_id, query.depth_limit, || (query.invoke_provider_fn)(tcx, key));
443443
let dep_node_index = tcx.dep_graph.next_virtual_depnode_index();
444444
prof_timer.finish_with_query_invocation_id(dep_node_index.into());
445445

446446
// Similarly, fingerprint the result to assert that
447447
// it doesn't have anything not considered hashable.
448448
if cfg!(debug_assertions)
449-
&& let Some(hash_result) = query.hash_result
449+
&& let Some(hash_value_fn) = query.hash_value_fn
450450
{
451451
tcx.with_stable_hashing_context(|mut hcx| {
452-
hash_result(&mut hcx, &result);
452+
hash_value_fn(&mut hcx, &value);
453453
});
454454
}
455455

456-
(result, dep_node_index)
456+
(value, dep_node_index)
457457
}
458458

459459
#[inline(always)]
@@ -509,7 +509,7 @@ fn execute_job_incr<'tcx, C: QueryCache>(
509509
tcx,
510510
(query, key),
511511
|tcx, (query, key)| (query.invoke_provider_fn)(tcx, key),
512-
query.hash_result,
512+
query.hash_value_fn,
513513
)
514514
});
515515

@@ -560,7 +560,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
560560
dep_graph_data,
561561
&value,
562562
prev_index,
563-
query.hash_result,
563+
query.hash_value_fn,
564564
query.format_value,
565565
);
566566
}
@@ -607,7 +607,7 @@ fn load_from_disk_or_invoke_provider_green<'tcx, C: QueryCache>(
607607
dep_graph_data,
608608
&value,
609609
prev_index,
610-
query.hash_result,
610+
query.hash_value_fn,
611611
query.format_value,
612612
);
613613

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,13 @@ macro_rules! is_feedable {
165165
};
166166
}
167167

168-
macro_rules! hash_result {
169-
([][$V:ty]) => {{
170-
Some(|hcx, result| {
171-
let result = rustc_middle::query::erase::restore_val::<$V>(*result);
172-
rustc_middle::dep_graph::hash_result(hcx, &result)
173-
})
174-
}};
175-
([(no_hash) $($rest:tt)*][$V:ty]) => {{
176-
None
177-
}};
178-
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
179-
hash_result!([$($modifiers)*][$($args)*])
180-
};
168+
/// Expands to `$yes` if the `no_hash` modifier is present, or `$no` otherwise.
169+
macro_rules! if_no_hash {
170+
([] $yes:tt $no:tt) => { $no };
171+
([(no_hash) $($modifiers:tt)*] $yes:tt $no:tt) => { $yes };
172+
([$other:tt $($modifiers:tt)*] $yes:tt $no:tt) => {
173+
if_no_hash!([$($modifiers)*] $yes $no)
174+
}
181175
}
182176

183177
macro_rules! call_provider {
@@ -560,7 +554,16 @@ macro_rules! define_queries {
560554
let result: queries::$name::Value<'tcx> = Value::from_cycle_error(tcx, cycle, guar);
561555
erase::erase_val(result)
562556
},
563-
hash_result: hash_result!([$($modifiers)*][queries::$name::Value<'tcx>]),
557+
hash_value_fn: if_no_hash!(
558+
[$($modifiers)*]
559+
None
560+
{
561+
Some(|hcx, erased_value: &erase::Erased<queries::$name::Value<'tcx>>| {
562+
let value = erase::restore_val(*erased_value);
563+
rustc_middle::dep_graph::hash_result(hcx, &value)
564+
})
565+
}
566+
),
564567
format_value: |value| format!("{:?}", erase::restore_val::<queries::$name::Value<'tcx>>(*value)),
565568
description_fn: $crate::queries::_description_fns::$name,
566569
execute_query_fn: if incremental {

0 commit comments

Comments
 (0)