From c5c38cdee80e42c374a0afddeb843851abfd105b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 18 Jul 2023 12:19:41 -0300 Subject: [PATCH] Add Closure ty to SMIR --- compiler/rustc_smir/src/rustc_internal/mod.rs | 8 ++++++++ compiler/rustc_smir/src/rustc_smir/mod.rs | 20 ++++++++++++++++++- compiler/rustc_smir/src/stable_mir/ty.rs | 4 ++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index e4f129bc1d6..d32215186a6 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -39,6 +39,10 @@ pub fn fn_def(did: DefId) -> stable_mir::ty::FnDef { with_tables(|t| t.fn_def(did)) } +pub fn closure_def(did: DefId) -> stable_mir::ty::ClosureDef { + with_tables(|t| t.closure_def(did)) +} + impl<'tcx> Tables<'tcx> { pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId { self.def_ids[item.0] @@ -60,6 +64,10 @@ pub fn fn_def(&mut self, did: DefId) -> stable_mir::ty::FnDef { stable_mir::ty::FnDef(self.create_def_id(did)) } + pub fn closure_def(&mut self, did: DefId) -> stable_mir::ty::ClosureDef { + stable_mir::ty::ClosureDef(self.create_def_id(did)) + } + fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId { // FIXME: this becomes inefficient when we have too many ids for (i, &d) in self.def_ids.iter().enumerate() { diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 81ca0f985cc..b542cc84f6a 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -148,7 +148,25 @@ fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind { )), ty::FnPtr(_) => todo!(), ty::Dynamic(_, _, _) => todo!(), - ty::Closure(_, _) => todo!(), + ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure( + rustc_internal::closure_def(*def_id), + GenericArgs( + generic_args + .iter() + .map(|arg| match arg.unpack() { + ty::GenericArgKind::Lifetime(region) => { + GenericArgKind::Lifetime(opaque(®ion)) + } + ty::GenericArgKind::Type(ty) => { + GenericArgKind::Type(self.intern_ty(ty)) + } + ty::GenericArgKind::Const(const_) => { + GenericArgKind::Const(opaque(&const_)) + } + }) + .collect(), + ), + )), ty::Generator(_, _, _) => todo!(), ty::Never => TyKind::RigidTy(RigidTy::Never), ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple( diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index f3dbde907a0..758e0bb7de7 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -33,6 +33,7 @@ pub enum RigidTy { RawPtr(Ty, Mutability), Ref(Region, Ty, Mutability), FnDef(FnDef, GenericArgs), + Closure(ClosureDef, GenericArgs), Never, Tuple(Vec), } @@ -69,6 +70,9 @@ pub enum FloatTy { #[derive(Clone, PartialEq, Eq, Debug)] pub struct FnDef(pub(crate) DefId); +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct ClosureDef(pub(crate) DefId); + #[derive(Clone, PartialEq, Eq, Debug)] pub struct AdtDef(pub(crate) DefId);