Skip to content

Commit 2a3da87

Browse files
committed
add missing rvalues to smir
1 parent 37343f4 commit 2a3da87

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+47-4
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
132132
use mir::Rvalue::*;
133133
match self {
134134
Use(op) => stable_mir::mir::Rvalue::Use(op.stable(tables)),
135-
Repeat(_, _) => todo!(),
135+
Repeat(op, len) => stable_mir::mir::Rvalue::Repeat(op.stable(tables), opaque(len)),
136136
Ref(region, kind, place) => stable_mir::mir::Rvalue::Ref(
137137
opaque(region),
138138
kind.stable(tables),
@@ -145,7 +145,11 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
145145
stable_mir::mir::Rvalue::AddressOf(mutability.stable(tables), place.stable(tables))
146146
}
147147
Len(place) => stable_mir::mir::Rvalue::Len(place.stable(tables)),
148-
Cast(_, _, _) => todo!(),
148+
Cast(cast_kind, op, ty) => stable_mir::mir::Rvalue::Cast(
149+
cast_kind.stable(tables),
150+
op.stable(tables),
151+
tables.intern_ty(*ty),
152+
),
149153
BinaryOp(bin_op, ops) => stable_mir::mir::Rvalue::BinaryOp(
150154
bin_op.stable(tables),
151155
ops.0.stable(tables),
@@ -163,8 +167,13 @@ impl<'tcx> Stable<'tcx> for mir::Rvalue<'tcx> {
163167
stable_mir::mir::Rvalue::UnaryOp(un_op.stable(tables), op.stable(tables))
164168
}
165169
Discriminant(place) => stable_mir::mir::Rvalue::Discriminant(place.stable(tables)),
166-
Aggregate(_, _) => todo!(),
167-
ShallowInitBox(_, _) => todo!(),
170+
Aggregate(agg_kind, operands) => {
171+
let operands = operands.iter().map(|op| op.stable(tables)).collect();
172+
stable_mir::mir::Rvalue::Aggregate(agg_kind.stable(tables), operands)
173+
}
174+
ShallowInitBox(op, ty) => {
175+
stable_mir::mir::Rvalue::ShallowInitBox(op.stable(tables), tables.intern_ty(*ty))
176+
}
168177
CopyForDeref(place) => stable_mir::mir::Rvalue::CopyForDeref(place.stable(tables)),
169178
}
170179
}
@@ -478,6 +487,40 @@ impl<'tcx> Stable<'tcx> for mir::UnOp {
478487
}
479488
}
480489

