Skip to content

Commit b9f378b

Browse files
committed
Implement a few more rvalue translation to smir
- Introduce an Opaque type for adding information that is still internal to the compiler.
1 parent 6975632 commit b9f378b

File tree

5 files changed

+318
-10
lines changed

5 files changed

+318
-10
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -4234,8 +4234,8 @@ dependencies = [
42344234
"rustc_hir",
42354235
"rustc_middle",
42364236
"rustc_span",
4237-
"scoped-tls",
42384237
"rustc_target",
4238+
"scoped-tls",
42394239
"tracing",
42404240
]
42414241

compiler/rustc_smir/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#![cfg_attr(not(feature = "default"), feature(rustc_private))]
1414
#![feature(local_key_cell_methods)]
1515
#![feature(ptr_metadata)]
16+
#![feature(type_alias_impl_trait)] // Used to define opaque types.
1617

1718
// Declare extern rustc_* crates to enable building this crate separately from the compiler.
1819
#[cfg(not(feature = "default"))]

compiler/rustc_smir/src/rustc_internal/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
//! For that, we define APIs that will temporarily be public to 3P that exposes rustc internal APIs
44
//! until stable MIR is complete.
55
6+
use std::fmt::Debug;
7+
use std::string::ToString;
8+
69
use crate::{
710
rustc_smir::Tables,
811
stable_mir::{self, with},
@@ -49,3 +52,10 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
4952
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
5053
crate::stable_mir::run(Tables { tcx, def_ids: vec![], types: vec![] }, f);
5154
}
55+
56+
/// A type that provides internal information but that can still be used for debug purpose.
57+
pub type Opaque = impl Debug + ToString + Clone;
58+
59+
pub(crate) fn opaque<T: Debug>(value: &T) -> Opaque {
60+
format!("{value:?}")
61+
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+127-7
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
//!
88
//! For now, we are developing everything inside `rustc`, thus, we keep this module private.
99
10+
use crate::rustc_internal::{self, opaque};
1011
use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy};
1112
use crate::stable_mir::{self, Context};
1213
use rustc_middle::mir;
1314
use rustc_middle::ty::{self, Ty, TyCtxt};
1415
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
16+
use rustc_target::abi::FieldIdx;
1517
use tracing::debug;
1618

1719
impl<'tcx> Context for Tables<'tcx> {
@@ -145,6 +147,13 @@ pub(crate) trait Stable {
145147
fn stable(&self) -> Self::T;
146148
}
147149

150+
impl Stable for DefId {
151+
type T = stable_mir::CrateItem;
152+
fn stable(&self) -> Self::T {
153+
rustc_internal::crate_item(*self)
154+
}
155+
}
156+
148157
impl<'tcx> Stable for mir::Statement<'tcx> {
149158
type T = stable_mir::mir::Statement;
150159
fn stable(&self) -> Self::T {
@@ -176,27 +185,138 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
176185
match self {
177186
Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
178187
Repeat(_, _) => todo!(),
179-
Ref(_, _, _) => todo!(),
180-
ThreadLocalRef(_) => todo!(),
181-
AddressOf(_, _) => todo!(),
182-
Len(_) => todo!(),
188+
Ref(region, kind, place) => {
189+
stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable())
190+
}
191+
ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()),
192+
AddressOf(mutability, place) => {
193+
stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable())
194+
}
195+
Len(place) => stable_mir::mir::Rvalue::Len(place.stable()),
183196
Cast(_, _, _) => todo!(),
184-
BinaryOp(_, _) => todo!(),
197+
BinaryOp(bin_op, ops) => {
198+
stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable())
199+
}
185200
CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
186201
bin_op.stable(),
187202
ops.0.stable(),
188203
ops.1.stable(),
189204
),
190205
NullaryOp(_, _) => todo!(),
191206
UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
192-
Discriminant(_) => todo!(),
207+
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()),
193208
Aggregate(_, _) => todo!(),
194209
ShallowInitBox(_, _) => todo!(),
195-
CopyForDeref(_) => todo!(),
210+
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()),
196211
}
197212
}
198213
}
199214

