about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2023-12-14 19:10:03 +0000
committerMichael Goulet <michael@errs.io>2023-12-14 19:10:03 +0000
commit742f193ef871aff89a5d818cdb1087f41c66f75d (patch)
tree78bdf181e975814988653361c64aeb3fc9f4ebeb
parent9f0849f9e059648ce18b996110276306863c5a6d (diff)
downloadrust-742f193ef871aff89a5d818cdb1087f41c66f75d.tar.gz
rust-742f193ef871aff89a5d818cdb1087f41c66f75d.zip
Move special methods from ClosureKind back into rustc
-rw-r--r--compiler/rustc_hir_typeck/src/closure.rs2
-rw-r--r--compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs2
-rw-r--r--compiler/rustc_hir_typeck/src/upvar.rs6
-rw-r--r--compiler/rustc_middle/src/middle/lang_items.rs11
-rw-r--r--compiler/rustc_middle/src/ty/context.rs24
-rw-r--r--compiler/rustc_middle/src/ty/sty.rs9
-rw-r--r--compiler/rustc_type_ir/src/interner.rs9
-rw-r--r--compiler/rustc_type_ir/src/lib.rs21
8 files changed, 27 insertions, 57 deletions
diff --git a/compiler/rustc_hir_typeck/src/closure.rs b/compiler/rustc_hir_typeck/src/closure.rs
index f0bb18df48c..4662ee5825e 100644
--- a/compiler/rustc_hir_typeck/src/closure.rs
+++ b/compiler/rustc_hir_typeck/src/closure.rs
@@ -141,7 +141,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         debug!(?sig, ?opt_kind);
 
         let closure_kind_ty = match opt_kind {
-            Some(kind) => kind.to_ty(self.tcx),
+            Some(kind) => Ty::from_closure_kind(self.tcx, kind),
 
             // Create a type variable (for now) to represent the closure kind.
             // It will be unified during the upvar inference phase (`upvar.rs`)
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
index bb9b849f03b..9ba61335e68 100644
--- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
+++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
@@ -2018,7 +2018,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     let new_def_id = self.probe(|_| {
                         let trait_ref = ty::TraitRef::new(
                             self.tcx,
-                            call_kind.to_def_id(self.tcx),
+                            self.tcx.fn_trait_kind_to_def_id(call_kind)?,
                             [
                                 callee_ty,
                                 self.next_ty_var(TypeVariableOrigin {
diff --git a/compiler/rustc_hir_typeck/src/upvar.rs b/compiler/rustc_hir_typeck/src/upvar.rs
index c0a5818b9e5..ebdd74fbc38 100644
--- a/compiler/rustc_hir_typeck/src/upvar.rs
+++ b/compiler/rustc_hir_typeck/src/upvar.rs
@@ -261,7 +261,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             // Unify the (as yet unbound) type variable in the closure
             // args with the kind we inferred.
             let closure_kind_ty = closure_args.as_closure().kind_ty();
-            self.demand_eqtype(span, closure_kind.to_ty(self.tcx), closure_kind_ty);
+            self.demand_eqtype(
+                span,
+                Ty::from_closure_kind(self.tcx, closure_kind),
+                closure_kind_ty,
+            );
 
             // If we have an origin, store it.
             if let Some(mut origin) = origin {
diff --git a/compiler/rustc_middle/src/middle/lang_items.rs b/compiler/rustc_middle/src/middle/lang_items.rs
index 9a633e04ce7..2899e629d79 100644
--- a/compiler/rustc_middle/src/middle/lang_items.rs
+++ b/compiler/rustc_middle/src/middle/lang_items.rs
@@ -36,6 +36,17 @@ impl<'tcx> TyCtxt<'tcx> {
         }
     }
 
+    /// Given a [`ty::ClosureKind`], get the [`DefId`] of its corresponding `Fn`-family
+    /// trait, if it is defined.
+    pub fn fn_trait_kind_to_def_id(self, kind: ty::ClosureKind) -> Option<DefId> {
+        let items = self.lang_items();
+        match kind {
+            ty::ClosureKind::Fn => items.fn_trait(),
+            ty::ClosureKind::FnMut => items.fn_mut_trait(),
+            ty::ClosureKind::FnOnce => items.fn_once_trait(),
+        }
+    }
+
     /// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits.
     pub fn is_fn_trait(self, id: DefId) -> bool {
         self.fn_trait_kind_from_def_id(id).is_some()
diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs
index ea9dc76d3cb..fd3058472bb 100644
--- a/compiler/rustc_middle/src/ty/context.rs
+++ b/compiler/rustc_middle/src/ty/context.rs
@@ -151,30 +151,6 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
     ) -> Self::Const {
         Const::new_bound(self, debruijn, var, ty)
     }
-
-    fn fn_def_id(self) -> Self::DefId {
-        self.require_lang_item(hir::LangItem::Fn, None)
-    }
-
-    fn fn_mut_def_id(self) -> Self::DefId {
-        self.require_lang_item(hir::LangItem::FnMut, None)
-    }
-
-    fn fn_once_def_id(self) -> Self::DefId {
-        self.require_lang_item(hir::LangItem::FnOnce, None)
-    }
-
-    fn i8_type(self) -> Self::Ty {
-        self.types.i8
-    }
-
-    fn i16_type(self) -> Self::Ty {
-        self.types.i16
-    }
-
-    fn i32_type(self) -> Self::Ty {
-        self.types.i32
-    }
 }
 
 type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs
index 42753f53c7b..09c90d80e97 100644
--- a/compiler/rustc_middle/src/ty/sty.rs
+++ b/compiler/rustc_middle/src/ty/sty.rs
@@ -2811,6 +2811,15 @@ impl<'tcx> Ty<'tcx> {
         }
     }
 
+    /// Inverse of [`Ty::to_opt_closure_kind`].
+    pub fn from_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
+        match kind {
+            ty::ClosureKind::Fn => tcx.types.i8,
+            ty::ClosureKind::FnMut => tcx.types.i16,
+            ty::ClosureKind::FnOnce => tcx.types.i32,
+        }
+    }
+
     /// Fast path helper for testing if a type is `Sized`.
     ///
     /// Returning true means the type is known to be sized. Returning
diff --git a/compiler/rustc_type_ir/src/interner.rs b/compiler/rustc_type_ir/src/interner.rs
index 0eccd2b4cf3..188910ecc52 100644
--- a/compiler/rustc_type_ir/src/interner.rs
+++ b/compiler/rustc_type_ir/src/interner.rs
@@ -86,15 +86,6 @@ pub trait Interner: Sized {
     fn mk_bound_ty(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Ty;
     fn mk_bound_region(self, debruijn: DebruijnIndex, var: BoundVar) -> Self::Region;
     fn mk_bound_const(self, debruijn: DebruijnIndex, var: BoundVar, ty: Self::Ty) -> Self::Const;
-
-    // FIXME: these could be consolidated into some "WellKnownTraits" thing like chalk does.
-    fn fn_def_id(self) -> Self::DefId;
-    fn fn_mut_def_id(self) -> Self::DefId;
-    fn fn_once_def_id(self) -> Self::DefId;
-
-    fn i8_type(self) -> Self::Ty;
-    fn i16_type(self) -> Self::Ty;
-    fn i32_type(self) -> Self::Ty;
 }
 
 /// Common capabilities of placeholder kinds
diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs
index f827969fb76..bff93859645 100644
--- a/compiler/rustc_type_ir/src/lib.rs
+++ b/compiler/rustc_type_ir/src/lib.rs
@@ -394,27 +394,6 @@ impl ClosureKind {
     pub fn extends(self, other: ClosureKind) -> bool {
         self <= other
     }
-
-    /// Converts `self` to a `DefId` of the corresponding trait.
-    ///
-    /// Note: the inverse of this function is `TyCtxt::fn_trait_kind_from_def_id`.
-    pub fn to_def_id<I: Interner>(self, interner: I) -> I::DefId {
-        match self {
-            ClosureKind::Fn => interner.fn_def_id(),
-            ClosureKind::FnMut => interner.fn_mut_def_id(),
-            ClosureKind::FnOnce => interner.fn_once_def_id(),
-        }
-    }
-
-    /// Returns the representative scalar type for this closure kind.
-    /// See `Ty::to_opt_closure_kind` for more details.
-    pub fn to_ty<I: Interner>(self, interner: I) -> I::Ty {
-        match self {
-            ClosureKind::Fn => interner.i8_type(),
-            ClosureKind::FnMut => interner.i16_type(),
-            ClosureKind::FnOnce => interner.i32_type(),
-        }
-    }
 }
 
 impl fmt::Display for ClosureKind {