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
4 changes: 2 additions & 2 deletions compiler/rustc_macros/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)));

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2,775 changes: 2,775 additions & 0 deletions compiler/rustc_middle/src/queries.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2,782 changes: 6 additions & 2,776 deletions compiler/rustc_middle/src/query/mod.rs

Large diffs are not rendered by default.

212 changes: 104 additions & 108 deletions compiler/rustc_middle/src/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 };
}

Expand All @@ -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)*)
Expand All @@ -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)*])
Expand Down Expand Up @@ -266,94 +266,90 @@ macro_rules! define_callbacks {
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,
)*
) => {

#[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 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)
$(#[allow(unused_lifetimes)] pub mod $name {
use super::*;
use $crate::query::erase::{self, Erased};

pub type Key<'tcx> = $($K)*;
pub type Value<'tcx> = $V;

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 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,
)
}
}
// Otherwise, the provided value is the value.
(provided_value)
);
erase::erase_val(value)
}

/// 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,
)
}
}
// 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)]
Expand All @@ -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(
Expand All @@ -386,21 +382,21 @@ 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)*)) {
crate::query::inner::query_ensure(
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,
);
})*
Expand All @@ -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
Expand All @@ -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),
))
})*
}
Expand All @@ -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 {
Expand Down Expand Up @@ -490,23 +486,23 @@ 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>>,)*
}
};
}

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;

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_middle/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Loading
Loading