215+
impl Stable for mir::Mutability {
216+
type T = stable_mir::mir::Mutability;
217+
fn stable(&self) -> Self::T {
218+
use mir::Mutability::*;
219+
match *self {
220+
Not => stable_mir::mir::Mutability::Not,
221+
Mut => stable_mir::mir::Mutability::Mut,
222+
}
223+
}
224+
}
225+
226+
impl Stable for mir::BorrowKind {
227+
type T = stable_mir::mir::BorrowKind;
228+
fn stable(&self) -> Self::T {
229+
use mir::BorrowKind::*;
230+
match *self {
231+
Shared => stable_mir::mir::BorrowKind::Shared,
232+
Shallow => stable_mir::mir::BorrowKind::Shallow,
233+
Mut { kind } => stable_mir::mir::BorrowKind::Mut { kind: kind.stable() },
234+
}
235+
}
236+
}
237+
238+
impl Stable for mir::MutBorrowKind {
239+
type T = stable_mir::mir::MutBorrowKind;
240+
fn stable(&self) -> Self::T {
241+
use mir::MutBorrowKind::*;
242+
match *self {
243+
Default => stable_mir::mir::MutBorrowKind::Default,
244+
TwoPhaseBorrow => stable_mir::mir::MutBorrowKind::TwoPhaseBorrow,
245+
ClosureCapture => stable_mir::mir::MutBorrowKind::ClosureCapture,
246+
}
247+
}
248+
}
249+
250+
impl<'tcx> Stable for mir::NullOp<'tcx> {
251+
type T = stable_mir::mir::NullOp;
252+
fn stable(&self) -> Self::T {
253+
use mir::NullOp::*;
254+
match self {
255+
SizeOf => stable_mir::mir::NullOp::SizeOf,
256+
AlignOf => stable_mir::mir::NullOp::AlignOf,
257+
OffsetOf(indices) => {
258+
stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect())
259+
}
260+
}
261+
}
262+
}
263+
264+
impl Stable for mir::CastKind {
265+
type T = stable_mir::mir::CastKind;
266+
fn stable(&self) -> Self::T {
267+
use mir::CastKind::*;
268+
match self {
269+
PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress,
270+
PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress,
271+
PointerCoercion(c) => stable_mir::mir::CastKind::PointerCoercion(c.stable()),
272+
DynStar => stable_mir::mir::CastKind::DynStar,
273+
IntToInt => stable_mir::mir::CastKind::IntToInt,
274+
FloatToInt => stable_mir::mir::CastKind::FloatToInt,
275+
FloatToFloat => stable_mir::mir::CastKind::FloatToFloat,
276+
IntToFloat => stable_mir::mir::CastKind::IntToFloat,
277+
PtrToPtr => stable_mir::mir::CastKind::PtrToPtr,
278+
FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr,
279+
Transmute => stable_mir::mir::CastKind::Transmute,
280+
}
281+
}
282+
}
283+
284+
impl Stable for ty::adjustment::PointerCoercion {
285+
type T = stable_mir::mir::PointerCoercion;
286+
fn stable(&self) -> Self::T {
287+
use ty::adjustment::PointerCoercion;
288+
match self {
289+
PointerCoercion::ReifyFnPointer => stable_mir::mir::PointerCoercion::ReifyFnPointer,
290+
PointerCoercion::UnsafeFnPointer => stable_mir::mir::PointerCoercion::UnsafeFnPointer,
291+
PointerCoercion::ClosureFnPointer(unsafety) => {
292+
stable_mir::mir::PointerCoercion::ClosureFnPointer(unsafety.stable())
293+
}
294+
PointerCoercion::MutToConstPointer => {
295+
stable_mir::mir::PointerCoercion::MutToConstPointer
296+
}
297+
PointerCoercion::ArrayToPointer => stable_mir::mir::PointerCoercion::ArrayToPointer,
298+
PointerCoercion::Unsize => stable_mir::mir::PointerCoercion::Unsize,
299+
}
300+
}
301+
}
302+
303+
impl Stable for rustc_hir::Unsafety {
304+
type T = stable_mir::mir::Safety;
305+
fn stable(&self) -> Self::T {
306+
match self {
307+
rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe,
308+
rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal,
309+
}
310+
}
311+
}
312+
313+
impl Stable for FieldIdx {
314+
type T = usize;
315+
fn stable(&self) -> Self::T {
316+
self.as_usize()
317+
}
318+
}
319+
200320
impl<'tcx> Stable for mir::Operand<'tcx> {
201321
type T = stable_mir::mir::Operand;
202322
fn stable(&self) -> Self::T {

0 commit comments

Comments
 (0)