Skip to content

Commit 3f73a7d

Browse files
authored
Rollup merge of rust-lang#112717 - celinval:stable-mir-rvalue-1, r=oli-obk
Implement a few more rvalue translation to smir Add the implementation for a few more RValue variants. For now, I simplified the stable version of `RValue::Ref` by removing the notion of Region. r? `@oli-obk`
2 parents b3ab80c + b9f378b commit 3f73a7d

File tree

7 files changed

+371
-41
lines changed

7 files changed

+371
-41
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4233,6 +4233,7 @@ dependencies = [
42334233
"rustc_hir",
42344234
"rustc_middle",
42354235
"rustc_span",
4236+
"rustc_target",
42364237
"scoped-tls",
42374238
"tracing",
42384239
]

compiler/rustc_smir/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7-
rustc_hir = { path = "../rustc_hir" }
7+
# Use optional dependencies for rustc_* in order to support building this crate separately.
8+
rustc_hir = { path = "../rustc_hir", optional = true }
89
rustc_middle = { path = "../rustc_middle", optional = true }
910
rustc_span = { path = "../rustc_span", optional = true }
11+
rustc_target = { path = "../rustc_target", optional = true }
1012
tracing = "0.1"
1113
scoped-tls = "1.0"
1214

1315
[features]
1416
default = [
17+
"rustc_hir",
1518
"rustc_middle",
1619
"rustc_span",
20+
"rustc_target",
1721
]
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2023-02-28"
2+
channel = "nightly-2023-06-14"
33
components = [ "rustfmt", "rustc-dev" ]

compiler/rustc_smir/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
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.
17+
18+
// Declare extern rustc_* crates to enable building this crate separately from the compiler.
19+
#[cfg(not(feature = "default"))]
20+
extern crate rustc_hir;
21+
#[cfg(not(feature = "default"))]
22+
extern crate rustc_middle;
23+
#[cfg(not(feature = "default"))]
24+
extern crate rustc_span;
25+
#[cfg(not(feature = "default"))]
26+
extern crate rustc_target;
1627

1728
pub mod rustc_internal;
1829
pub mod stable_mir;

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

+164-37
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> {
@@ -137,11 +139,21 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
137139
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
138140
}
139141

140-
pub trait Stable {
142+
/// Trait used to convert between an internal MIR type to a Stable MIR type.
143+
pub(crate) trait Stable {
144+
/// The stable representation of the type implementing Stable.
141145
type T;
146+
/// Converts an object to the equivalent Stable MIR representation.
142147
fn stable(&self) -> Self::T;
143148
}
144149

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+
145157
impl<'tcx> Stable for mir::Statement<'tcx> {
146158
type T = stable_mir::mir::Statement;
147159
fn stable(&self) -> Self::T {
@@ -173,27 +185,138 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
173185
match self {
174186
Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
175187
Repeat(_, _) => todo!(),
176-
Ref(_, _, _) => todo!(),
177-
ThreadLocalRef(_) => todo!(),
178-
AddressOf(_, _) => todo!(),
179-
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()),
180196
Cast(_, _, _) => todo!(),
181-
BinaryOp(_, _) => todo!(),
197+
BinaryOp(bin_op, ops) => {
198+
stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable())
199+
}
182200
CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
183201
bin_op.stable(),
184202
ops.0.stable(),
185203
ops.1.stable(),
186204
),
187205
NullaryOp(_, _) => todo!(),
188206
UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
189-
Discriminant(_) => todo!(),
207+
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()),
190208
Aggregate(_, _) => todo!(),
191209
ShallowInitBox(_, _) => todo!(),
192-
CopyForDeref(_) => todo!(),
210+
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable()),
211+
}
212+
}
213+
}
214+
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,
193280
}
194281
}
195282
}
196283

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+
197320
impl<'tcx> Stable for mir::Operand<'tcx> {
198321
type T = stable_mir::mir::Operand;
199322
fn stable(&self) -> Self::T {
@@ -229,34 +352,38 @@ impl Stable for mir::UnwindAction {
229352
}
230353
}
231354

232-
fn rustc_assert_msg_to_msg<'tcx>(
233-
assert_message: &rustc_middle::mir::AssertMessage<'tcx>,
234-
) -> stable_mir::mir::AssertMessage {
235-
use rustc_middle::mir::AssertKind;
236-
match assert_message {
237-
AssertKind::BoundsCheck { len, index } => {
238-
stable_mir::mir::AssertMessage::BoundsCheck { len: len.stable(), index: index.stable() }
239-
}
240-
AssertKind::Overflow(bin_op, op1, op2) => {
241-
stable_mir::mir::AssertMessage::Overflow(bin_op.stable(), op1.stable(), op2.stable())
242-
}
243-
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
244-
AssertKind::DivisionByZero(op) => {
245-
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
246-
}
247-
AssertKind::RemainderByZero(op) => {
248-
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
249-
}
250-
AssertKind::ResumedAfterReturn(generator) => {
251-
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
252-
}
253-
AssertKind::ResumedAfterPanic(generator) => {
254-
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
255-
}
256-
AssertKind::MisalignedPointerDereference { required, found } => {
257-
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
258-
required: required.stable(),
259-
found: found.stable(),
355+
impl<'tcx> Stable for mir::AssertMessage<'tcx> {
356+
type T = stable_mir::mir::AssertMessage;
357+
fn stable(&self) -> Self::T {
358+
use rustc_middle::mir::AssertKind;
359+
match self {
360+
AssertKind::BoundsCheck { len, index } => stable_mir::mir::AssertMessage::BoundsCheck {
361+
len: len.stable(),
362+
index: index.stable(),
363+
},
364+
AssertKind::Overflow(bin_op, op1, op2) => stable_mir::mir::AssertMessage::Overflow(
365+
bin_op.stable(),
366+
op1.stable(),
367+
op2.stable(),
368+
),
369+
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
370+
AssertKind::DivisionByZero(op) => {
371+
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
372+
}
373+
AssertKind::RemainderByZero(op) => {
374+
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
375+
}
376+
AssertKind::ResumedAfterReturn(generator) => {
377+
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
378+
}
379+
AssertKind::ResumedAfterPanic(generator) => {
380+
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
381+
}
382+
AssertKind::MisalignedPointerDereference { required, found } => {
383+
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
384+
required: required.stable(),
385+
found: found.stable(),
386+
}
260387
}
261388
}
262389
}
@@ -381,7 +508,7 @@ impl<'tcx> Stable for mir::Terminator<'tcx> {
381508
Assert { cond, expected, msg, target, unwind } => Terminator::Assert {
382509
cond: cond.stable(),
383510
expected: *expected,
384-
msg: rustc_assert_msg_to_msg(msg),
511+
msg: msg.stable(),
385512
target: target.as_usize(),
386513
unwind: unwind.stable(),
387514
},

0 commit comments

Comments
 (0)