]> git.lizzy.rs Git - rust.git/commitdiff
Use custom trait instead of `Into`
authorDylan MacKenzie <ecstaticmorse@gmail.com>
Fri, 10 Apr 2020 20:54:06 +0000 (13:54 -0700)
committerDylan MacKenzie <ecstaticmorse@gmail.com>
Sat, 11 Apr 2020 22:18:51 +0000 (15:18 -0700)
src/librustc_middle/ty/query/mod.rs
src/librustc_middle/ty/query/plumbing.rs

index 9986eb88dc32681e20f9f27c4d59362e6fa19577..9f04cff4d2fb9d591c917b463c441b67440cf02f 100644 (file)
@@ -189,3 +189,23 @@ 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)
 }
+
+/// 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 {
+    fn into_query_param(self) -> P {
+        self
+    }
+}
+
+impl IntoQueryParam<DefId> for LocalDefId {
+    fn into_query_param(self) -> DefId {
+        self.to_def_id()
+    }
+}
index f3a49438f5b2fc5db5aa31faf5abe273eb80fc70..068322b08b7a31a4eef5c1b8350aacacf31a4577 100644 (file)
@@ -243,7 +243,7 @@ macro_rules! define_queries {
 }
 
 macro_rules! query_helper_param_ty {
-    (DefId) => { impl Into<DefId> };
+    (DefId) => { impl IntoQueryParam<DefId> };
     ($K:ty) => { $K };
 }
 
@@ -386,7 +386,7 @@ impl TyCtxtEnsure<$tcx> {
             $($(#[$attr])*
             #[inline(always)]
             pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
-                ensure_query::<queries::$name<'_>, _>(self.tcx, key.into())
+                ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
             })*
         }
 
@@ -464,7 +464,7 @@ impl TyCtxtAt<$tcx> {
             $($(#[$attr])*
             #[inline(always)]
             pub fn $name(self, key: query_helper_param_ty!($($K)*)) -> $V {
-                get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into())
+                get_query::<queries::$name<'_>, _>(self.tcx, self.span, key.into_query_param())
             })*
         }