Skip to content

Commit f16cfc0

Browse files
committed
Auto merge of #152841 - Zalathar:unerased, r=<try>
Streamline `QueryVTableUnerased` into `GetQueryVTable`
2 parents ef70767 + 73e18de commit f16cfc0

3 files changed

Lines changed: 33 additions & 48 deletions

File tree

compiler/rustc_query_impl/src/dep_kind_vtables.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, KeyFingerprintStyle};
33
use rustc_middle::query::QueryCache;
44

55
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};
6-
use crate::{QueryDispatcherUnerased, QueryFlags};
6+
use crate::{GetQueryVTable, QueryFlags};
77

88
/// [`DepKindVTable`] constructors for special dep kinds that aren't queries.
99
#[expect(non_snake_case, reason = "use non-snake case to avoid collision with query names")]
@@ -110,18 +110,17 @@ mod non_query {
110110

111111
/// Shared implementation of the [`DepKindVTable`] constructor for queries.
112112
/// Called from macro-generated code for each query.
113-
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache, const FLAGS: QueryFlags>(
113+
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, const FLAGS: QueryFlags>(
114114
is_eval_always: bool,
115115
) -> DepKindVTable<'tcx>
116116
where
117-
Q: QueryDispatcherUnerased<'tcx, Cache, FLAGS>,
118-
Cache: QueryCache + 'tcx,
117+
Q: GetQueryVTable<'tcx, FLAGS>,
119118
{
120119
let is_anon = FLAGS.is_anon;
121120
let key_fingerprint_style = if is_anon {
122121
KeyFingerprintStyle::Opaque
123122
} else {
124-
<Cache::Key as DepNodeKey<'tcx>>::key_fingerprint_style()
123+
<Q::Cache as QueryCache>::Key::key_fingerprint_style()
125124
};
126125

127126
if is_anon || !key_fingerprint_style.reconstructible() {

compiler/rustc_query_impl/src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,22 @@ impl<'tcx, C: QueryCache, const FLAGS: QueryFlags> SemiDynamicQueryDispatcher<'t
217217
}
218218
}
219219

220-
/// Provides access to vtable-like operations for a query
221-
/// (by creating a [`SemiDynamicQueryDispatcher`]),
222-
/// but also keeps track of the "unerased" value type of the query
223-
/// (i.e. the actual result type in the query declaration).
220+
/// Trait that knows how to look up the [`QueryVTable`] for a particular query,
221+
/// and can combine it with static query flags to produce a
222+
/// [`SemiDynamicQueryDispatcher`].
224223
///
225224
/// This trait allows some per-query code to be defined in generic functions
226225
/// with a trait bound, instead of having to be defined inline within a macro
227226
/// expansion.
228227
///
229228
/// There is one macro-generated implementation of this trait for each query,
230-
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
231-
trait QueryDispatcherUnerased<'tcx, C: QueryCache, const FLAGS: QueryFlags> {
232-
type UnerasedValue;
229+
/// on the type `rustc_query_impl::query_impl::$name::VTableGetter`.
230+
trait GetQueryVTable<'tcx, const FLAGS: QueryFlags> {
231+
type Cache: QueryCache + 'tcx;
233232

234233
const NAME: &'static &'static str;
235234

236-
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, C, FLAGS>;
237-
238-
fn restore_val(value: C::Value) -> Self::UnerasedValue;
235+
fn query_dispatcher(tcx: TyCtxt<'tcx>) -> SemiDynamicQueryDispatcher<'tcx, Self::Cache, FLAGS>;
239236
}
240237

241238
pub fn query_system<'tcx>(

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ use rustc_middle::dep_graph::DepKindVTable;
1717
use rustc_middle::dep_graph::{
1818
self, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex, dep_kinds,
1919
};
20+
use rustc_middle::query::erase::{Erasable, Erased};
2021
use rustc_middle::query::on_disk_cache::{
2122
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
2223
};
2324
use rustc_middle::query::plumbing::QueryVTable;
2425
use rustc_middle::query::{
25-
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra,
26+
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra, erase,
2627
};
2728
use rustc_middle::ty::codec::TyEncoder;
2829
use rustc_middle::ty::print::with_reduced_queries;
@@ -34,7 +35,7 @@ use rustc_span::def_id::LOCAL_CRATE;
3435
use crate::error::{QueryOverflow, QueryOverflowNote};
3536
use crate::execution::{all_inactive, force_query};
3637
use crate::job::{QueryJobMap, find_dep_kind_root};
37-
use crate::{QueryDispatcherUnerased, QueryFlags, SemiDynamicQueryDispatcher};
38+
use crate::{GetQueryVTable, QueryFlags, SemiDynamicQueryDispatcher};
3839

3940
fn depth_limit_error<'tcx>(tcx: TyCtxt<'tcx>, job: QueryJobId) {
4041
let job_map =
@@ -335,15 +336,16 @@ where
335336
QueryStackFrame::new(info, kind, hash, def_id, def_id_for_ty_in_cycle)
336337
}
337338

338-
pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: QueryFlags>(
339-
query: SemiDynamicQueryDispatcher<'tcx, C, FLAGS>,
339+
pub(crate) fn encode_query_results_inner<'a, 'tcx, Q, const FLAGS: QueryFlags, Value>(
340340
tcx: TyCtxt<'tcx>,
341341
encoder: &mut CacheEncoder<'a, 'tcx>,
342342
query_result_index: &mut EncodedDepNodeIndex,
343343
) where
344-
Q: QueryDispatcherUnerased<'tcx, C, FLAGS>,
345-
Q::UnerasedValue: Encodable<CacheEncoder<'a, 'tcx>>,
344+
Q: GetQueryVTable<'tcx, FLAGS>,
345+
Q::Cache: QueryCache<Value = Erased<Value>>,
346+
Value: Erasable + Encodable<CacheEncoder<'a, 'tcx>>,
346347
{
348+
let query = Q::query_dispatcher(tcx);
347349
let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name());
348350

349351
assert!(all_inactive(query.query_state(tcx)));
@@ -357,7 +359,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache, const FLAGS: Quer
357359

358360
// Encode the type check tables with the `SerializedDepNodeIndex`
359361
// as tag.
360-
encoder.encode_tagged(dep_node, &Q::restore_val(*value));
362+
encoder.encode_tagged(dep_node, &erase::restore_val(*value));
361363
}
362364
});
363365
}
@@ -485,7 +487,6 @@ macro_rules! define_queries {
485487

486488
pub(crate) mod query_impl { $(pub(crate) mod $name {
487489
use super::super::*;
488-
use std::marker::PhantomData;
489490
use ::rustc_middle::query::erase::{self, Erased};
490491

491492
pub(crate) mod get_query_incr {
@@ -503,7 +504,7 @@ macro_rules! define_queries {
503504
#[cfg(debug_assertions)]
504505
let _guard = tracing::span!(tracing::Level::TRACE, stringify!($name), ?key).entered();
505506
execution::get_query_incr(
506-
QueryType::query_dispatcher(tcx),
507+
VTableGetter::query_dispatcher(tcx),
507508
tcx,
508509
span,
509510
key,
@@ -523,7 +524,7 @@ macro_rules! define_queries {
523524
__mode: QueryMode,
524525
) -> Option<Erased<queries::$name::Value<'tcx>>> {
525526
Some(execution::get_query_non_incr(
526-
QueryType::query_dispatcher(tcx),
527+
VTableGetter::query_dispatcher(tcx),
527528
tcx,
528529
span,
529530
key,
@@ -616,39 +617,28 @@ macro_rules! define_queries {
616617
}
617618
}
618619

619-
#[derive(Copy, Clone, Default)]
620-
pub(crate) struct QueryType<'tcx> {
621-
data: PhantomData<&'tcx ()>
622-
}
623-
624620
const FLAGS: QueryFlags = QueryFlags {
625621
is_anon: is_anon!([$($modifiers)*]),
626622
is_depth_limit: depth_limit!([$($modifiers)*]),
627623
is_feedable: feedable!([$($modifiers)*]),
628624
};
629625

630-
impl<'tcx> QueryDispatcherUnerased<'tcx, queries::$name::Storage<'tcx>, FLAGS>
631-
for QueryType<'tcx>
632-
{
633-
type UnerasedValue = queries::$name::Value<'tcx>;
626+
/// Marker type that implements [`GetQueryVTable`] for this query.
627+
pub(crate) enum VTableGetter {}
628+
629+
impl<'tcx> GetQueryVTable<'tcx, FLAGS> for VTableGetter {
630+
type Cache = queries::$name::Storage<'tcx>;
634631

635632
const NAME: &'static &'static str = &stringify!($name);
636633

637634
#[inline(always)]
638635
fn query_dispatcher(tcx: TyCtxt<'tcx>)
639-
-> SemiDynamicQueryDispatcher<'tcx, queries::$name::Storage<'tcx>, FLAGS>
636+
-> SemiDynamicQueryDispatcher<'tcx, Self::Cache, FLAGS>
640637
{
641638
SemiDynamicQueryDispatcher {
642639
vtable: &tcx.query_system.query_vtables.$name,
643640
}
644641
}
645-
646-
#[inline(always)]
647-
fn restore_val(value: <queries::$name::Storage<'tcx> as QueryCache>::Value)
648-
-> Self::UnerasedValue
649-
{
650-
erase::restore_val::<queries::$name::Value<'tcx>>(value)
651-
}
652642
}
653643

654644
/// Internal per-query plumbing for collecting the set of active jobs for this query.
@@ -702,12 +692,11 @@ macro_rules! define_queries {
702692
encoder: &mut CacheEncoder<'_, 'tcx>,
703693
query_result_index: &mut EncodedDepNodeIndex
704694
) {
705-
$crate::plumbing::encode_query_results::<
706-
query_impl::$name::QueryType<'tcx>,
695+
$crate::plumbing::encode_query_results_inner::<
696+
VTableGetter,
707697
_,
708698
_
709699
> (
710-
query_impl::$name::QueryType::query_dispatcher(tcx),
711700
tcx,
712701
encoder,
713702
query_result_index,
@@ -717,7 +706,7 @@ macro_rules! define_queries {
717706

718707
pub(crate) fn query_key_hash_verify<'tcx>(tcx: TyCtxt<'tcx>) {
719708
$crate::plumbing::query_key_hash_verify(
720-
query_impl::$name::QueryType::query_dispatcher(tcx),
709+
query_impl::$name::VTableGetter::query_dispatcher(tcx),
721710
tcx,
722711
)
723712
}
@@ -793,8 +782,8 @@ macro_rules! define_queries {
793782
$(
794783
/// `DepKindVTable` constructor for this query.
795784
pub(crate) fn $name<'tcx>() -> DepKindVTable<'tcx> {
796-
use $crate::query_impl::$name::QueryType;
797-
make_dep_kind_vtable_for_query::<QueryType<'tcx>, _, _>(
785+
use $crate::query_impl::$name::VTableGetter;
786+
make_dep_kind_vtable_for_query::<VTableGetter, _>(
798787
is_eval_always!([$($modifiers)*]),
799788
)
800789
}

0 commit comments

Comments
 (0)