Skip to content

Commit c5c38cd

Browse files
committed
Add Closure ty to SMIR
1 parent e5c0b96 commit c5c38cd

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ pub fn fn_def(did: DefId) -> stable_mir::ty::FnDef {
3939
with_tables(|t| t.fn_def(did))
4040
}
4141

42+
pub fn closure_def(did: DefId) -> stable_mir::ty::ClosureDef {
43+
with_tables(|t| t.closure_def(did))
44+
}
45+
4246
impl<'tcx> Tables<'tcx> {
4347
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
4448
self.def_ids[item.0]
@@ -60,6 +64,10 @@ impl<'tcx> Tables<'tcx> {
6064
stable_mir::ty::FnDef(self.create_def_id(did))
6165
}
6266

67+
pub fn closure_def(&mut self, did: DefId) -> stable_mir::ty::ClosureDef {
68+
stable_mir::ty::ClosureDef(self.create_def_id(did))
69+
}
70+
6371
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
6472
// FIXME: this becomes inefficient when we have too many ids
6573
for (i, &d) in self.def_ids.iter().enumerate() {

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,25 @@ impl<'tcx> Tables<'tcx> {
148148
)),
149149
ty::FnPtr(_) => todo!(),
150150
ty::Dynamic(_, _, _) => todo!(),
151-
ty::Closure(_, _) => todo!(),
151+
ty::Closure(def_id, generic_args) => TyKind::RigidTy(RigidTy::Closure(
152+
rustc_internal::closure_def(*def_id),
153+
GenericArgs(
154+
generic_args
155+
.iter()
156+
.map(|arg| match arg.unpack() {
157+
ty::GenericArgKind::Lifetime(region) => {
158+
GenericArgKind::Lifetime(opaque(&region))
159+
}
160+
ty::GenericArgKind::Type(ty) => {
161+
GenericArgKind::Type(self.intern_ty(ty))
162+
}
163+
ty::GenericArgKind::Const(const_) => {
164+
GenericArgKind::Const(opaque(&const_))
165+
}
166+
})
167+
.collect(),
168+
),
169+
)),
152170
ty::Generator(_, _, _) => todo!(),
153171
ty::Never => TyKind::RigidTy(RigidTy::Never),
154172
ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(

compiler/rustc_smir/src/stable_mir/ty.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum RigidTy {
3333
RawPtr(Ty, Mutability),
3434
Ref(Region, Ty, Mutability),
3535
FnDef(FnDef, GenericArgs),
36+
Closure(ClosureDef, GenericArgs),
3637
Never,
3738
Tuple(Vec<Ty>),
3839
}
@@ -69,6 +70,9 @@ pub struct ForeignDef(pub(crate) DefId);
6970
#[derive(Clone, PartialEq, Eq, Debug)]
7071
pub struct FnDef(pub(crate) DefId);
7172

73+
#[derive(Clone, PartialEq, Eq, Debug)]
74+
pub struct ClosureDef(pub(crate) DefId);
75+
7276
#[derive(Clone, PartialEq, Eq, Debug)]
7377
pub struct AdtDef(pub(crate) DefId);
7478

0 commit comments

Comments
 (0)