Skip to content

Commit 92f20fe

Browse files
authored
Rollup merge of rust-lang#113629 - spastorino:smir-types-3, r=oli-obk
Add Adt to SMIR r? ``@oli-obk``
2 parents 541482a + c80a0f3 commit 92f20fe

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,33 @@ pub fn crate_item(did: DefId) -> stable_mir::CrateItem {
2727
with_tables(|t| t.crate_item(did))
2828
}
2929

30+
pub fn adt_def(did: DefId) -> stable_mir::ty::AdtDef {
31+
with_tables(|t| t.adt_def(did))
32+
}
33+
3034
impl<'tcx> Tables<'tcx> {
3135
pub fn item_def_id(&self, item: &stable_mir::CrateItem) -> DefId {
3236
self.def_ids[item.0]
3337
}
3438

3539
pub fn crate_item(&mut self, did: DefId) -> stable_mir::CrateItem {
40+
stable_mir::CrateItem(self.create_def_id(did))
41+
}
42+
43+
pub fn adt_def(&mut self, did: DefId) -> stable_mir::ty::AdtDef {
44+
stable_mir::ty::AdtDef(self.create_def_id(did))
45+
}
46+
47+
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
3648
// FIXME: this becomes inefficient when we have too many ids
3749
for (i, &d) in self.def_ids.iter().enumerate() {
3850
if d == did {
39-
return stable_mir::CrateItem(i);
51+
return i;
4052
}
4153
}
4254
let id = self.def_ids.len();
4355
self.def_ids.push(did);
44-
stable_mir::CrateItem(id)
56+
id
4557
}
4658
}
4759

compiler/rustc_smir/src/rustc_smir/mod.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
1010
use crate::rustc_internal::{self, opaque};
11-
use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy};
11+
use crate::stable_mir::ty::{AdtSubsts, FloatTy, GenericArgKind, IntTy, RigidTy, TyKind, UintTy};
1212
use crate::stable_mir::{self, Context};
1313
use rustc_middle::mir;
1414
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -94,7 +94,25 @@ impl<'tcx> Tables<'tcx> {
9494
ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)),
9595
ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)),
9696
},
97-
ty::Adt(_, _) => todo!(),
97+
ty::Adt(adt_def, substs) => TyKind::RigidTy(RigidTy::Adt(
98+
rustc_internal::adt_def(adt_def.did()),
99+
AdtSubsts(
100+
substs
101+
.iter()
102+
.map(|arg| match arg.unpack() {
103+
ty::GenericArgKind::Lifetime(region) => {
104+
GenericArgKind::Lifetime(opaque(&region))
105+
}
106+
ty::GenericArgKind::Type(ty) => {
107+
GenericArgKind::Type(self.intern_ty(ty))
108+
}
109+
ty::GenericArgKind::Const(const_) => {
110+
GenericArgKind::Const(opaque(&const_))
111+
}
112+
})
113+
.collect(),
114+
),
115+
)),
98116
ty::Foreign(_) => todo!(),
99117
ty::Str => todo!(),
100118
ty::Array(_, _) => todo!(),
@@ -149,13 +167,6 @@ pub(crate) trait Stable {
149167
fn stable(&self) -> Self::T;
150168
}
151169

152-
impl Stable for DefId {
153-
type T = stable_mir::CrateItem;
154-
fn stable(&self) -> Self::T {
155-
rustc_internal::crate_item(*self)
156-
}
157-
}
158-
159170
impl<'tcx> Stable for mir::Statement<'tcx> {
160171
type T = stable_mir::mir::Statement;
161172
fn stable(&self) -> Self::T {
@@ -190,7 +201,9 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
190201
Ref(region, kind, place) => {
191202
stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable())
192203
}
193-
ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()),
204+
ThreadLocalRef(def_id) => {
205+
stable_mir::mir::Rvalue::ThreadLocalRef(rustc_internal::crate_item(*def_id))
206+
}
194207
AddressOf(mutability, place) => {
195208
stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable())
196209
}

compiler/rustc_smir/src/stable_mir/ty.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use super::with;
1+
use super::{with, DefId};
2+
use crate::rustc_internal::Opaque;
23

34
#[derive(Copy, Clone, Debug)]
45
pub struct Ty(pub usize);
@@ -9,6 +10,9 @@ impl Ty {
910
}
1011
}
1112

13+
type Const = Opaque;
14+
type Region = Opaque;
15+
1216
#[derive(Clone, Debug)]
1317
pub enum TyKind {
1418
RigidTy(RigidTy),
@@ -21,6 +25,7 @@ pub enum RigidTy {
2125
Int(IntTy),
2226
Uint(UintTy),
2327
Float(FloatTy),
28+
Adt(AdtDef, AdtSubsts),
2429
Tuple(Vec<Ty>),
2530
}
2631

@@ -49,3 +54,18 @@ pub enum FloatTy {
4954
F32,
5055
F64,
5156
}
57+
58+
#[derive(Clone, PartialEq, Eq, Debug)]
59+
pub struct AdtDef(pub(crate) DefId);
60+
61+
#[derive(Clone, Debug)]
62+
pub struct AdtSubsts(pub Vec<GenericArgKind>);
63+
64+
#[derive(Clone, Debug)]
65+
pub enum GenericArgKind {
66+
// FIXME add proper region
67+
Lifetime(Region),
68+
Type(Ty),
69+
// FIXME add proper const
70+
Const(Const),
71+
}

0 commit comments

Comments
 (0)