490+
impl<'tcx> Stable<'tcx> for mir::AggregateKind<'tcx> {
491+
type T = stable_mir::mir::AggregateKind;
492+
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
493+
match self {
494+
mir::AggregateKind::Array(ty) => {
495+
stable_mir::mir::AggregateKind::Array(tables.intern_ty(*ty))
496+
}
497+
mir::AggregateKind::Tuple => stable_mir::mir::AggregateKind::Tuple,
498+
mir::AggregateKind::Adt(def_id, var_idx, generic_arg, user_ty_index, field_idx) => {
499+
stable_mir::mir::AggregateKind::Adt(
500+
rustc_internal::adt_def(*def_id),
501+
var_idx.index(),
502+
generic_arg.stable(tables),
503+
user_ty_index.map(|idx| idx.index()),
504+
field_idx.map(|idx| idx.index()),
505+
)
506+
}
507+
mir::AggregateKind::Closure(def_id, generic_arg) => {
508+
stable_mir::mir::AggregateKind::Closure(
509+
rustc_internal::closure_def(*def_id),
510+
generic_arg.stable(tables),
511+
)
512+
}
513+
mir::AggregateKind::Generator(def_id, generic_arg, movability) => {
514+
stable_mir::mir::AggregateKind::Generator(
515+
rustc_internal::generator_def(*def_id),
516+
generic_arg.stable(tables),
517+
movability.stable(tables),
518+
)
519+
}
520+
}
521+
}
522+
}
523+
481524
impl<'tcx> Stable<'tcx> for rustc_hir::GeneratorKind {
482525
type T = stable_mir::mir::GeneratorKind;
483526
fn stable(&self, _: &mut Tables<'tcx>) -> Self::T {

compiler/rustc_smir/src/stable_mir/mir/body.rs

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::stable_mir::ty::Region;
1+
use crate::stable_mir::ty::{
2+
AdtDef, ClosureDef, Const, GeneratorDef, GenericArgs, Movability, Region,
3+
};
24
use crate::stable_mir::{self, ty::Ty};
35

46
#[derive(Clone, Debug)]
@@ -137,7 +139,6 @@ pub enum Statement {
137139
Nop,
138140
}
139141

140-
// FIXME this is incomplete
141142
#[derive(Clone, Debug)]
142143
pub enum Rvalue {
143144
/// Creates a pointer with the indicated mutability to the place.
@@ -146,6 +147,16 @@ pub enum Rvalue {
146147
/// `&raw v` or `addr_of!(v)`.
147148
AddressOf(Mutability, Place),
148149

150+
/// Creates an aggregate value, like a tuple or struct.
151+
///
152+
/// This is needed because dataflow analysis needs to distinguish
153+
/// `dest = Foo { x: ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case that `Foo`
154+
/// has a destructor.
155+
///
156+
/// Disallowed after deaggregation for all aggregate kinds except `Array` and `Generator`. After
157+
/// generator lowering, `Generator` aggregate kinds are disallowed too.
158+
Aggregate(AggregateKind, Vec<Operand>),
159+
149160
/// * `Offset` has the same semantics as [`offset`](pointer::offset), except that the second
150161
/// parameter may be a `usize` as well.
151162
/// * The comparison operations accept `bool`s, `char`s, signed or unsigned integers, floats,
@@ -198,6 +209,16 @@ pub enum Rvalue {
198209
/// Creates a reference to the place.
199210
Ref(Region, BorrowKind, Place),
200211

212+
/// Creates an array where each element is the value of the operand.
213+
///
214+
/// This is the cause of a bug in the case where the repetition count is zero because the value
215+
/// is not dropped, see [#74836].
216+
///
217+
/// Corresponds to source code like `[x; 32]`.
218+
///
219+
/// [#74836]: https://github.com/rust-lang/rust/issues/74836
220+
Repeat(Operand, Const),
221+
201222
/// Transmutes a `*mut u8` into shallow-initialized `Box<T>`.
202223
///
203224
/// This is different from a normal transmute because dataflow analysis will treat the box as
@@ -232,6 +253,15 @@ pub enum Rvalue {
232253
Use(Operand),
233254
}
234255

256+
#[derive(Clone, Debug)]
257+
pub enum AggregateKind {
258+
Array(Ty),
259+
Tuple,
260+
Adt(AdtDef, VariantIdx, GenericArgs, Option<UserTypeAnnotationIndex>, Option<FieldIdx>),
261+
Closure(ClosureDef, GenericArgs),
262+
Generator(GeneratorDef, GenericArgs, Movability),
263+
}
264+
235265
#[derive(Clone, Debug)]
236266
pub enum Operand {
237267
Copy(Place),
@@ -247,6 +277,11 @@ pub struct Place {
247277

248278
type FieldIdx = usize;
249279

280+
/// The source-order index of a variant in a type.
281+
type VariantIdx = usize;
282+
283+
type UserTypeAnnotationIndex = usize;
284+
250285
#[derive(Clone, Debug)]
251286
pub struct SwitchTarget {
252287
pub value: u128,

compiler/rustc_smir/src/stable_mir/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Ty {
1010
}
1111
}
1212

13-
type Const = Opaque;
13+
pub(crate) type Const = Opaque;
1414
pub(crate) type Region = Opaque;
1515
type Span = Opaque;
1616

0 commit comments

Comments
 (0)