Skip to content

Commit 71a9222

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 b83c009 + 97e609c commit 71a9222

File tree

7 files changed

+359
-41
lines changed

7 files changed

+359
-41
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4099,6 +4099,7 @@ dependencies = [
40994099
"rustc_hir",
41004100
"rustc_middle",
41014101
"rustc_span",
4102+
"rustc_target",
41024103
"tracing",
41034104
]
41044105

compiler/rustc_smir/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ 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

1214
[features]
1315
default = [
16+
"rustc_hir",
1417
"rustc_middle",
1518
"rustc_span",
19+
"rustc_target",
1620
]
+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

+153-37
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> {
@@ -119,11 +121,21 @@ fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
119121
stable_mir::Crate { id: crate_num.into(), name: crate_name, is_local }
120122
}
121123

122-
pub trait Stable {
124+
/// Trait used to convert between an internal MIR type to a Stable MIR type.
125+
pub(crate) trait Stable {
126+
/// The stable representation of the type implementing Stable.
123127
type T;
128+
/// Converts an object to the equivalent Stable MIR representation.
124129
fn stable(&self) -> Self::T;
125130
}
126131

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+
127139
impl<'tcx> Stable for mir::Statement<'tcx> {
128140
type T = stable_mir::mir::Statement;
129141
fn stable(&self) -> Self::T {
@@ -155,27 +167,127 @@ impl<'tcx> Stable for mir::Rvalue<'tcx> {
155167
match self {
156168
Use(op) => stable_mir::mir::Rvalue::Use(op.stable()),
157169
Repeat(_, _) => todo!(),
158-
Ref(_, _, _) => todo!(),
159-
ThreadLocalRef(_) => todo!(),
160-
AddressOf(_, _) => todo!(),
161-
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()),
162178
Cast(_, _, _) => todo!(),
163-
BinaryOp(_, _) => todo!(),
179+
BinaryOp(bin_op, ops) => {
180+
stable_mir::mir::Rvalue::BinaryOp(bin_op.stable(), ops.0.stable(), ops.1.stable())
181+
}
164182
CheckedBinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::CheckedBinaryOp(
165183
bin_op.stable(),
166184
ops.0.stable(),
167185
ops.1.stable(),
168186
),
169187
NullaryOp(_, _) => todo!(),
170188
UnaryOp(un_op, op) => stable_mir::mir::Rvalue::UnaryOp(un_op.stable(), op.stable()),
171-
Discriminant(_) => todo!(),
189+
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable()),
172190
Aggregate(_, _) => todo!(),
173191
ShallowInitBox(_, _) => todo!(),
174-
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+
Unique => stable_mir::mir::BorrowKind::Unique,
216+
Mut { allow_two_phase_borrow } => {
217+
stable_mir::mir::BorrowKind::Mut { allow_two_phase_borrow }
218+
}
219+
}
220+
}
221+
}
222+
223+
impl<'tcx> Stable for mir::NullOp<'tcx> {
224+
type T = stable_mir::mir::NullOp;
225+
fn stable(&self) -> Self::T {
226+
use mir::NullOp::*;
227+
match self {
228+
SizeOf => stable_mir::mir::NullOp::SizeOf,
229+
AlignOf => stable_mir::mir::NullOp::AlignOf,
230+
OffsetOf(indices) => {
231+
stable_mir::mir::NullOp::OffsetOf(indices.iter().map(|idx| idx.stable()).collect())
232+
}
233+
}
234+
}
235+
}
236+
237+
impl Stable for mir::CastKind {
238+
type T = stable_mir::mir::CastKind;
239+
fn stable(&self) -> Self::T {
240+
use mir::CastKind::*;
241+
match self {
242+
PointerExposeAddress => stable_mir::mir::CastKind::PointerExposeAddress,
243+
PointerFromExposedAddress => stable_mir::mir::CastKind::PointerFromExposedAddress,
244+
Pointer(cast) => stable_mir::mir::CastKind::Pointer(cast.stable()),
245+
DynStar => stable_mir::mir::CastKind::DynStar,
246+
IntToInt => stable_mir::mir::CastKind::IntToInt,
247+
FloatToInt => stable_mir::mir::CastKind::FloatToInt,
248+
FloatToFloat => stable_mir::mir::CastKind::FloatToFloat,
249+
IntToFloat => stable_mir::mir::CastKind::IntToFloat,
250+
PtrToPtr => stable_mir::mir::CastKind::PtrToPtr,
251+
FnPtrToPtr => stable_mir::mir::CastKind::FnPtrToPtr,
252+
Transmute => stable_mir::mir::CastKind::Transmute,
253+
}
254+
}
255+
}
256+
257+
impl Stable for ty::adjustment::PointerCast {
258+
type T = stable_mir::mir::PointerCast;
259+
fn stable(&self) -> Self::T {
260+
use ty::adjustment::PointerCast;
261+
match self {
262+
PointerCast::ReifyFnPointer => stable_mir::mir::PointerCast::ReifyFnPointer,
263+
PointerCast::UnsafeFnPointer => stable_mir::mir::PointerCast::UnsafeFnPointer,
264+
PointerCast::ClosureFnPointer(unsafety) => {
265+
stable_mir::mir::PointerCast::ClosureFnPointer(unsafety.stable())
266+
}
267+
PointerCast::MutToConstPointer => stable_mir::mir::PointerCast::MutToConstPointer,
268+
PointerCast::ArrayToPointer => stable_mir::mir::PointerCast::ArrayToPointer,
269+
PointerCast::Unsize => stable_mir::mir::PointerCast::Unsize,
270+
}
271+
}
272+
}
273+
274+
impl Stable for rustc_hir::Unsafety {
275+
type T = stable_mir::mir::Safety;
276+
fn stable(&self) -> Self::T {
277+
match self {
278+
rustc_hir::Unsafety::Unsafe => stable_mir::mir::Safety::Unsafe,
279+
rustc_hir::Unsafety::Normal => stable_mir::mir::Safety::Normal,
175280
}
176281
}
177282
}
178283

