Skip to content

Commit 83b6dc9

Browse files
committed
Move almost all of the function in query_callbacks to a generic function
1 parent 8f442e8 commit 83b6dc9

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

compiler/rustc_query_impl/src/plumbing.rs

+47-28
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ use crate::{on_disk_cache, Queries};
77
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
88
use rustc_data_structures::sync::Lock;
99
use rustc_errors::{Diagnostic, Handler};
10-
use rustc_middle::dep_graph::{self, DepKind, DepNode, DepNodeIndex, SerializedDepNodeIndex};
10+
use rustc_middle::dep_graph::{
11+
self, DepKind, DepKindStruct, DepNode, DepNodeIndex, SerializedDepNodeIndex,
12+
};
1113
use rustc_middle::ty::tls::{self, ImplicitCtxt};
1214
use rustc_middle::ty::{self, TyCtxt};
1315
use rustc_query_system::dep_graph::{DepNodeParams, HasDepContext};
1416
use rustc_query_system::ich::StableHashingContext;
1517
use rustc_query_system::query::{
16-
force_query, QueryContext, QueryDescription, QueryJobId, QueryMap, QuerySideEffects,
17-
QueryStackFrame,
18+
force_query, QueryConfig, QueryContext, QueryDescription, QueryJobId, QueryMap,
19+
QuerySideEffects, QueryStackFrame,
1820
};
1921
use std::any::Any;
2022
use std::num::NonZeroU64;
@@ -303,19 +305,19 @@ pub(crate) fn try_load_from_on_disk_cache<'tcx, K: DepNodeParams<TyCtxt<'tcx>>,
303305
tcx: TyCtxt<'tcx>,
304306
dep_node: DepNode,
305307
cache_on_disk: fn(TyCtxt<'tcx>, &K) -> bool,
306-
do_query: fn(TyCtxt<'tcx>, K) -> V,
308+
cache_query_deps: fn(TyCtxt<'tcx>, K) -> V,
307309
) {
308310
debug_assert!(tcx.dep_graph.is_green(&dep_node));
309311

310312
let key = K::recover(tcx, &dep_node).unwrap_or_else(|| {
311313
panic!("Failed to recover key for {:?} with hash {}", dep_node, dep_node.hash)
312314
});
313315
if cache_on_disk(tcx, &key) {
314-
let _ = do_query(tcx, key);
316+
let _ = cache_query_deps(tcx, key);
315317
}
316318
}
317319

318-
fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
320+
pub(crate) fn force_from_dep_node<'tcx, Q>(tcx: TyCtxt<'tcx>, dep_node: DepNode) -> bool
319321
where
320322
Q: QueryDescription<QueryCtxt<'tcx>>,
321323
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
@@ -331,6 +333,39 @@ where
331333
}
332334
}
333335

336+
pub(crate) fn query_callback<'tcx, Q: QueryConfig>(
337+
// NOTE: we can't remove these function pointers, because `recover` is invariant -> `try_load_from_on_disk_cache` takes a concrete lifetime, not a universal lifetime.
338+
// Instead, we infer the correct lifetime at the callsite, so we can pass in a HRTB function pointer to the DepKindStruct.
339+
try_load_from_on_disk_cache: fn(TyCtxt<'_>, DepNode),
340+
force_from_dep_node: fn(TyCtxt<'_>, DepNode) -> bool,
341+
is_anon: bool,
342+
is_eval_always: bool,
343+
) -> DepKindStruct
344+
where
345+
Q: QueryDescription<QueryCtxt<'tcx>>,
346+
Q::Key: DepNodeParams<TyCtxt<'tcx>>,
347+
{
348+
let fingerprint_style = Q::Key::fingerprint_style();
349+
350+
if is_anon || !fingerprint_style.reconstructible() {
351+
return DepKindStruct {
352+
is_anon,
353+
is_eval_always,
354+
fingerprint_style,
355+
force_from_dep_node: None,
356+
try_load_from_on_disk_cache: None,
357+
};
358+
}
359+
360+
DepKindStruct {
361+
is_anon,
362+
is_eval_always,
363+
fingerprint_style,
364+
force_from_dep_node: Some(force_from_dep_node),
365+
try_load_from_on_disk_cache: Some(try_load_from_on_disk_cache),
366+
}
367+
}
368+
334369
// NOTE: `$V` isn't used here, but we still need to match on it so it can be passed to other macros
335370
// invoked by `rustc_query_append`.
336371
macro_rules! define_queries {
@@ -399,7 +434,6 @@ macro_rules! define_queries {
399434
#[allow(nonstandard_style)]
400435
mod query_callbacks {
401436
use super::*;
402-
use rustc_query_system::dep_graph::DepNodeParams;
403437
use rustc_query_system::query::QueryDescription;
404438
use rustc_query_system::dep_graph::FingerprintStyle;
405439

@@ -458,29 +492,14 @@ macro_rules! define_queries {
458492
$(pub(crate) fn $name()-> DepKindStruct {
459493
let is_anon = is_anon!([$($modifiers)*]);
460494
let is_eval_always = is_eval_always!([$($modifiers)*]);
495+
type Q<'tcx> = queries::$name<'tcx>;
461496

462-
let fingerprint_style =
463-
<<queries::$name<'_> as QueryConfig>::Key as DepNodeParams<TyCtxt<'_>>>::fingerprint_style();
464-
465-
if is_anon || !fingerprint_style.reconstructible() {
466-
return DepKindStruct {
467-
is_anon,
468-
is_eval_always,
469-
fingerprint_style,
470-
force_from_dep_node: None,
471-
try_load_from_on_disk_cache: None,
472-
}
473-
}
474-
475-
DepKindStruct {
497+
$crate::plumbing::query_callback::<Q<'_>>(
498+
|tcx, key| $crate::plumbing::try_load_from_on_disk_cache::<<Q<'_> as QueryConfig>::Key, _>(tcx, key, <Q<'_>>::cache_on_disk, TyCtxt::$name),
499+
|tcx, key| $crate::plumbing::force_from_dep_node::<Q<'_>>(tcx, key),
476500
is_anon,
477-
is_eval_always,
478-
fingerprint_style,
479-
force_from_dep_node: Some(|tcx, dep_node| $crate::plumbing::force_from_dep_node::<queries::$name<'_>>(tcx, dep_node)),
480-
try_load_from_on_disk_cache: Some(|tcx, key| $crate::plumbing::try_load_from_on_disk_cache::<
481-
<queries::$name<'_> as QueryConfig>::Key, _
482-
>(tcx, key, queries::$name::cache_on_disk, TyCtxt::$name)),
483-
}
501+
is_eval_always
502+
)
484503
})*
485504
}
486505

0 commit comments

Comments
 (0)