Skip to content

Commit 39f558f

Browse files
authored
Rollup merge of #113412 - spastorino:smir-types-1, r=oli-obk
Add basic types to SMIR Still incomplete but I think this can be merged and we can keep iterating over it. r? ``@oli-obk``
2 parents 8dc9461 + 9ca51b9 commit 39f558f

File tree

3 files changed

+99
-9
lines changed

3 files changed

+99
-9
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10-
use crate::stable_mir::{self, ty::TyKind, Context};
10+
use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy};
11+
use crate::stable_mir::{self, Context};
1112
use rustc_middle::mir;
1213
use rustc_middle::ty::{self, Ty, TyCtxt};
1314
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
@@ -69,11 +70,28 @@ pub struct Tables<'tcx> {
6970
impl<'tcx> Tables<'tcx> {
7071
fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind {
7172
match ty.kind() {
72-
ty::Bool => TyKind::Bool,
73-
ty::Char => todo!(),
74-
ty::Int(_) => todo!(),
75-
ty::Uint(_) => todo!(),
76-
ty::Float(_) => todo!(),
73+
ty::Bool => TyKind::RigidTy(RigidTy::Bool),
74+
ty::Char => TyKind::RigidTy(RigidTy::Char),
75+
ty::Int(int_ty) => match int_ty {
76+
ty::IntTy::Isize => TyKind::RigidTy(RigidTy::Int(IntTy::Isize)),
77+
ty::IntTy::I8 => TyKind::RigidTy(RigidTy::Int(IntTy::I8)),
78+
ty::IntTy::I16 => TyKind::RigidTy(RigidTy::Int(IntTy::I16)),
79+
ty::IntTy::I32 => TyKind::RigidTy(RigidTy::Int(IntTy::I32)),
80+
ty::IntTy::I64 => TyKind::RigidTy(RigidTy::Int(IntTy::I64)),
81+
ty::IntTy::I128 => TyKind::RigidTy(RigidTy::Int(IntTy::I128)),
82+
},
83+
ty::Uint(uint_ty) => match uint_ty {
84+
ty::UintTy::Usize => TyKind::RigidTy(RigidTy::Uint(UintTy::Usize)),
85+
ty::UintTy::U8 => TyKind::RigidTy(RigidTy::Uint(UintTy::U8)),
86+
ty::UintTy::U16 => TyKind::RigidTy(RigidTy::Uint(UintTy::U16)),
87+
ty::UintTy::U32 => TyKind::RigidTy(RigidTy::Uint(UintTy::U32)),
88+
ty::UintTy::U64 => TyKind::RigidTy(RigidTy::Uint(UintTy::U64)),
89+
ty::UintTy::U128 => TyKind::RigidTy(RigidTy::Uint(UintTy::U128)),
90+
},
91+
ty::Float(float_ty) => match float_ty {
92+
ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)),
93+
ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)),
94+
},
7795
ty::Adt(_, _) => todo!(),
7896
ty::Foreign(_) => todo!(),
7997
ty::Str => todo!(),
@@ -90,9 +108,9 @@ impl<'tcx> Tables<'tcx> {
90108
ty::GeneratorWitness(_) => todo!(),
91109
ty::GeneratorWitnessMIR(_, _) => todo!(),
92110
ty::Never => todo!(),
93-
ty::Tuple(fields) => {
94-
TyKind::Tuple(fields.iter().map(|ty| self.intern_ty(ty)).collect())
95-
}
111+
ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple(
112+
fields.iter().map(|ty| self.intern_ty(ty)).collect(),
113+
)),
96114
ty::Alias(_, _) => todo!(),
97115
ty::Param(_) => todo!(),
98116
ty::Bound(_, _) => todo!(),

compiler/rustc_smir/src/stable_mir/ty.rs

+36
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,43 @@ impl Ty {
99
}
1010
}
1111

12+
#[derive(Clone, Debug)]
1213
pub enum TyKind {
14+
RigidTy(RigidTy),
15+
}
16+
17+
#[derive(Clone, Debug)]
18+
pub enum RigidTy {
1319
Bool,
20+
Char,
21+
Int(IntTy),
22+
Uint(UintTy),
23+
Float(FloatTy),
1424
Tuple(Vec<Ty>),
1525
}
26+
27+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28+
pub enum IntTy {
29+
Isize,
30+
I8,
31+
I16,
32+
I32,
33+
I64,
34+
I128,
35+
}
36+
37+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38+
pub enum UintTy {
39+
Usize,
40+
U8,
41+
U16,
42+
U32,
43+
U64,
44+
U128,
45+
}
46+
47+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48+
pub enum FloatTy {
49+
F32,
50+
F64,
51+
}

tests/ui-fulldeps/stable-mir/crate-info.rs

+36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// edition: 2021
88

99
#![feature(rustc_private)]
10+
#![feature(assert_matches)]
1011

1112
extern crate rustc_driver;
1213
extern crate rustc_hir;
@@ -21,6 +22,7 @@ use rustc_interface::{interface, Queries};
2122
use rustc_middle::ty::TyCtxt;
2223
use rustc_session::EarlyErrorHandler;
2324
use rustc_smir::{rustc_internal, stable_mir};
25+
use std::assert_matches::assert_matches;
2426
use std::io::Write;
2527

2628
const CRATE_NAME: &str = "input";
@@ -65,6 +67,36 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
6567
other => panic!("{other:?}"),
6668
}
6769

70+
let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap();
71+
let body = types.body();
72+
assert_eq!(body.locals.len(), 6);
73+
assert_matches!(
74+
body.locals[0].kind(),
75+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool)
76+
);
77+
assert_matches!(
78+
body.locals[1].kind(),
79+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool)
80+
);
81+
assert_matches!(
82+
body.locals[2].kind(),
83+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Char)
84+
);
85+
assert_matches!(
86+
body.locals[3].kind(),
87+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Int(stable_mir::ty::IntTy::I32))
88+
);
89+
assert_matches!(
90+
body.locals[4].kind(),
91+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Uint(stable_mir::ty::UintTy::U64))
92+
);
93+
assert_matches!(
94+
body.locals[5].kind(),
95+
stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Float(
96+
stable_mir::ty::FloatTy::F64
97+
))
98+
);
99+
68100
let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap();
69101
let body = drop.body();
70102
assert_eq!(body.blocks.len(), 2);
@@ -156,6 +188,10 @@ fn generate_input(path: &str) -> std::io::Result<()> {
156188
x_64.wrapping_add(y_64)
157189
}}
158190
191+
pub fn types(b: bool, _: char, _: i32, _: u64, _: f64) -> bool {{
192+
b
193+
}}
194+
159195
pub fn drop(_: String) {{}}
160196
161197
pub fn assert(x: i32) -> i32 {{

0 commit comments

Comments
 (0)