Skip to content

Commit 00ce295

Browse files
committed
Remove ShallowInitBox in runtime MIR.
1 parent 608d306 commit 00ce295

File tree

12 files changed

+36
-24
lines changed

12 files changed

+36
-24
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -724,22 +724,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
724724
}
725725
}
726726
}
727-
mir::Rvalue::ShallowInitBox(ref operand, content_ty) => {
728-
let operand = self.codegen_operand(bx, operand);
729-
let val = operand.immediate();
730-
731-
let content_ty = self.monomorphize(content_ty);
732-
let box_layout = bx.cx().layout_of(Ty::new_box(bx.tcx(), content_ty));
733-
734-
OperandRef { val: OperandValue::Immediate(val), layout: box_layout }
735-
}
736727
mir::Rvalue::WrapUnsafeBinder(ref operand, binder_ty) => {
737728
let operand = self.codegen_operand(bx, operand);
738729
let binder_ty = self.monomorphize(binder_ty);
739730
let layout = bx.cx().layout_of(binder_ty);
740731
OperandRef { val: operand.val, layout }
741732
}
742733
mir::Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
734+
mir::Rvalue::ShallowInitBox(..) => {
735+
bug!("ShallowInitBox must not appear in runtime MIR")
736+
}
743737
}
744738
}
745739

compiler/rustc_const_eval/src/interpret/step.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,6 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
255255
self.write_immediate(*val, &dest)?;
256256
}
257257

258-
ShallowInitBox(ref operand, _) => {
259-
let src = self.eval_operand(operand, None)?;
260-
let v = self.read_immediate(&src)?;
261-
self.write_immediate(*v, &dest)?;
262-
}
263-
264258
Cast(cast_kind, ref operand, cast_ty) => {
265259
let src = self.eval_operand(operand, None)?;
266260
let cast_ty =
@@ -281,6 +275,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
281275
let op = self.eval_operand(op, None)?;
282276
self.copy_op_allow_transmute(&op, &dest)?;
283277
}
278+
279+
ShallowInitBox(..) => bug!("ShallowInitBox must not appear in runtime MIR"),
284280
}
285281

286282
trace!("{:?}", self.dump_place(&dest));

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ pub enum RuntimePhase {
129129
/// disallowed:
130130
/// * [`TerminatorKind::Yield`]
131131
/// * [`TerminatorKind::CoroutineDrop`]
132-
/// * [`Rvalue::Aggregate`] for any `AggregateKind` except `Array`
133132
/// * [`Rvalue::CopyForDeref`]
133+
/// * [`Rvalue::ShallowInitBox`]
134134
/// * [`PlaceElem::OpaqueCast`]
135135
/// * [`LocalInfo::DerefTemp`](super::LocalInfo::DerefTemp)
136136
///

compiler/rustc_mir_transform/src/dataflow_const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,11 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
489489
| Rvalue::Cast(..)
490490
| Rvalue::BinaryOp(..)
491491
| Rvalue::Aggregate(..)
492-
| Rvalue::ShallowInitBox(..)
493492
| Rvalue::WrapUnsafeBinder(..) => {
494493
// No modification is possible through these r-values.
495494
return ValueOrPlace::TOP;
496495
}
496+
Rvalue::ShallowInitBox(..) => bug!("ShallowInitBox must not appear in runtime MIR"),
497497
};
498498
ValueOrPlace::Value(val)
499499
}

compiler/rustc_mir_transform/src/elaborate_box_derefs.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ impl<'a, 'tcx> MutVisitor<'tcx> for ElaborateBoxDerefVisitor<'a, 'tcx> {
8686

8787
self.super_place(place, context, location);
8888
}
89+
90+
fn visit_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, location: Location) {
91+
if let Rvalue::ShallowInitBox(operand, ty) = rvalue {
92+
let ty = Ty::new_box(self.tcx, *ty);
93+
let operand = std::mem::replace(
94+
operand,
95+
// Just a meaningless placeholder.
96+
Operand::Copy(RETURN_PLACE.into()),
97+
);
98+
*rvalue = Rvalue::Cast(CastKind::Transmute, operand, ty)
99+
}
100+
101+
self.super_rvalue(rvalue, location);
102+
}
89103
}
90104

91105
pub(super) struct ElaborateBoxDerefs;

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,9 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10731073
}
10741074

10751075
// Unsupported values.
1076-
Rvalue::ThreadLocalRef(..) | Rvalue::ShallowInitBox(..) => return None,
1076+
Rvalue::ThreadLocalRef(..) => return None,
10771077
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in runtime MIR"),
1078+
Rvalue::ShallowInitBox(..) => bug!("ShallowInitBox must not appear in runtime MIR"),
10781079
};
10791080
let ty = rvalue.ty(self.local_decls, self.tcx);
10801081
Some(self.insert(ty, value))

compiler/rustc_mir_transform/src/validate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11731173
}
11741174
}
11751175
Rvalue::ShallowInitBox(operand, _) => {
1176+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1177+
self.fail(
1178+
location,
1179+
"`ShallowInitBox` should have been removed after box elaboration phase",
1180+
);
1181+
}
1182+
11761183
let a = operand.ty(&self.body.local_decls, self.tcx);
11771184
check_kinds!(a, "Cannot shallow init type {:?}", ty::RawPtr(..));
11781185
}

tests/mir-opt/box_expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
fn move_from_inner() {
88
// CHECK-LABEL: fn move_from_inner(
99
// CHECK: debug x => [[x:_.*]];
10-
// CHECK: [[box:_.*]] = ShallowInitBox(
10+
// CHECK: [[box:_.*]] = move {{_.*}} as std::boxed::Box<S> (Transmute);
1111
// CHECK: [[ptr:_.*]] = copy (([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>) as *const S (Transmute);
1212
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
1313
// CHECK: [[ret]]: {
@@ -34,7 +34,7 @@ fn move_from_inner() {
3434
// EMIT_MIR box_expr.main.ElaborateDrops.diff
3535
fn main() {
3636
// CHECK-LABEL: fn main(
37-
// CHECK: [[box:_.*]] = ShallowInitBox(
37+
// CHECK: [[box:_.*]] = move {{_.*}} as std::boxed::Box<S> (Transmute);
3838
// CHECK: [[ptr:_.*]] = copy (([[box]].0: std::ptr::Unique<S>).0: std::ptr::NonNull<S>) as *const S (Transmute);
3939
// CHECK: (*[[ptr]]) = S::new() -> [return: [[ret:bb.*]], unwind: [[unwind:bb.*]]];
4040
// CHECK: [[ret]]: {

tests/mir-opt/const_prop/boxes.main.GVN.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
bb1: {
3333
StorageLive(_7);
34-
_7 = ShallowInitBox(move _6, i32);
34+
_7 = move _6 as std::boxed::Box<i32> (Transmute);
3535
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3636
(*_8) = const 42_i32;
3737
_3 = move _7;

tests/mir-opt/const_prop/boxes.main.GVN.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
bb1: {
3333
StorageLive(_7);
34-
_7 = ShallowInitBox(move _6, i32);
34+
_7 = move _6 as std::boxed::Box<i32> (Transmute);
3535
_8 = copy ((_7.0: std::ptr::Unique<i32>).0: std::ptr::NonNull<i32>) as *const i32 (Transmute);
3636
(*_8) = const 42_i32;
3737
_3 = move _7;

0 commit comments

Comments
 (0)