about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-04-13 12:50:43 +0000
committerbors <bors@rust-lang.org>2020-04-13 12:50:43 +0000
commit5179ebe2064e15196c5be1f8df950736140b8fdd (patch)
treec315639b655f8bd1014db97b02364f12e89986a3
parentd28a46444eacf066ea0e7fdf6eda066e315aaa4a (diff)
parent04c91a0bba32d7c361b17ee8c5c0b3cc12c0d42b (diff)
downloadrust-5179ebe2064e15196c5be1f8df950736140b8fdd.tar.gz
rust-5179ebe2064e15196c5be1f8df950736140b8fdd.zip
Auto merge of #70961 - ecstatic-morse:into-def-id, r=eddyb
Take `impl Into<DefId>` for query methods on `TyCtxt`

Alternative implementation of #70956. cc #70853.
-rw-r--r--src/librustc_middle/ty/query/mod.rs28
-rw-r--r--src/librustc_middle/ty/query/plumbing.rs29
-rw-r--r--src/librustc_middle/ty/trait_def.rs2
-rw-r--r--src/librustc_typeck/check/wfcheck.rs2
4 files changed, 47 insertions, 14 deletions
diff --git a/src/librustc_middle/ty/query/mod.rs b/src/librustc_middle/ty/query/mod.rs
index 9986eb88dc3..899479e65a7 100644
--- a/src/librustc_middle/ty/query/mod.rs
+++ b/src/librustc_middle/ty/query/mod.rs
@@ -189,3 +189,31 @@ pub fn force_from_dep_node<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> bool
 pub(crate) fn try_load_from_on_disk_cache<'tcx>(tcx: TyCtxt<'tcx>, dep_node: &DepNode) {
     rustc_dep_node_try_load_from_on_disk_cache!(dep_node, tcx)
 }
+
+mod sealed {
+    use super::{DefId, LocalDefId};
+
+    /// An analogue of the `Into` trait that's intended only for query paramaters.
+    ///
+    /// This exists to allow queries to accept either `DefId` or `LocalDefId` while requiring that the
+    /// user call `to_def_id` to convert between them everywhere else.
+    pub trait IntoQueryParam<P> {
+        fn into_query_param(self) -> P;
+    }
+
+    impl<P> IntoQueryParam<P> for P {
+        #[inline(always)]
+        fn into_query_param(self) -> P {
+            self
+        }
+    }
+
+    impl IntoQueryParam<DefId> for LocalDefId {
+        #[inline(always)]
+        fn into_query_param(self) -> DefId {
+            self.to_def_id()
+        }
+    }
+}
+
+use sealed::IntoQueryParam;
diff --git a/src/librustc_middle/ty/query/plumbing.rs b/src/librustc_middle/ty/query/plumbing.rs
index 1bb392f436f..068322b08b7 100644
--- a/src/librustc_middle/ty/query/plumbing.rs
+++ b/src/librustc_middle/ty/query/plumbing.rs
@@ -234,18 +234,23 @@ macro_rules! hash_result {
 
 macro_rules! define_queries {
     (<$tcx:tt> $($category:tt {
-        $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*
+        $($(#[$attr:meta])* [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*
     },)*) => {
         define_queries_inner! { <$tcx>
-            $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($K) -> $V,)*)*
+            $($( $(#[$attr])* category<$category> [$($modifiers)*] fn $name: $node($($K)*) -> $V,)*)*
         }
     }
 }
 
+macro_rules! query_helper_param_ty {
+    (DefId) => { impl IntoQueryParam<DefId> };
+    ($K:ty) => { $K };
+}
+
 macro_rules! define_queries_inner {
     (<$tcx:tt>
      $($(#[$attr:meta])* category<$category:tt>
-        [$($modifiers:tt)*] fn $name:ident: $node:ident($K:ty) -> $V:ty,)*) => {
+        [$($modifiers:tt)*] fn $name:ident: $node:ident($($K:tt)*) -> $V:ty,)*) => {
 
         use std::mem;
         use crate::{
@@ -263,7 +268,7 @@ macro_rules! define_queries_inner {
         #[allow(nonstandard_style)]
         #[derive(Clone, Debug)]
         pub enum Query<$tcx> {
-            $($(#[$attr])* $name($K)),*
+            $($(#[$attr])* $name($($K)*)),*
         }
 
         impl<$tcx> Query<$tcx> {
@@ -321,7 +326,7 @@ macro_rules! define_queries_inner {
         }
 
         $(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
-            type Key = $K;
+            type Key = $($K)*;
             type Value = $V;
             const NAME: &'static str = stringify!($name);
             const CATEGORY: ProfileCategory = $category;
@@ -332,7 +337,7 @@ macro_rules! define_queries_inner {
             const EVAL_ALWAYS: bool = is_eval_always!([$($modifiers)*]);
             const DEP_KIND: dep_graph::DepKind = dep_graph::DepKind::$node;
 
-            type Cache = query_storage!([$($modifiers)*][$K, $V]);
+            type Cache = query_storage!([$($modifiers)*][$($K)*, $V]);
 
             #[inline(always)]
             fn query_state<'a>(tcx: TyCtxt<$tcx>) -> &'a QueryState<TyCtxt<$tcx>, Self::Cache> {
@@ -380,8 +385,8 @@ macro_rules! define_queries_inner {
         impl TyCtxtEnsure<$tcx> {
             $($(#[$attr])*
             #[inline(always)]
-            pub fn $name(self, key: $K) {
-                ensure_query::<queries::$name<'_>, _>(self.tcx, key)
+            pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
+                ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
             })*
         }
 
@@ -421,7 +426,7 @@ macro_rules! define_queries_inner {
 
             $($(#[$attr])*
             #[inline(always)]
-            pub fn $name(self, key: $K) -> $V {
+            pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
                 self.at(DUMMY_SP).$name(key)
             })*
 
@@ -458,14 +463,14 @@ macro_rules! define_queries_inner {
         impl TyCtxtAt<$tcx> {
             $($(#[$attr])*
             #[inline(always)]
-            pub fn $name(self, key: $K) -> $V {
-                get_query::<queries::$name<'_>, _>(self.tcx, self.span, key)
+            pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
+                get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
             })*
         }
 
         define_provider_struct! {
             tcx: $tcx,
-            input: ($(([$($modifiers)*] [$name] [$K] [$V]))*)
+            input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
         }
 
         impl<$tcx> Copy for Providers<$tcx> {}
diff --git a/src/librustc_middle/ty/trait_def.rs b/src/librustc_middle/ty/trait_def.rs
index ed9054fcffd..912f8be1d33 100644
--- a/src/librustc_middle/ty/trait_def.rs
+++ b/src/librustc_middle/ty/trait_def.rs
@@ -193,7 +193,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> &Trai
     let mut impls = TraitImpls::default();
 
     {
-        let mut add_impl = |impl_def_id| {
+        let mut add_impl = |impl_def_id: DefId| {
             let impl_self_ty = tcx.type_of(impl_def_id);
             if impl_def_id.is_local() && impl_self_ty.references_error() {
                 return;
diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs
index 5bfad558b6b..6b6e2bb329f 100644
--- a/src/librustc_typeck/check/wfcheck.rs
+++ b/src/librustc_typeck/check/wfcheck.rs
@@ -335,7 +335,7 @@ fn for_id(tcx: TyCtxt<'_>, id: hir::HirId, span: Span) -> CheckWfFcxBuilder<'_>
         inherited: Inherited::build(tcx, def_id),
         id,
         span,
-        param_env: tcx.param_env(def_id.to_def_id()),
+        param_env: tcx.param_env(def_id),
     }
 }