From 5cf570f3256f69bf1dfe7f999c2802753dda84d5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 11 Jul 2023 17:16:44 -0300 Subject: [PATCH 1/2] DefIds are not only about CrateItem --- compiler/rustc_smir/src/rustc_smir/mod.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index f22c620021e..cd40ddab3fe 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -147,13 +147,6 @@ pub(crate) trait Stable { fn stable(&self) -> Self::T; } -impl Stable for DefId { - type T = stable_mir::CrateItem; - fn stable(&self) -> Self::T { - rustc_internal::crate_item(*self) - } -} - impl<'tcx> Stable for mir::Statement<'tcx> { type T = stable_mir::mir::Statement; fn stable(&self) -> Self::T { @@ -188,7 +181,9 @@ fn stable(&self) -> Self::T { Ref(region, kind, place) => { stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable()) } - ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()), + ThreadLocalRef(def_id) => { + stable_mir::mir::Rvalue::ThreadLocalRef(rustc_internal::crate_item(*def_id)) + } AddressOf(mutability, place) => { stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable()) } From c80a0f3178d30207ee10faa32e6fab273d19c25f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 12 Jul 2023 16:24:33 -0300 Subject: [PATCH 2/2] Add Adt to SMIR --- compiler/rustc_smir/src/rustc_internal/mod.rs | 16 ++++++++++++-- compiler/rustc_smir/src/rustc_smir/mod.rs | 22 +++++++++++++++++-- compiler/rustc_smir/src/stable_mir/ty.rs | 22 ++++++++++++++++++- 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs index 87e0b211556..527d5220564 100644 --- a/compiler/rustc_smir/src/rustc_internal/mod.rs +++ b/compiler/rustc_smir/src/rustc_internal/mod.rs @@ -27,21 +27,33 @@ pub fn crate_item(did: DefId) -> stable_mir::CrateItem { with_tables(|t| t.crate_item(did)) } +pub fn adt_def(did: DefId) -> stable_mir::ty::AdtDef { + with_tables(|t| t.adt_def(did)) +} + impl<'tcx> Tables<'tcx> { pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId { self.def_ids[item.0] } pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem { + stable_mir::CrateItem(self.create_def_id(did)) + } + + pub fn adt_def(&mut self, did: DefId) -> stable_mir::ty::AdtDef { + stable_mir::ty::AdtDef(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() { if d == did { - return stable_mir::CrateItem(i); + return i; } } let id = self.def_ids.len(); self.def_ids.push(did); - stable_mir::CrateItem(id) + id } } diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index cd40ddab3fe..2e150166239 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -8,7 +8,7 @@ //! For now, we are developing everything inside `rustc`, thus, we keep this module private. use crate::rustc_internal::{self, opaque}; -use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy}; +use crate::stable_mir::ty::{AdtSubsts, FloatTy, GenericArgKind, IntTy, RigidTy, TyKind, UintTy}; use crate::stable_mir::{self, Context}; use rustc_middle::mir; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -94,7 +94,25 @@ fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind { ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)), ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)), }, - ty::Adt(_, _) => todo!(), + ty::Adt(adt_def, substs) => TyKind::RigidTy(RigidTy::Adt( + rustc_internal::adt_def(adt_def.did()), + AdtSubsts( + substs + .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::Foreign(_) => todo!(), ty::Str => todo!(), ty::Array(_, _) => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 3181af46e9c..389e3364117 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -1,4 +1,5 @@ -use super::with; +use super::{with, DefId}; +use crate::rustc_internal::Opaque; #[derive(Copy, Clone, Debug)] pub struct Ty(pub usize); @@ -9,6 +10,9 @@ pub fn kind(&self) -> TyKind { } } +type Const = Opaque; +type Region = Opaque; + #[derive(Clone, Debug)] pub enum TyKind { RigidTy(RigidTy), @@ -21,6 +25,7 @@ pub enum RigidTy { Int(IntTy), Uint(UintTy), Float(FloatTy), + Adt(AdtDef, AdtSubsts), Tuple(Vec), } @@ -49,3 +54,18 @@ pub enum FloatTy { F32, F64, } + +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct AdtDef(pub(crate) DefId); + +#[derive(Clone, Debug)] +pub struct AdtSubsts(pub Vec); + +#[derive(Clone, Debug)] +pub enum GenericArgKind { + // FIXME add proper region + Lifetime(Region), + Type(Ty), + // FIXME add proper const + Const(Const), +}