284+
impl Stable for FieldIdx {
285+
type T = usize;
286+
fn stable(&self) -> Self::T {
287+
self.as_usize()
288+
}
289+
}
290+
179291
impl<'tcx> Stable for mir::Operand<'tcx> {
180292
type T = stable_mir::mir::Operand;
181293
fn stable(&self) -> Self::T {
@@ -211,34 +323,38 @@ impl Stable for mir::UnwindAction {
211323
}
212324
}
213325

214-
fn rustc_assert_msg_to_msg<'tcx>(
215-
assert_message: &rustc_middle::mir::AssertMessage<'tcx>,
216-
) -> stable_mir::mir::AssertMessage {
217-
use rustc_middle::mir::AssertKind;
218-
match assert_message {
219-
AssertKind::BoundsCheck { len, index } => {
220-
stable_mir::mir::AssertMessage::BoundsCheck { len: len.stable(), index: index.stable() }
221-
}
222-
AssertKind::Overflow(bin_op, op1, op2) => {
223-
stable_mir::mir::AssertMessage::Overflow(bin_op.stable(), op1.stable(), op2.stable())
224-
}
225-
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
226-
AssertKind::DivisionByZero(op) => {
227-
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
228-
}
229-
AssertKind::RemainderByZero(op) => {
230-
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
231-
}
232-
AssertKind::ResumedAfterReturn(generator) => {
233-
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
234-
}
235-
AssertKind::ResumedAfterPanic(generator) => {
236-
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
237-
}
238-
AssertKind::MisalignedPointerDereference { required, found } => {
239-
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
240-
required: required.stable(),
241-
found: found.stable(),
326+
impl<'tcx> Stable for mir::AssertMessage<'tcx> {
327+
type T = stable_mir::mir::AssertMessage;
328+
fn stable(&self) -> Self::T {
329+
use rustc_middle::mir::AssertKind;
330+
match self {
331+
AssertKind::BoundsCheck { len, index } => stable_mir::mir::AssertMessage::BoundsCheck {
332+
len: len.stable(),
333+
index: index.stable(),
334+
},
335+
AssertKind::Overflow(bin_op, op1, op2) => stable_mir::mir::AssertMessage::Overflow(
336+
bin_op.stable(),
337+
op1.stable(),
338+
op2.stable(),
339+
),
340+
AssertKind::OverflowNeg(op) => stable_mir::mir::AssertMessage::OverflowNeg(op.stable()),
341+
AssertKind::DivisionByZero(op) => {
342+
stable_mir::mir::AssertMessage::DivisionByZero(op.stable())
343+
}
344+
AssertKind::RemainderByZero(op) => {
345+
stable_mir::mir::AssertMessage::RemainderByZero(op.stable())
346+
}
347+
AssertKind::ResumedAfterReturn(generator) => {
348+
stable_mir::mir::AssertMessage::ResumedAfterReturn(generator.stable())
349+
}
350+
AssertKind::ResumedAfterPanic(generator) => {
351+
stable_mir::mir::AssertMessage::ResumedAfterPanic(generator.stable())
352+
}
353+
AssertKind::MisalignedPointerDereference { required, found } => {
354+
stable_mir::mir::AssertMessage::MisalignedPointerDereference {
355+
required: required.stable(),
356+
found: found.stable(),
357+
}
242358
}
243359
}
244360
}
@@ -363,7 +479,7 @@ impl<'tcx> Stable for mir::Terminator<'tcx> {
363479
Assert { cond, expected, msg, target, unwind } => Terminator::Assert {
364480
cond: cond.stable(),
365481
expected: *expected,
366-
msg: rustc_assert_msg_to_msg(msg),
482+
msg: msg.stable(),
367483
target: target.as_usize(),
368484
unwind: unwind.stable(),
369485
},

0 commit comments

Comments
 (0)