Rollup merge of #151893 - Zoxc:query-mod-move, r=nnethercote

Move the query list into a new `rustc_middle::queries` module

This moves the query list from `rustc_middle::query` into a new `rustc_middle::queries` module. This splits up the use of the query system from the remaining implementation of it in `rustc_middle::query`, which conceptually belong to `rustc_query_system`.

The goal is to let rustc crates define queries with their own `queries` module, and this makes `rustc_middle` also fit this pattern.

The inner `queries` module used by the macros are renamed to `query_info`, so it doesn't conflict with the new outer name.
This commit is contained in:
Jonathan Brouwer 2026-02-04 08:12:40 +01:00 committed by GitHub
commit 89594620f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 2902 additions and 2897 deletions

View file

@ -289,7 +289,7 @@ fn add_query_desc_cached_impl(
cached.extend(quote! {
#[allow(unused_variables, unused_braces, rustc::pass_by_value)]
#[inline]
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::query::queries::#name::Key<'tcx>) -> bool {
pub fn #name<'tcx>(#tcx: TyCtxt<'tcx>, #key: &crate::queries::#name::Key<'tcx>) -> bool {
#ra_hint
#expr
}
@ -301,7 +301,7 @@ fn add_query_desc_cached_impl(
let desc = quote! {
#[allow(unused_variables)]
pub fn #name<'tcx>(tcx: TyCtxt<'tcx>, key: crate::query::queries::#name::Key<'tcx>) -> String {
pub fn #name<'tcx>(tcx: TyCtxt<'tcx>, key: crate::queries::#name::Key<'tcx>) -> String {
let (#tcx, #key) = (tcx, key);
format!(#desc)
}

View file

@ -11,7 +11,8 @@ use rustc_middle::bug;
use rustc_middle::metadata::{AmbigModChild, ModChild};
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::query::{ExternProviders, LocalCrate};
use rustc_middle::queries::ExternProviders;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::util::Providers;
@ -134,8 +135,8 @@ macro_rules! provide_one {
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => $compute:block) => {
fn $name<'tcx>(
$tcx: TyCtxt<'tcx>,
def_id_arg: rustc_middle::query::queries::$name::Key<'tcx>,
) -> rustc_middle::query::queries::$name::ProvidedValue<'tcx> {
def_id_arg: rustc_middle::queries::$name::Key<'tcx>,
) -> rustc_middle::queries::$name::ProvidedValue<'tcx> {
let _prof_timer =
$tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name)));

View file

@ -86,6 +86,8 @@ mod values;
#[macro_use]
pub mod query;
#[macro_use]
pub mod queries;
#[macro_use]
pub mod dep_graph;
// Allows macros to refer to this crate as `::rustc_middle`

File diff suppressed because it is too large Load diff

View file

@ -15,7 +15,7 @@ use rustc_span::source_map::Spanned;
use crate::mir::interpret::EvalToValTreeResult;
use crate::mir::mono::{MonoItem, NormalizationErrorInMono};
use crate::query::CyclePlaceholder;
use crate::query::plumbing::CyclePlaceholder;
use crate::traits::solve;
use crate::ty::adjustment::CoerceUnsizedInfo;
use crate::ty::{self, Ty, TyCtxt};

File diff suppressed because it is too large Load diff

View file

@ -12,10 +12,10 @@ pub use sealed::IntoQueryParam;
use crate::dep_graph;
use crate::dep_graph::DepKind;
use crate::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use crate::query::{
use crate::queries::{
ExternProviders, PerQueryVTables, Providers, QueryArenas, QueryCaches, QueryEngine, QueryStates,
};
use crate::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use crate::ty::TyCtxt;
pub type WillCacheOnDiskForKeyFn<'tcx, Key> = fn(tcx: TyCtxt<'tcx>, key: &Key) -> bool;
@ -189,8 +189,8 @@ macro_rules! query_ensure_select {
}
macro_rules! query_helper_param_ty {
(DefId) => { impl IntoQueryParam<DefId> };
(LocalDefId) => { impl IntoQueryParam<LocalDefId> };
(DefId) => { impl $crate::query::IntoQueryParam<DefId> };
(LocalDefId) => { impl $crate::query::IntoQueryParam<LocalDefId> };
($K:ty) => { $K };
}
@ -213,7 +213,7 @@ macro_rules! local_key_if_separate_extern {
$($K)*
};
([(separate_provide_extern) $($rest:tt)*] $($K:tt)*) => {
<$($K)* as AsLocalKey>::LocalKey
<$($K)* as $crate::query::AsLocalKey>::LocalKey
};
([$other:tt $($modifiers:tt)*] $($K:tt)*) => {
local_key_if_separate_extern!([$($modifiers)*] $($K)*)
@ -227,8 +227,8 @@ macro_rules! separate_provide_extern_decl {
([(separate_provide_extern) $($rest:tt)*][$name:ident]) => {
for<'tcx> fn(
TyCtxt<'tcx>,
queries::$name::Key<'tcx>,
) -> queries::$name::ProvidedValue<'tcx>
$name::Key<'tcx>,
) -> $name::ProvidedValue<'tcx>
};
([$other:tt $($modifiers:tt)*][$($args:tt)*]) => {
separate_provide_extern_decl!([$($modifiers)*][$($args)*])
@ -266,94 +266,90 @@ macro_rules! define_callbacks {
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,
)*
) => {
$(#[allow(unused_lifetimes)] pub mod $name {
use super::*;
use $crate::query::erase::{self, Erased};
#[allow(unused_lifetimes)]
pub mod queries {
$(pub mod $name {
use super::super::*;
use $crate::query::erase::{self, Erased};
pub type Key<'tcx> = $($K)*;
pub type Value<'tcx> = $V;
pub type Key<'tcx> = $($K)*;
pub type Value<'tcx> = $V;
pub type LocalKey<'tcx> = local_key_if_separate_extern!([$($modifiers)*] $($K)*);
pub type LocalKey<'tcx> = local_key_if_separate_extern!([$($modifiers)*] $($K)*);
/// This type alias specifies the type returned from query providers and the type
/// used for decoding. For regular queries this is the declared returned type `V`,
/// but `arena_cache` will use `<V as ArenaCached>::Provided` instead.
pub type ProvidedValue<'tcx> = query_if_arena!(
[$($modifiers)*]
(<$V as $crate::query::arena_cached::ArenaCached<'tcx>>::Provided)
($V)
);
/// This type alias specifies the type returned from query providers and the type
/// used for decoding. For regular queries this is the declared returned type `V`,
/// but `arena_cache` will use `<V as ArenaCached>::Provided` instead.
pub type ProvidedValue<'tcx> = query_if_arena!(
[$($modifiers)*]
(<$V as $crate::query::arena_cached::ArenaCached<'tcx>>::Provided)
($V)
);
/// This function takes `ProvidedValue` and converts it to an erased `Value` by
/// allocating it on an arena if the query has the `arena_cache` modifier. The
/// value is then erased and returned. This will happen when computing the query
/// using a provider or decoding a stored result.
#[inline(always)]
pub fn provided_to_erased<'tcx>(
_tcx: TyCtxt<'tcx>,
provided_value: ProvidedValue<'tcx>,
) -> Erased<Value<'tcx>> {
// Store the provided value in an arena and get a reference
// to it, for queries with `arena_cache`.
let value: Value<'tcx> = query_if_arena!([$($modifiers)*]
{
use $crate::query::arena_cached::ArenaCached;
/// This function takes `ProvidedValue` and converts it to an erased `Value` by
/// allocating it on an arena if the query has the `arena_cache` modifier. The
/// value is then erased and returned. This will happen when computing the query
/// using a provider or decoding a stored result.
#[inline(always)]
pub fn provided_to_erased<'tcx>(
_tcx: TyCtxt<'tcx>,
provided_value: ProvidedValue<'tcx>,
) -> Erased<Value<'tcx>> {
// Store the provided value in an arena and get a reference
// to it, for queries with `arena_cache`.
let value: Value<'tcx> = query_if_arena!([$($modifiers)*]
{
use $crate::query::arena_cached::ArenaCached;
if mem::needs_drop::<<$V as ArenaCached<'tcx>>::Allocated>() {
<$V as ArenaCached>::alloc_in_arena(
|v| _tcx.query_system.arenas.$name.alloc(v),
provided_value,
)
} else {
<$V as ArenaCached>::alloc_in_arena(
|v| _tcx.arena.dropless.alloc(v),
provided_value,
)
}
if mem::needs_drop::<<$V as ArenaCached<'tcx>>::Allocated>() {
<$V as ArenaCached>::alloc_in_arena(
|v| _tcx.query_system.arenas.$name.alloc(v),
provided_value,
)
} else {
<$V as ArenaCached>::alloc_in_arena(
|v| _tcx.arena.dropless.alloc(v),
provided_value,
)
}
// Otherwise, the provided value is the value.
(provided_value)
);
erase::erase_val(value)
}
// Otherwise, the provided value is the value.
(provided_value)
);
erase::erase_val(value)
}
pub type Storage<'tcx> = <$($K)* as $crate::query::Key>::Cache<Erased<$V>>;
// Ensure that keys grow no larger than 88 bytes by accident.
// Increase this limit if necessary, but do try to keep the size low if possible
#[cfg(target_pointer_width = "64")]
const _: () = {
if size_of::<Key<'static>>() > 88 {
panic!("{}", concat!(
"the query `",
stringify!($name),
"` has a key type `",
stringify!($($K)*),
"` that is too large"
));
}
};
pub type Storage<'tcx> = <$($K)* as keys::Key>::Cache<Erased<$V>>;
// Ensure that keys grow no larger than 88 bytes by accident.
// Increase this limit if necessary, but do try to keep the size low if possible
#[cfg(target_pointer_width = "64")]
const _: () = {
if size_of::<Key<'static>>() > 88 {
panic!("{}", concat!(
"the query `",
stringify!($name),
"` has a key type `",
stringify!($($K)*),
"` that is too large"
));
}
};
// Ensure that values grow no larger than 64 bytes by accident.
// Increase this limit if necessary, but do try to keep the size low if possible
#[cfg(target_pointer_width = "64")]
#[cfg(not(feature = "rustc_randomized_layouts"))]
const _: () = {
if size_of::<Value<'static>>() > 64 {
panic!("{}", concat!(
"the query `",
stringify!($name),
"` has a value type `",
stringify!($V),
"` that is too large"
));
}
};
})*
}
// Ensure that values grow no larger than 64 bytes by accident.
// Increase this limit if necessary, but do try to keep the size low if possible
#[cfg(target_pointer_width = "64")]
#[cfg(not(feature = "rustc_randomized_layouts"))]
const _: () = {
if size_of::<Value<'static>>() > 64 {
panic!("{}", concat!(
"the query `",
stringify!($name),
"` has a value type `",
stringify!($V),
"` that is too large"
));
}
};
})*
/// Holds per-query arenas for queries with the `arena_cache` modifier.
#[derive(Default)]
@ -371,10 +367,10 @@ macro_rules! define_callbacks {
#[derive(Default)]
pub struct QueryCaches<'tcx> {
$($(#[$attr])* pub $name: queries::$name::Storage<'tcx>,)*
$($(#[$attr])* pub $name: $name::Storage<'tcx>,)*
}
impl<'tcx> TyCtxtEnsureOk<'tcx> {
impl<'tcx> $crate::query::TyCtxtEnsureOk<'tcx> {
$($(#[$attr])*
#[inline(always)]
pub fn $name(
@ -386,13 +382,13 @@ macro_rules! define_callbacks {
self.tcx,
self.tcx.query_system.fns.engine.$name,
&self.tcx.query_system.caches.$name,
key.into_query_param(),
$crate::query::IntoQueryParam::into_query_param(key),
false,
)
})*
}
impl<'tcx> TyCtxtEnsureDone<'tcx> {
impl<'tcx> $crate::query::TyCtxtEnsureDone<'tcx> {
$($(#[$attr])*
#[inline(always)]
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
@ -400,7 +396,7 @@ macro_rules! define_callbacks {
self.tcx,
self.tcx.query_system.fns.engine.$name,
&self.tcx.query_system.caches.$name,
key.into_query_param(),
$crate::query::IntoQueryParam::into_query_param(key),
true,
);
})*
@ -416,7 +412,7 @@ macro_rules! define_callbacks {
})*
}
impl<'tcx> TyCtxtAt<'tcx> {
impl<'tcx> $crate::query::TyCtxtAt<'tcx> {
$($(#[$attr])*
#[inline(always)]
pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V
@ -428,7 +424,7 @@ macro_rules! define_callbacks {
self.tcx.query_system.fns.engine.$name,
&self.tcx.query_system.caches.$name,
self.span,
key.into_query_param(),
$crate::query::IntoQueryParam::into_query_param(key),
))
})*
}
@ -438,22 +434,22 @@ macro_rules! define_callbacks {
/// ("Per" just makes this pluralized name more visually distinct.)
pub struct PerQueryVTables<'tcx> {
$(
pub $name: ::rustc_middle::query::plumbing::QueryVTable<'tcx, queries::$name::Storage<'tcx>>,
pub $name: ::rustc_middle::query::plumbing::QueryVTable<'tcx, $name::Storage<'tcx>>,
)*
}
#[derive(Default)]
pub struct QueryStates<'tcx> {
$(
pub $name: QueryState<'tcx, $($K)*>,
pub $name: $crate::query::QueryState<'tcx, $($K)*>,
)*
}
pub struct Providers {
$(pub $name: for<'tcx> fn(
TyCtxt<'tcx>,
queries::$name::LocalKey<'tcx>,
) -> queries::$name::ProvidedValue<'tcx>,)*
$name::LocalKey<'tcx>,
) -> $name::ProvidedValue<'tcx>,)*
}
pub struct ExternProviders {
@ -490,8 +486,8 @@ macro_rules! define_callbacks {
$(pub $name: for<'tcx> fn(
TyCtxt<'tcx>,
Span,
queries::$name::Key<'tcx>,
QueryMode,
$name::Key<'tcx>,
$crate::query::QueryMode,
) -> Option<$crate::query::erase::Erased<$V>>,)*
}
};
@ -499,14 +495,14 @@ macro_rules! define_callbacks {
macro_rules! define_feedable {
($($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
$(impl<'tcx, K: IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
$(impl<'tcx, K: $crate::query::IntoQueryParam<$($K)*> + Copy> TyCtxtFeed<'tcx, K> {
$(#[$attr])*
#[inline(always)]
pub fn $name(self, value: queries::$name::ProvidedValue<'tcx>) {
pub fn $name(self, value: $name::ProvidedValue<'tcx>) {
let key = self.key().into_query_param();
let tcx = self.tcx;
let erased_value = queries::$name::provided_to_erased(tcx, value);
let erased_value = $name::provided_to_erased(tcx, value);
let dep_kind: dep_graph::DepKind = dep_graph::dep_kinds::$name;

View file

@ -2,7 +2,7 @@ pub mod bug;
#[derive(Default, Copy, Clone)]
pub struct Providers {
pub queries: crate::query::Providers,
pub extern_queries: crate::query::ExternProviders,
pub queries: crate::queries::Providers,
pub extern_queries: crate::queries::ExternProviders,
pub hooks: crate::hooks::Providers,
}

View file

@ -13,11 +13,12 @@ use rustc_data_structures::stable_hasher::HashStable;
use rustc_data_structures::sync::AtomicU64;
use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::{self, DepKind, DepKindVTable, DepNodeIndex};
use rustc_middle::queries::{
self, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates,
};
use rustc_middle::query::AsLocalKey;
use rustc_middle::query::on_disk_cache::{CacheEncoder, EncodedDepNodeIndex, OnDiskCache};
use rustc_middle::query::plumbing::{QuerySystem, QuerySystemFns, QueryVTable};
use rustc_middle::query::{
AsLocalKey, ExternProviders, Providers, QueryCaches, QueryEngine, QueryStates, queries,
};
use rustc_middle::ty::TyCtxt;
use rustc_query_system::Value;
use rustc_query_system::dep_graph::SerializedDepNodeIndex;

View file

@ -647,7 +647,7 @@ macro_rules! define_queries {
query_state: std::mem::offset_of!(QueryStates<'tcx>, $name),
query_cache: std::mem::offset_of!(QueryCaches<'tcx>, $name),
will_cache_on_disk_for_key_fn: should_ever_cache_on_disk!([$($modifiers)*] {
Some(::rustc_middle::query::cached::$name)
Some(queries::cached::$name)
} {
None
}),
@ -671,7 +671,7 @@ macro_rules! define_queries {
try_load_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
Some(|tcx, key, prev_index, index| {
// Check the `cache_on_disk_if` condition for this key.
if !::rustc_middle::query::cached::$name(tcx, key) {
if !queries::cached::$name(tcx, key) {
return None;
}
@ -686,7 +686,7 @@ macro_rules! define_queries {
}),
is_loadable_from_disk_fn: should_ever_cache_on_disk!([$($modifiers)*] {
Some(|tcx, key, index| -> bool {
::rustc_middle::query::cached::$name(tcx, key) &&
::rustc_middle::queries::cached::$name(tcx, key) &&
$crate::plumbing::loadable_from_disk(tcx, index)
})
} {
@ -746,7 +746,7 @@ macro_rules! define_queries {
let make_frame = |tcx, key| {
let kind = rustc_middle::dep_graph::dep_kinds::$name;
let name = stringify!($name);
$crate::plumbing::create_query_frame(tcx, rustc_middle::query::descs::$name, key, kind, name)
$crate::plumbing::create_query_frame(tcx, queries::descs::$name, key, kind, name)
};
// Call `gather_active_jobs_inner` to do the actual work.
@ -816,8 +816,8 @@ macro_rules! define_queries {
}
}
pub fn make_query_vtables<'tcx>() -> ::rustc_middle::query::PerQueryVTables<'tcx> {
::rustc_middle::query::PerQueryVTables {
pub fn make_query_vtables<'tcx>() -> queries::PerQueryVTables<'tcx> {
queries::PerQueryVTables {
$(
$name: query_impl::$name::make_query_vtable(),
)*

View file

@ -37,7 +37,7 @@ use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_interface::Config;
use rustc_interface::interface::Compiler;
use rustc_middle::query::queries::mir_borrowck::ProvidedValue;
use rustc_middle::queries::mir_borrowck::ProvidedValue;
use rustc_middle::ty::TyCtxt;
use rustc_middle::util::Providers;
use rustc_session::Session;