From 742f193ef871aff89a5d818cdb1087f41c66f75d Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Thu, 14 Dec 2023 19:10:03 +0000 Subject: Move special methods from ClosureKind back into rustc --- compiler/rustc_middle/src/middle/lang_items.rs | 11 +++++++++++ compiler/rustc_middle/src/ty/context.rs | 24 ------------------------ compiler/rustc_middle/src/ty/sty.rs | 9 +++++++++ 3 files changed, 20 insertions(+), 24 deletions(-) (limited to 'compiler/rustc_middle/src') 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 { + 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, ()>; 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 -- cgit 1.4.1-3-g733a5