]> git.lizzy.rs Git - rust.git/commitdiff
Make QueryDescription parameter a type.
authorCamille GILLOT <gillot.camille@gmail.com>
Sun, 8 Mar 2020 18:42:11 +0000 (19:42 +0100)
committerCamille GILLOT <gillot.camille@gmail.com>
Thu, 26 Mar 2020 08:22:44 +0000 (09:22 +0100)
src/librustc/ty/query/config.rs
src/librustc/ty/query/on_disk_cache.rs
src/librustc/ty/query/plumbing.rs
src/librustc_macros/src/query.rs

index eaa1006791b565c152133943dcddac43c64fdabc..e65f3094820466c3d2bee65c637bbebfe099347d 100644 (file)
@@ -5,12 +5,12 @@
 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 @@ fn hash_result(hcx: &mut StableHashingContext<'_>, result: &Self::Value)
     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 @@ impl<'tcx, M> QueryDescription<'tcx> for M
         }
     }
 
-    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.")
     }
 }
index 155f792bd9d0ad92dde106a3e9f4a152f19ecd30..14839e6ad5000ff4f2168e3cba29c3db4e03393a 100644 (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
index 6506e1f83b43b57020ac2fc1a8f7f2750ea981df..c0f2c4a11bc2fd308a8fbed0f742beba6d74070c 100644 (file)
@@ -17,6 +17,8 @@
 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 @@ fn try_start<Q>(
         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 @@ enum TryGetJob<'tcx, C: QueryCache>
 
 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 @@ fn try_get_cached<C, R, OnHit, OnMiss>(
     }
 
     #[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 @@ pub(super) fn get_query<Q: QueryDescription<'tcx> + '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 @@ fn try_execute_query<Q: QueryDescription<'tcx> + '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 @@ fn load_from_disk_and_cache_in_memory<Q: QueryDescription<'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 @@ fn incremental_verify_ich<Q: QueryDescription<'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 @@ fn force_query_with_job<Q: QueryDescription<'tcx> + '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 @@ pub(super) fn ensure_query<Q: QueryDescription<'tcx> + 'tcx>(self, key: Q::Key)
     }
 
     #[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 @@ pub fn name(&self) -> &'static str {
                 }
             }
 
-            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))
index e7005f2f5ba775578e5fcd5ffa3c69f0d77246e9..57fe8ede9d197026822b5706bbe3d70d58b4ad5b 100644 (file)
@@ -380,7 +380,7 @@ fn cache_on_disk(
         quote! {
             #[allow(unused_variables)]
             fn describe(
-                #tcx: TyCtxt<'_>,
+                #tcx: TyCtxt<'tcx>,
                 #key: #arg,
             ) -> Cow<'static, str> {
                 format!(#desc).into()
@@ -393,7 +393,7 @@ fn describe(
         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
             }