Skip to content

Commit c8a2f53

Browse files
committed
Rvalue::InitBox prototype
1 parent 8ceea01 commit c8a2f53

File tree

21 files changed

+127
-6
lines changed

21 files changed

+127
-6
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,18 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
544544
OperandRef::new_zst(&mut bx, self.cx.layout_of(self.monomorphize(ty)));
545545
(bx, operand)
546546
}
547+
mir::Rvalue::InitBox(ref operand, content_ty) => {
548+
let operand = self.codegen_operand(&mut bx, operand);
549+
let lloperand = operand.immediate();
550+
551+
let content_ty = self.monomorphize(content_ty);
552+
let box_layout = bx.cx().layout_of(bx.tcx().mk_box(content_ty));
553+
let llty_ptr = bx.cx().backend_type(box_layout);
554+
555+
let val = bx.pointercast(lloperand, llty_ptr);
556+
let operand = OperandRef { val: OperandValue::Immediate(val), layout: box_layout };
557+
(bx, operand)
558+
}
547559
}
548560
}
549561

@@ -757,6 +769,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
757769
mir::Rvalue::AddressOf(..) |
758770
mir::Rvalue::Len(..) |
759771
mir::Rvalue::Cast(..) | // (*)
772+
mir::Rvalue::InitBox(..) | // (*)
760773
mir::Rvalue::BinaryOp(..) |
761774
mir::Rvalue::CheckedBinaryOp(..) |
762775
mir::Rvalue::UnaryOp(..) |

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ language_item_table! {
294294
BeginPanicFmt, sym::begin_panic_fmt, begin_panic_fmt, Target::Fn, GenericRequirement::None;
295295

296296
ExchangeMalloc, sym::exchange_malloc, exchange_malloc_fn, Target::Fn, GenericRequirement::None;
297+
BoxNew, sym::box_new, box_new_fn, Target::Fn, GenericRequirement::Minimum(1);
297298
BoxFree, sym::box_free, box_free_fn, Target::Fn, GenericRequirement::Minimum(1);
298299
DropInPlace, sym::drop_in_place, drop_in_place_fn, Target::Fn, GenericRequirement::Minimum(1);
299300
Oom, sym::oom, oom, Target::Fn, GenericRequirement::None;

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,8 @@ pub enum Rvalue<'tcx> {
21702170
/// that `Foo` has a destructor. These rvalues can be optimized
21712171
/// away after type-checking and before lowering.
21722172
Aggregate(Box<AggregateKind<'tcx>>, Vec<Operand<'tcx>>),
2173+
2174+
InitBox(Operand<'tcx>, Ty<'tcx>),
21732175
}
21742176

21752177
#[cfg(target_arch = "x86_64")]
@@ -2418,6 +2420,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
24182420
}),
24192421
}
24202422
}
2423+
2424+
InitBox(ref place, ref ty) => {
2425+
write!(fmt, "{:?} as box {:?}", place, ty)
2426+
}
24212427
}
24222428
}
24232429
}

compiler/rustc_middle/src/mir/tcx.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ impl<'tcx> Rvalue<'tcx> {
206206
tcx.mk_generator(did, substs, movability)
207207
}
208208
},
209+
Rvalue::InitBox(_, ty) => tcx.mk_box(ty),
209210
}
210211
}
211212

@@ -215,6 +216,7 @@ impl<'tcx> Rvalue<'tcx> {
215216
pub fn initialization_state(&self) -> RvalueInitializationState {
216217
match *self {
217218
Rvalue::NullaryOp(NullOp::Box, _) => RvalueInitializationState::Shallow,
219+
Rvalue::InitBox(_, _) => RvalueInitializationState::Shallow,
218220
_ => RvalueInitializationState::Deep,
219221
}
220222
}

compiler/rustc_middle/src/mir/type_foldable.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
210210
});
211211
Aggregate(kind, fields.fold_with(folder))
212212
}
213+
InitBox(op, ty) => InitBox(op.fold_with(folder), ty.fold_with(folder)),
213214
}
214215
}
215216

@@ -255,6 +256,10 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
255256
}
256257
fields.visit_with(visitor)
257258
}
259+
InitBox(ref op, ty) => {
260+
op.visit_with(visitor)?;
261+
ty.visit_with(visitor)
262+
}
258263
}
259264
}
260265
}

compiler/rustc_middle/src/mir/visit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,11 @@ macro_rules! make_mir_visitor {
753753
self.visit_operand(operand, location);
754754
}
755755
}
756+
757+
Rvalue::InitBox(operand, ty) => {
758+
self.visit_operand(operand, location);
759+
self.visit_ty(ty, TyContext::Location(location));
760+
}
756761
}
757762
}
758763

compiler/rustc_mir/src/borrow_check/invalidation.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,8 @@ impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
318318
Rvalue::Use(ref operand)
319319
| Rvalue::Repeat(ref operand, _)
320320
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)
321-
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
322-
self.consume_operand(location, operand)
323-
}
321+
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
322+
| Rvalue::InitBox(ref operand, _ /*ty*/) => self.consume_operand(location, operand),
324323

325324
Rvalue::Len(place) | Rvalue::Discriminant(place) => {
326325
let af = match *rvalue {

compiler/rustc_mir/src/borrow_check/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13411341
Rvalue::Use(ref operand)
13421342
| Rvalue::Repeat(ref operand, _)
13431343
| Rvalue::UnaryOp(_ /*un_op*/, ref operand)
1344-
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/) => {
1344+
| Rvalue::Cast(_ /*cast_kind*/, ref operand, _ /*ty*/)
1345+
| Rvalue::InitBox(ref operand, _ /*ty*/) => {
13451346
self.consume_operand(location, (operand, span), flow_state)
13461347
}
13471348

compiler/rustc_mir/src/borrow_check/type_check/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,25 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
23202320
}
23212321
}
23222322

2323+
Rvalue::InitBox(_, ty) => {
2324+
// Even with unsized locals cannot box an unsized value.
2325+
if self.unsized_feature_enabled() {
2326+
let span = body.source_info(location).span;
2327+
self.ensure_place_sized(ty, span);
2328+
}
2329+
2330+
let trait_ref = ty::TraitRef {
2331+
def_id: tcx.require_lang_item(LangItem::Sized, Some(self.last_span)),
2332+
substs: tcx.mk_substs_trait(ty, &[]),
2333+
};
2334+
2335+
self.prove_trait_ref(
2336+
trait_ref,
2337+
location.to_locations(),
2338+
ConstraintCategory::SizedBound,
2339+
);
2340+
}
2341+
23232342
Rvalue::AddressOf(..)
23242343
| Rvalue::ThreadLocalRef(..)
23252344
| Rvalue::Use(..)
@@ -2343,6 +2362,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
23432362
| Rvalue::AddressOf(..)
23442363
| Rvalue::Len(..)
23452364
| Rvalue::Cast(..)
2365+
| Rvalue::InitBox(..)
23462366
| Rvalue::BinaryOp(..)
23472367
| Rvalue::CheckedBinaryOp(..)
23482368
| Rvalue::NullaryOp(..)

compiler/rustc_mir/src/dataflow/impls/borrowed_locals.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ where
169169
}
170170

171171
mir::Rvalue::Cast(..)
172+
| mir::Rvalue::InitBox(..)
172173
| mir::Rvalue::Use(..)
173174
| mir::Rvalue::ThreadLocalRef(..)
174175
| mir::Rvalue::Repeat(..)

0 commit comments

Comments
 (0)