Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions compiler/rustc_query_impl/src/dep_kind_vtables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::bug;
use rustc_middle::dep_graph::{DepKindVTable, DepNodeKey, KeyFingerprintStyle};
use rustc_middle::query::QueryCache;

use crate::QueryDispatcherUnerased;
use crate::GetQueryVTable;
use crate::plumbing::{force_from_dep_node_inner, try_load_from_on_disk_cache_inner};

/// [`DepKindVTable`] constructors for special dep kinds that aren't queries.
Expand Down Expand Up @@ -102,18 +102,17 @@ mod non_query {

/// Shared implementation of the [`DepKindVTable`] constructor for queries.
/// Called from macro-generated code for each query.
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q, Cache>(
pub(crate) fn make_dep_kind_vtable_for_query<'tcx, Q>(
is_anon: bool,
is_eval_always: bool,
) -> DepKindVTable<'tcx>
where
Q: QueryDispatcherUnerased<'tcx, Cache>,
Cache: QueryCache + 'tcx,
Q: GetQueryVTable<'tcx>,
{
let key_fingerprint_style = if is_anon {
KeyFingerprintStyle::Opaque
} else {
<Cache::Key as DepNodeKey<'tcx>>::key_fingerprint_style()
<Q::Cache as QueryCache>::Key::key_fingerprint_style()
};

if is_anon || !key_fingerprint_style.reconstructible() {
Expand Down
14 changes: 5 additions & 9 deletions compiler/rustc_query_impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,18 @@ mod job;
mod profiling_support;
mod values;

/// Provides access to vtable-like operations for a query (by obtaining a
/// `QueryVTable`), but also keeps track of the "unerased" value type of the
/// query (i.e. the actual result type in the query declaration).
/// Trait that knows how to look up the [`QueryVTable`] for a particular query.
///
/// This trait allows some per-query code to be defined in generic functions
/// with a trait bound, instead of having to be defined inline within a macro
/// expansion.
///
/// There is one macro-generated implementation of this trait for each query,
/// on the type `rustc_query_impl::query_impl::$name::QueryType`.
trait QueryDispatcherUnerased<'tcx, C: QueryCache> {
type UnerasedValue;
/// on the type `rustc_query_impl::query_impl::$name::VTableGetter`.
trait GetQueryVTable<'tcx> {
type Cache: QueryCache + 'tcx;

fn query_vtable(tcx: TyCtxt<'tcx>) -> &'tcx QueryVTable<'tcx, C>;

fn restore_val(value: C::Value) -> Self::UnerasedValue;
fn query_vtable(tcx: TyCtxt<'tcx>) -> &'tcx QueryVTable<'tcx, Self::Cache>;
}

pub fn query_system<'tcx>(
Expand Down
49 changes: 16 additions & 33 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ use rustc_middle::bug;
#[expect(unused_imports, reason = "used by doc comments")]
use rustc_middle::dep_graph::DepKindVTable;
use rustc_middle::dep_graph::{DepKind, DepNode, DepNodeIndex, DepNodeKey, SerializedDepNodeIndex};
use rustc_middle::query::erase::{Erasable, Erased};
use rustc_middle::query::on_disk_cache::{
AbsoluteBytePos, CacheDecoder, CacheEncoder, EncodedDepNodeIndex,
};
use rustc_middle::query::plumbing::QueryVTable;
use rustc_middle::query::{
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra,
Key, QueryCache, QueryJobId, QueryStackDeferred, QueryStackFrame, QueryStackFrameExtra, erase,
};
use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::print::with_reduced_queries;
Expand All @@ -27,7 +28,6 @@ use rustc_middle::ty::{self, TyCtxt};
use rustc_serialize::{Decodable, Encodable};
use rustc_span::def_id::LOCAL_CRATE;

use crate::QueryDispatcherUnerased;
use crate::error::{QueryOverflow, QueryOverflowNote};
use crate::execution::{all_inactive, force_query};
use crate::job::{QueryJobMap, find_dep_kind_root};
Expand Down Expand Up @@ -324,14 +324,14 @@ where
QueryStackFrame::new(info, kind, def_id, def_id_for_ty_in_cycle)
}

pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache>(
query: &'tcx QueryVTable<'tcx, C>,
pub(crate) fn encode_query_results_inner<'a, 'tcx, C, V>(
tcx: TyCtxt<'tcx>,
query: &'tcx QueryVTable<'tcx, C>,
encoder: &mut CacheEncoder<'a, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex,
) where
Q: QueryDispatcherUnerased<'tcx, C>,
Q::UnerasedValue: Encodable<CacheEncoder<'a, 'tcx>>,
C: QueryCache<Value = Erased<V>>,
V: Erasable + Encodable<CacheEncoder<'a, 'tcx>>,
{
let _timer = tcx.prof.generic_activity_with_arg("encode_query_results_for", query.name);

Expand All @@ -346,7 +346,7 @@ pub(crate) fn encode_query_results<'a, 'tcx, Q, C: QueryCache>(

// Encode the type check tables with the `SerializedDepNodeIndex`
// as tag.
encoder.encode_tagged(dep_node, &Q::restore_val(*value));
encoder.encode_tagged(dep_node, &erase::restore_val::<V>(*value));
}
});
}
Expand Down Expand Up @@ -473,7 +473,6 @@ macro_rules! define_queries {

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

pub(crate) mod get_query_incr {
Expand Down Expand Up @@ -607,29 +606,16 @@ macro_rules! define_queries {
}
}

#[derive(Copy, Clone, Default)]
pub(crate) struct QueryType<'tcx> {
data: PhantomData<&'tcx ()>
}
/// Marker type that implements [`GetQueryVTable`] for this query.
pub(crate) enum VTableGetter {}

impl<'tcx> QueryDispatcherUnerased<'tcx, queries::$name::Storage<'tcx>>
for QueryType<'tcx>
{
type UnerasedValue = queries::$name::Value<'tcx>;
impl<'tcx> GetQueryVTable<'tcx> for VTableGetter {
type Cache = rustc_middle::queries::$name::Storage<'tcx>;

#[inline(always)]
fn query_vtable(tcx: TyCtxt<'tcx>)
-> &'tcx QueryVTable<'tcx, queries::$name::Storage<'tcx>>
{
fn query_vtable(tcx: TyCtxt<'tcx>) -> &'tcx QueryVTable<'tcx, Self::Cache> {
&tcx.query_system.query_vtables.$name
}

#[inline(always)]
fn restore_val(value: <queries::$name::Storage<'tcx> as QueryCache>::Value)
-> Self::UnerasedValue
{
erase::restore_val::<queries::$name::Value<'tcx>>(value)
}
}

/// Internal per-query plumbing for collecting the set of active jobs for this query.
Expand Down Expand Up @@ -683,12 +669,9 @@ macro_rules! define_queries {
encoder: &mut CacheEncoder<'_, 'tcx>,
query_result_index: &mut EncodedDepNodeIndex
) {
$crate::plumbing::encode_query_results::<
query_impl::$name::QueryType<'tcx>,
_
> (
&tcx.query_system.query_vtables.$name,
$crate::plumbing::encode_query_results_inner(
tcx,
&tcx.query_system.query_vtables.$name,
encoder,
query_result_index,
)
Expand Down Expand Up @@ -773,8 +756,8 @@ macro_rules! define_queries {
$(
/// `DepKindVTable` constructor for this query.
pub(crate) fn $name<'tcx>() -> DepKindVTable<'tcx> {
use $crate::query_impl::$name::QueryType;
make_dep_kind_vtable_for_query::<QueryType<'tcx>, _>(
use $crate::query_impl::$name::VTableGetter;
make_dep_kind_vtable_for_query::<VTableGetter>(
is_anon!([$($modifiers)*]),
is_eval_always!([$($modifiers)*]),
)
Expand Down
Loading