Make QueryDescription parameter a type.

This commit is contained in:
Camille GILLOT 2020-03-08 19:42:11 +01:00
parent f74fd03999
commit 57c3177b31
4 changed files with 40 additions and 27 deletions

View File

@ -5,12 +5,12 @@ use crate::dep_graph::{DepKind, DepNode};
use crate::ty::query::caches::QueryCache;
use crate::ty::query::plumbing::CycleError;
use crate::ty::query::QueryState;
use crate::ty::TyCtxt;
use rustc_data_structures::profiling::ProfileCategory;
use rustc_hir::def_id::DefId;
use crate::ich::StableHashingContext;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_session::Session;
use std::borrow::Cow;
use std::fmt::Debug;
use std::hash::Hash;
@ -25,6 +25,12 @@ pub trait QueryConfig<CTX> {
pub trait QueryContext: Copy {
type Query;
/// Access the session.
fn session(&self) -> &Session;
/// Get string representation from DefPath.
fn def_path_str(&self, def_id: DefId) -> String;
}
pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
@ -48,26 +54,25 @@ pub(crate) trait QueryAccessors<CTX: QueryContext>: QueryConfig<CTX> {
fn handle_cycle_error(tcx: CTX, error: CycleError<CTX>) -> Self::Value;
}
pub(crate) trait QueryDescription<'tcx>: QueryAccessors<TyCtxt<'tcx>> {
fn describe(tcx: TyCtxt<'_>, key: Self::Key) -> Cow<'static, str>;
pub(crate) trait QueryDescription<CTX: QueryContext>: QueryAccessors<CTX> {
fn describe(tcx: CTX, key: Self::Key) -> Cow<'static, str>;
#[inline]
fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
false
}
fn try_load_from_disk(_: TyCtxt<'tcx>, _: SerializedDepNodeIndex) -> Option<Self::Value> {
fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}
}
impl<'tcx, M> QueryDescription<'tcx> for M
impl<CTX: QueryContext, M> QueryDescription<CTX> for M
where
M: QueryAccessors<TyCtxt<'tcx>, Key = DefId>,
//M::Cache: QueryCache<DefId, M::Value>,
M: QueryAccessors<CTX, Key = DefId>,
{
default fn describe(tcx: TyCtxt<'_>, def_id: DefId) -> Cow<'static, str> {
if !tcx.sess.verbose() {
default fn describe(tcx: CTX, def_id: DefId) -> Cow<'static, str> {
if !tcx.session().verbose() {
format!("processing `{}`", tcx.def_path_str(def_id)).into()
} else {
let name = ::std::any::type_name::<M>();
@ -75,14 +80,11 @@ where
}
}
default fn cache_on_disk(_: TyCtxt<'tcx>, _: Self::Key, _: Option<&Self::Value>) -> bool {
default fn cache_on_disk(_: CTX, _: Self::Key, _: Option<&Self::Value>) -> bool {
false
}
default fn try_load_from_disk(
_: TyCtxt<'tcx>,
_: SerializedDepNodeIndex,
) -> Option<Self::Value> {
default fn try_load_from_disk(_: CTX, _: SerializedDepNodeIndex) -> Option<Self::Value> {
bug!("QueryDescription::load_from_disk() called for an unsupported query.")
}
}

View File

@ -994,7 +994,8 @@ fn encode_query_results<'a, 'tcx, Q, E>(
query_result_index: &mut EncodedQueryResultIndex,
) -> Result<(), E::Error>
where
Q: super::config::QueryDescription<'tcx, Value: Encodable>,
Q: super::config::QueryDescription<TyCtxt<'tcx>>,
Q::Value: Encodable,
E: 'a + TyEncoder,
{
let _timer = tcx

View File

@ -17,6 +17,8 @@ use rustc_data_structures::sharded::Sharded;
use rustc_data_structures::sync::{Lock, LockGuard};
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::{struct_span_err, Diagnostic, DiagnosticBuilder, FatalError, Handler, Level};
use rustc_session::Session;
use rustc_span::def_id::DefId;
use rustc_span::source_map::DUMMY_SP;
use rustc_span::Span;
use std::collections::hash_map::Entry;
@ -181,7 +183,7 @@ where
mut lookup: QueryLookup<'tcx, TyCtxt<'tcx>, C::Key, C::Sharded>,
) -> TryGetJob<'tcx, C>
where
Q: QueryDescription<'tcx, Key = C::Key, Value = C::Value, Cache = C>,
Q: QueryDescription<TyCtxt<'tcx>, Key = C::Key, Value = C::Value, Cache = C>,
{
let lock = &mut *lookup.lock;
@ -356,6 +358,14 @@ where
impl QueryContext for TyCtxt<'tcx> {
type Query = Query<'tcx>;
fn session(&self) -> &Session {
&self.sess
}
fn def_path_str(&self, def_id: DefId) -> String {
TyCtxt::def_path_str(*self, def_id)
}
}
impl<'tcx> TyCtxt<'tcx> {
@ -517,7 +527,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
#[inline(never)]
pub(super) fn get_query<Q: QueryDescription<'tcx> + 'tcx>(
pub(super) fn get_query<Q: QueryDescription<TyCtxt<'tcx>> + 'tcx>(
self,
span: Span,
key: Q::Key,
@ -536,7 +546,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
#[inline(always)]
fn try_execute_query<Q: QueryDescription<'tcx> + 'tcx>(
fn try_execute_query<Q: QueryDescription<TyCtxt<'tcx>> + 'tcx>(
self,
span: Span,
key: Q::Key,
@ -614,7 +624,7 @@ impl<'tcx> TyCtxt<'tcx> {
result
}
fn load_from_disk_and_cache_in_memory<Q: QueryDescription<'tcx>>(
fn load_from_disk_and_cache_in_memory<Q: QueryDescription<TyCtxt<'tcx>>>(
self,
key: Q::Key,
prev_dep_node_index: SerializedDepNodeIndex,
@ -671,7 +681,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[inline(never)]
#[cold]
fn incremental_verify_ich<Q: QueryDescription<'tcx>>(
fn incremental_verify_ich<Q: QueryDescription<TyCtxt<'tcx>>>(
self,
result: &Q::Value,
dep_node: &DepNode,
@ -698,7 +708,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
#[inline(always)]
fn force_query_with_job<Q: QueryDescription<'tcx> + 'tcx>(
fn force_query_with_job<Q: QueryDescription<TyCtxt<'tcx>> + 'tcx>(
self,
key: Q::Key,
job: JobOwner<'tcx, TyCtxt<'tcx>, Q::Cache>,
@ -756,7 +766,7 @@ impl<'tcx> TyCtxt<'tcx> {
/// side-effects -- e.g., in order to report errors for erroneous programs.
///
/// Note: The optimization is only available during incr. comp.
pub(super) fn ensure_query<Q: QueryDescription<'tcx> + 'tcx>(self, key: Q::Key) {
pub(super) fn ensure_query<Q: QueryDescription<TyCtxt<'tcx>> + 'tcx>(self, key: Q::Key) {
if Q::EVAL_ALWAYS {
let _ = self.get_query::<Q>(DUMMY_SP, key);
return;
@ -784,7 +794,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
#[allow(dead_code)]
pub(super) fn force_query<Q: QueryDescription<'tcx> + 'tcx>(
pub(super) fn force_query<Q: QueryDescription<TyCtxt<'tcx>> + 'tcx>(
self,
key: Q::Key,
span: Span,
@ -920,7 +930,7 @@ macro_rules! define_queries_inner {
}
}
pub fn describe(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
pub fn describe(&self, tcx: TyCtxt<$tcx>) -> Cow<'static, str> {
let (r, name) = match *self {
$(Query::$name(key) => {
(queries::$name::describe(tcx, key), stringify!($name))

View File

@ -380,7 +380,7 @@ fn add_query_description_impl(
quote! {
#[allow(unused_variables)]
fn describe(
#tcx: TyCtxt<'_>,
#tcx: TyCtxt<'tcx>,
#key: #arg,
) -> Cow<'static, str> {
format!(#desc).into()
@ -393,7 +393,7 @@ fn add_query_description_impl(
let desc = desc.unwrap_or(quote! {});
impls.extend(quote! {
impl<'tcx> QueryDescription<'tcx> for queries::#name<'tcx> {
impl<'tcx> QueryDescription<TyCtxt<'tcx>> for queries::#name<'tcx> {
#desc
#cache
}