Skip to content

Commit 633c022

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 a6632f1 commit 633c022

File tree

5 files changed

+316
-10
lines changed

5 files changed

+316
-10
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3948,8 +3948,8 @@ dependencies = [
39483948
"rustc_hir",
39493949
"rustc_middle",
39503950
"rustc_span",
3951-
"scoped-tls",
39523951
"rustc_target",
3952+
"scoped-tls",
39533953
"tracing",
39543954
]
39553955

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

+125-7
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
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::{self, ty::TyKind, Context};
1112
use rustc_middle::mir;
1213
use rustc_middle::ty::{self, Ty, TyCtxt};
1314
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
15+
use rustc_target::abi::FieldIdx;
1416
use tracing::debug;
1517

1618
impl<'tcx> Context for Tables<'tcx> {
@@ -127,6 +129,13 @@ pub(crate) trait Stable {
127129
fn stable(&self) -> Self::T;
128130
}
129131

132+
impl Stable for DefId {
133+
type T = stable_mir::CrateItem;
134+
fn stable(&self) -> Self::T {
135+
rustc_internal::crate_item(*self)
136+
}
137+
}
138+
130139
impl<'tcx> Stable for mir::Statement<'tcx> {
131140
type T = stable_mir::mir::Statement;
132141
fn stable(&self) -> Self::T {
@@ -158,27 +167,136 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
158167
match self {
159168
Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
160169
Repeat(_, _) => todo!(),
161-
Ref(_, _, _) => todo!(),
162-
ThreadLocalRef(_) => todo!(),
163-
AddressOf(_, _) => todo!(),
164-
Len(_) => todo!(),
170+
Ref(region, kind, place) => {
171+
stable_mir::mir::Rvalue::Ref(opaque(region), kind.stable(), place.stable())
172+
}
173+
ThreadLocalRef(def_id) => stable_mir::mir::Rvalue::ThreadLocalRef(def_id.stable()),
174+
AddressOf(mutability, place) => {
175+
stable_mir::mir::Rvalue::AddressOf(mutability.stable(), place.stable())
176+
}
177+
Len(place) => stable_mir::mir::Rvalue::Len(place.stable()),
165178
Cast(_, _, _) => todo!(),
166-
BinaryOp(_, _) => todo!(),
179+
BinaryOp(bin_op, ops) => {
180+
stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable())
181+
}
167182
CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
168183
bin_op.stable(),
169184
ops.0.stable(),
170185
ops.1.stable(),
171186
),
172187
NullaryOp(_, _) => todo!(),
173188
UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
174-
Discriminant(_) => todo!(),
189+
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()),
175190
Aggregate(_, _) => todo!(),
176191
ShallowInitBox(_, _) => todo!(),
177-
CopyForDeref(_) => todo!(),
192+
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()),
193+
}
194+
}
195+
}
196+
197+
impl Stable for mir::Mutability {
198+
type T = stable_mir::mir::Mutability;
199+
fn stable(&self) -> Self::T {
200+
use mir::Mutability::*;
201+
match *self {
202+
Not => stable_mir::mir::Mutability::Not,
203+
Mut => stable_mir::mir::Mutability::Mut,
204+
}
205+
}
206+
}
207+
208+
impl Stable for mir::BorrowKind {
209+
type T = stable_mir::mir::BorrowKind;
210+
fn stable(&self) -> Self::T {
211+
use mir::BorrowKind::*;
212+
match *self {
213+
Shared => stable_mir::mir::BorrowKind::Shared,
214+
Shallow => stable_mir::mir::BorrowKind::Shallow,
215+
Mut { kind } => stable_mir::mir::BorrowKind::Mut { kind: kind.stable() },
216+
}
217+
}
218+
}
219+
220+
impl Stable for mir::MutBorrowKind {
221+
type T = stable_mir::mir::MutBorrowKind;
222+
fn stable(&self) -> Self::T {
223+
use mir::MutBorrowKind::*;
224+
match *self {
225+
Default => stable_mir::mir::MutBorrowKind::Default,
226+
TwoPhaseBorrow => stable_mir::mir::MutBorrowKind::TwoPhaseBorrow,
227+
ClosureCapture => stable_mir::mir::MutBorrowKind::ClosureCapture,
228+
}
229+
}
230+
}
231+
232+
impl<'tcx> Stable for mir::NullOp<'tcx> {
233+
type T = stable_mir::mir::NullOp;
234+
fn stable(&self) -> Self::T {
235+
use mir::NullOp::*;
236+
match self {
237+
SizeOf => stable_mir::mir::NullOp::SizeOf,
238+
AlignOf => stable_mir::mir::NullOp::AlignOf,
239+
OffsetOf(indices) => {
240+
stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect())
241+
}
242+
}
243+
}
244+
}
245+
246+
impl Stable for mir::CastKind {
247+
type T = stable_mir::mir::CastKind;
248+
fn stable(&self) -> Self::T {
249+
use mir::CastKind::*;
250+
match self {
251+
PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress,
252+
PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress,
253+
Pointer(cast) => stable_mir::mir::CastKind::Pointer(cast.stable()),
254+
DynStar => stable_mir::mir::CastKind::DynStar,
255+
IntToInt => stable_mir::mir::CastKind::IntToInt,
256+
FloatToInt => stable_mir::mir::CastKind::FloatToInt,
257+
FloatToFloat => stable_mir::mir::CastKind::FloatToFloat,
258+
IntToFloat => stable_mir::mir::CastKind::IntToFloat,
259+
PtrToPtr => stable_mir::mir::CastKind::PtrToPtr,
260+
FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr,
261+
Transmute => stable_mir::mir::CastKind::Transmute,
262+
}
263+
}
264+
}
265+
266+
impl Stable for ty::adjustment::PointerCast {
267+
type T = stable_mir::mir::PointerCast;
268+
fn stable(&self) -> Self::T {
269+
use ty::adjustment::PointerCast;
270+
match self {
271+
PointerCast::ReifyFnPointer => stable_mir::mir::PointerCast::ReifyFnPointer,
272+
PointerCast::UnsafeFnPointer => stable_mir::mir::PointerCast::UnsafeFnPointer,
273+
PointerCast::ClosureFnPointer(unsafety) => {
274+
stable_mir::mir::PointerCast::ClosureFnPointer(unsafety.stable())
275+
}
276+
PointerCast::MutToConstPointer => stable_mir::mir::PointerCast::MutToConstPointer,
277+
PointerCast::ArrayToPointer => stable_mir::mir::PointerCast::ArrayToPointer,
278+
PointerCast::Unsize => stable_mir::mir::PointerCast::Unsize,
178279
}
179280
}
180281
}
181282

283+
impl Stable for rustc_hir::Unsafety {
284+
type T = stable_mir::mir::Safety;
285+
fn stable(&self) -> Self::T {
286+
match self {
287+
rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe,
288+
rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal,
289+
}
290+
}
291+
}
292+
293+
impl Stable for FieldIdx {
294+
type T = usize;
295+
fn stable(&self) -> Self::T {
296+
self.as_usize()
297+
}
298+
}
299+
182300
impl<'tcx> Stable for mir::Operand<'tcx> {
183301
type T = stable_mir::mir::Operand;
184302
fn stable(&self) -> Self::T {

0 commit comments

Comments
 (0)