rust/compiler/rustc_middle/src/ty/query/plumbing.rs

572 lines
19 KiB
Rust
Raw Normal View History

2020-03-19 14:24:21 +01:00
//! The implementation of the query system itself. This defines the macros that
//! generate the actual methods on tcx which find and execute the provider,
//! manage the caches, and so forth.
use crate::dep_graph;
use crate::ty::query::{on_disk_cache, Queries, Query};
2020-03-19 14:24:21 +01:00
use crate::ty::tls::{self, ImplicitCtxt};
use crate::ty::{self, TyCtxt};
use rustc_query_system::dep_graph::HasDepContext;
2020-03-19 14:24:21 +01:00
use rustc_query_system::query::QueryContext;
use rustc_query_system::query::{CycleError, QueryJobId, QueryJobInfo};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, Handler, Level};
2020-10-11 10:34:13 +02:00
use rustc_serialize::opaque;
2020-03-19 14:24:21 +01:00
use rustc_span::def_id::DefId;
use rustc_span::Span;
#[derive(Copy, Clone)]
2020-10-11 10:34:13 +02:00
pub struct QueryCtxt<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub queries: &'tcx super::Queries<'tcx>,
}
impl<'tcx> std::ops::Deref for QueryCtxt<'tcx> {
type Target = TyCtxt<'tcx>;
fn deref(&self) -> &Self::Target {
2020-10-11 10:34:13 +02:00
&self.tcx
}
}
impl HasDepContext for QueryCtxt<'tcx> {
type DepKind = crate::dep_graph::DepKind;
type StableHashingContext = crate::ich::StableHashingContext<'tcx>;
type DepContext = TyCtxt<'tcx>;
#[inline]
fn dep_context(&self) -> &Self::DepContext {
2020-10-11 10:34:13 +02:00
&self.tcx
}
}
impl QueryContext for QueryCtxt<'tcx> {
2020-03-19 14:24:21 +01:00
type Query = Query<'tcx>;
2020-03-25 08:02:27 +01:00
fn incremental_verify_ich(&self) -> bool {
self.sess.opts.debugging_opts.incremental_verify_ich
}
fn verbose(&self) -> bool {
self.sess.verbose()
2020-03-19 14:24:21 +01:00
}
fn def_path_str(&self, def_id: DefId) -> String {
2020-10-11 10:34:13 +02:00
self.tcx.def_path_str(def_id)
2020-03-19 14:24:21 +01:00
}
fn current_query_job(&self) -> Option<QueryJobId<Self::DepKind>> {
tls::with_related_context(**self, |icx| icx.query)
2020-03-19 14:24:21 +01:00
}
fn try_collect_active_jobs(
&self,
) -> Option<FxHashMap<QueryJobId<Self::DepKind>, QueryJobInfo<Self::DepKind, Self::Query>>>
{
2020-03-19 14:24:21 +01:00
self.queries.try_collect_active_jobs()
}
fn try_load_from_on_disk_cache(&self, dep_node: &dep_graph::DepNode) {
(dep_node.kind.try_load_from_on_disk_cache)(*self, dep_node)
}
2020-03-19 14:24:21 +01:00
/// Executes a job by changing the `ImplicitCtxt` to point to the
/// new query job while it executes. It returns the diagnostics
/// captured during execution and the actual result.
#[inline(always)]
fn start_query<R>(
&self,
token: QueryJobId<Self::DepKind>,
diagnostics: Option<&Lock<ThinVec<Diagnostic>>>,
2020-10-18 21:01:36 +02:00
compute: impl FnOnce() -> R,
2020-03-19 14:24:21 +01:00
) -> R {
// The `TyCtxt` stored in TLS has the same global interner lifetime
// as `self`, so we use `with_related_context` to relate the 'tcx lifetimes
// when accessing the `ImplicitCtxt`.
tls::with_related_context(**self, move |current_icx| {
2020-03-19 14:24:21 +01:00
// Update the `ImplicitCtxt` to point to our new query job.
let new_icx = ImplicitCtxt {
tcx: **self,
2020-03-19 14:24:21 +01:00
query: Some(token),
diagnostics,
layout_depth: current_icx.layout_depth,
task_deps: current_icx.task_deps,
};
// Use the `ImplicitCtxt` while we execute the query.
tls::enter_context(&new_icx, |_| {
2020-10-18 21:01:36 +02:00
rustc_data_structures::stack::ensure_sufficient_stack(compute)
})
2020-03-19 14:24:21 +01:00
})
}
}
impl<'tcx> QueryCtxt<'tcx> {
2020-03-19 14:24:21 +01:00
#[inline(never)]
#[cold]
pub(super) fn report_cycle(
self,
CycleError { usage, cycle: stack }: CycleError<Query<'tcx>>,
) -> DiagnosticBuilder<'tcx> {
assert!(!stack.is_empty());
let fix_span = |span: Span, query: &Query<'tcx>| {
self.sess.source_map().guess_head_span(query.default_span(*self, span))
2020-03-19 14:24:21 +01:00
};
// Disable naming impls with types in this path, since that
// sometimes cycles itself, leading to extra cycle errors.
// (And cycle errors around impls tend to occur during the
// collect/coherence phases anyhow.)
ty::print::with_forced_impl_filename_line(|| {
let span = fix_span(stack[1 % stack.len()].span, &stack[0].query);
let mut err = struct_span_err!(
self.sess,
span,
E0391,
"cycle detected when {}",
stack[0].query.describe(self)
);
for i in 1..stack.len() {
let query = &stack[i].query;
let span = fix_span(stack[(i + 1) % stack.len()].span, query);
err.span_note(span, &format!("...which requires {}...", query.describe(self)));
}
err.note(&format!(
"...which again requires {}, completing the cycle",
stack[0].query.describe(self)
));
if let Some((span, query)) = usage {
err.span_note(
fix_span(span, &query),
&format!("cycle used when {}", query.describe(self)),
);
}
err
})
}
2020-10-11 10:34:13 +02:00
pub(super) fn encode_query_results(
self,
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
) -> opaque::FileEncodeResult {
macro_rules! encode_queries {
($($query:ident,)*) => {
$(
on_disk_cache::encode_query_results::<ty::query::queries::$query<'_>>(
self,
encoder,
query_result_index
)?;
)*
}
}
rustc_cached_queries!(encode_queries!);
Ok(())
}
}
2020-03-19 14:24:21 +01:00
impl<'tcx> Queries<'tcx> {
pub fn try_print_query_stack(
&'tcx self,
tcx: TyCtxt<'tcx>,
query: Option<QueryJobId<dep_graph::DepKind>>,
handler: &Handler,
num_frames: Option<usize>,
) -> usize {
let query_map = self.try_collect_active_jobs();
let mut current_query = query;
let mut i = 0;
while let Some(query) = current_query {
if Some(i) == num_frames {
break;
2020-03-19 14:24:21 +01:00
}
let query_info = if let Some(info) = query_map.as_ref().and_then(|map| map.get(&query))
{
info
} else {
break;
};
let mut diag = Diagnostic::new(
Level::FailureNote,
&format!(
"#{} [{}] {}",
i,
query_info.info.query.name(),
query_info.info.query.describe(QueryCtxt { tcx, queries: self })
),
);
diag.span = tcx.sess.source_map().guess_head_span(query_info.info.span).into();
handler.force_print_diagnostic(diag);
2020-03-19 14:24:21 +01:00
current_query = query_info.job.parent;
i += 1;
}
i
2020-03-19 14:24:21 +01:00
}
}
macro_rules! handle_cycle_error {
([][$tcx: expr, $error:expr]) => {{
$tcx.report_cycle($error).emit();
Value::from_cycle_error($tcx)
}};
([fatal_cycle $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).emit();
$tcx.sess.abort_if_errors();
unreachable!()
}};
([cycle_delay_bug $($rest:tt)*][$tcx:expr, $error:expr]) => {{
$tcx.report_cycle($error).delay_as_bug();
Value::from_cycle_error($tcx)
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
handle_cycle_error!([$($($modifiers)*)*][$($args)*])
};
}
macro_rules! is_anon {
([]) => {{
false
}};
([anon $($rest:tt)*]) => {{
true
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
is_anon!([$($($modifiers)*)*])
};
}
macro_rules! is_eval_always {
([]) => {{
false
}};
([eval_always $($rest:tt)*]) => {{
true
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*]) => {
is_eval_always!([$($($modifiers)*)*])
};
}
macro_rules! query_storage {
2020-03-27 07:50:28 +01:00
([][$K:ty, $V:ty]) => {
<DefaultCacheSelector as CacheSelector<$K, $V>>::Cache
2020-03-19 14:24:21 +01:00
};
2020-03-27 07:50:28 +01:00
([storage($ty:ty) $($rest:tt)*][$K:ty, $V:ty]) => {
2020-03-27 18:46:25 +01:00
<$ty as CacheSelector<$K, $V>>::Cache
2020-03-19 14:24:21 +01:00
};
2020-03-27 07:50:28 +01:00
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
query_storage!([$($($modifiers)*)*][$($args)*])
2020-03-19 14:24:21 +01:00
};
}
macro_rules! hash_result {
([][$hcx:expr, $result:expr]) => {{
dep_graph::hash_result($hcx, &$result)
}};
([no_hash $($rest:tt)*][$hcx:expr, $result:expr]) => {{
None
}};
([$other:ident $(($($other_args:tt)*))* $(, $($modifiers:tt)*)*][$($args:tt)*]) => {
hash_result!([$($($modifiers)*)*][$($args)*])
};
}
2020-04-10 13:44:15 -07:00
macro_rules! query_helper_param_ty {
2020-04-10 13:54:06 -07:00
(DefId) => { impl IntoQueryParam<DefId> };
2020-04-10 13:44:15 -07:00
($K:ty) => { $K };
}
2020-10-11 20:44:24 +02:00
macro_rules! define_queries {
2020-03-19 14:24:21 +01:00
(<$tcx:tt>
2020-10-11 20:44:24 +02:00
$($(#[$attr:meta])*
2020-10-11 20:46:46 +02:00
[$($modifiers:tt)*] fn $name:ident($($K:tt)*) -> $V:ty,)*) => {
2020-03-19 14:24:21 +01:00
use std::mem;
use crate::{
rustc_data_structures::stable_hasher::HashStable,
rustc_data_structures::stable_hasher::StableHasher,
ich::StableHashingContext
};
define_queries_struct! {
tcx: $tcx,
input: ($(([$($modifiers)*] [$($attr)*] [$name]))*)
}
#[allow(nonstandard_style)]
#[derive(Clone, Debug)]
pub enum Query<$tcx> {
$($(#[$attr])* $name($($K)*)),*
2020-03-19 14:24:21 +01:00
}
impl<$tcx> Query<$tcx> {
pub fn name(&self) -> &'static str {
match *self {
$(Query::$name(_) => stringify!($name),)*
}
}
pub(crate) fn describe(&self, tcx: QueryCtxt<$tcx>) -> String {
2020-03-19 14:24:21 +01:00
let (r, name) = match *self {
$(Query::$name(key) => {
(queries::$name::describe(tcx, key), stringify!($name))
})*
};
if tcx.sess.verbose() {
format!("{} [{}]", r, name)
2020-03-19 14:24:21 +01:00
} else {
r
}
}
// FIXME(eddyb) Get more valid `Span`s on queries.
pub fn default_span(&self, tcx: TyCtxt<$tcx>, span: Span) -> Span {
if !span.is_dummy() {
return span;
}
// The `def_span` query is used to calculate `default_span`,
// so exit to avoid infinite recursion.
if let Query::def_span(..) = *self {
return span
}
match *self {
$(Query::$name(key) => key.default_span(tcx),)*
}
}
}
impl<'a, $tcx> HashStable<StableHashingContext<'a>> for Query<$tcx> {
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
mem::discriminant(self).hash_stable(hcx, hasher);
match *self {
$(Query::$name(key) => key.hash_stable(hcx, hasher),)*
}
}
}
#[allow(nonstandard_style)]
2020-03-19 14:24:21 +01:00
pub mod queries {
use std::marker::PhantomData;
$(pub struct $name<$tcx> {
2020-03-19 14:24:21 +01:00
data: PhantomData<&$tcx ()>
})*
}
// HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
// below, but using type aliases instead of associated types, to bypass
// the limitations around normalizing under HRTB - for example, this:
// `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
// doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
// This is primarily used by the `provide!` macro in `rustc_metadata`.
#[allow(nonstandard_style, unused_lifetimes)]
pub mod query_keys {
use super::*;
$(pub type $name<$tcx> = $($K)*;)*
}
#[allow(nonstandard_style, unused_lifetimes)]
pub mod query_values {
use super::*;
$(pub type $name<$tcx> = $V;)*
}
2021-01-17 14:57:07 +01:00
#[allow(nonstandard_style, unused_lifetimes)]
2021-01-19 20:02:05 +01:00
pub mod query_storage {
use super::*;
$(pub type $name<$tcx> = query_storage!([$($modifiers)*][$($K)*, $V]);)*
}
#[allow(nonstandard_style, unused_lifetimes)]
2021-01-17 14:57:07 +01:00
pub mod query_stored {
use super::*;
2021-01-19 20:02:05 +01:00
$(pub type $name<$tcx> = <query_storage::$name<$tcx> as QueryStorage>::Stored;)*
2021-01-17 14:57:07 +01:00
}
#[derive(Default)]
pub struct QueryCaches<$tcx> {
$($(#[$attr])* $name: QueryCacheStore<query_storage::$name<$tcx>>,)*
}
$(impl<$tcx> QueryConfig for queries::$name<$tcx> {
type Key = $($K)*;
2020-03-19 14:24:21 +01:00
type Value = $V;
2021-01-17 14:57:07 +01:00
type Stored = query_stored::$name<$tcx>;
2020-03-19 14:24:21 +01:00
const NAME: &'static str = stringify!($name);
}
impl<$tcx> QueryAccessors<QueryCtxt<$tcx>> for queries::$name<$tcx> {
2020-03-19 14:24:21 +01:00
const ANON: bool = is_anon!([$($modifiers)*]);
const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
2020-10-11 20:46:46 +02:00
const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$name;
2020-03-19 14:24:21 +01:00
2021-01-19 20:02:05 +01:00
type Cache = query_storage::$name<$tcx>;
2020-03-19 14:24:21 +01:00
#[inline(always)]
fn query_state<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryState<crate::dep_graph::DepKind, Query<$tcx>, Self::Key> {
2020-03-19 14:24:21 +01:00
&tcx.queries.$name
}
#[inline(always)]
fn query_cache<'a>(tcx: QueryCtxt<$tcx>) -> &'a QueryCacheStore<Self::Cache>
where 'tcx:'a
{
&tcx.query_caches.$name
}
2020-03-19 14:24:21 +01:00
#[inline]
fn compute(tcx: QueryCtxt<'tcx>, key: Self::Key) -> Self::Value {
2020-03-19 14:24:21 +01:00
let provider = tcx.queries.providers.get(key.query_crate())
// HACK(eddyb) it's possible crates may be loaded after
// the query engine is created, and because crate loading
// is not yet integrated with the query engine, such crates
// would be missing appropriate entries in `providers`.
.unwrap_or(&tcx.queries.fallback_extern_providers)
.$name;
provider(*tcx, key)
2020-03-19 14:24:21 +01:00
}
fn hash_result(
_hcx: &mut StableHashingContext<'_>,
_result: &Self::Value
) -> Option<Fingerprint> {
hash_result!([$($modifiers)*][_hcx, _result])
}
fn handle_cycle_error(
tcx: QueryCtxt<'tcx>,
2020-03-19 14:24:21 +01:00
error: CycleError<Query<'tcx>>
) -> Self::Value {
handle_cycle_error!([$($modifiers)*][tcx, error])
}
})*
define_provider_struct! {
tcx: $tcx,
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
2020-03-19 14:24:21 +01:00
}
}
}
// FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
// We should either not take `$tcx` at all and use `'tcx` everywhere, or use
// `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
2020-03-19 14:24:21 +01:00
macro_rules! define_queries_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
pub struct Queries<$tcx> {
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Box<Providers>,
2020-03-19 14:24:21 +01:00
$($(#[$attr])* $name: QueryState<
crate::dep_graph::DepKind,
Query<$tcx>,
query_keys::$name<$tcx>,
2020-03-19 14:24:21 +01:00
>,)*
}
impl<$tcx> Queries<$tcx> {
2020-10-11 10:34:13 +02:00
pub fn new(
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Providers,
2020-03-19 14:24:21 +01:00
) -> Self {
Queries {
providers,
fallback_extern_providers: Box::new(fallback_extern_providers),
$($name: Default::default()),*
}
}
pub(crate) fn try_collect_active_jobs(
&self
) -> Option<FxHashMap<QueryJobId<crate::dep_graph::DepKind>, QueryJobInfo<crate::dep_graph::DepKind, Query<$tcx>>>> {
2020-03-19 14:24:21 +01:00
let mut jobs = FxHashMap::default();
$(
self.$name.try_collect_active_jobs(
<queries::$name<'tcx> as QueryAccessors<QueryCtxt<'tcx>>>::DEP_KIND,
2020-03-19 14:24:21 +01:00
Query::$name,
&mut jobs,
)?;
)*
Some(jobs)
}
2021-01-17 14:58:34 +01:00
2020-10-11 10:34:13 +02:00
#[cfg(parallel_compiler)]
2020-11-18 19:02:31 +01:00
pub unsafe fn deadlock(&'tcx self, tcx: TyCtxt<'tcx>, registry: &rustc_rayon_core::Registry) {
2020-10-11 10:34:13 +02:00
let tcx = QueryCtxt { tcx, queries: self };
rustc_query_system::query::deadlock(tcx, registry)
}
pub(crate) fn encode_query_results(
&'tcx self,
tcx: TyCtxt<'tcx>,
encoder: &mut on_disk_cache::CacheEncoder<'a, 'tcx, opaque::FileEncoder>,
query_result_index: &mut on_disk_cache::EncodedQueryResultIndex,
) -> opaque::FileEncodeResult {
let tcx = QueryCtxt { tcx, queries: self };
tcx.encode_query_results(encoder, query_result_index)
}
fn exec_cache_promotions(&'tcx self, tcx: TyCtxt<'tcx>) {
let tcx = QueryCtxt { tcx, queries: self };
tcx.dep_graph.exec_cache_promotions(tcx)
}
2021-01-17 14:58:34 +01:00
$($(#[$attr])*
#[inline(always)]
fn $name(
2020-10-11 10:34:13 +02:00
&'tcx self,
2021-01-17 14:58:34 +01:00
tcx: TyCtxt<$tcx>,
span: Span,
key: query_keys::$name<$tcx>,
lookup: QueryLookup,
mode: QueryMode,
) -> Option<query_stored::$name<$tcx>> {
2020-10-11 10:34:13 +02:00
let qcx = QueryCtxt { tcx, queries: self };
2021-01-17 14:58:34 +01:00
get_query::<queries::$name<$tcx>, _>(qcx, span, key, lookup, mode)
})*
2020-03-19 14:24:21 +01:00
}
};
}
macro_rules! define_provider_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
pub struct Providers {
$(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
2020-03-19 14:24:21 +01:00
}
impl Default for Providers {
2020-03-19 14:24:21 +01:00
fn default() -> Self {
$(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
bug!("`tcx.{}({:?})` unsupported by its crate",
stringify!($name), key);
})*
Providers { $($name),* }
}
}
impl Copy for Providers {}
impl Clone for Providers {
fn clone(&self) -> Self { *self }
}
2020-03-19 14:24:21 +01:00
};
}