Skip to content

Commit b773f8d

Browse files
committed
rustc: Duplicate heap data of interior vectors when passing them by value
1 parent 0eb257e commit b773f8d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/comp/middle/trans.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5515,7 +5515,21 @@ fn trans_arg_expr(&@block_ctxt cx, &ty::arg arg, TypeRef lldestty0,
55155515
val = do_spill(lv.res.bcx, lv.res.val);
55165516
}
55175517
} else { auto re = trans_expr(bcx, e); val = re.val; bcx = re.bcx; }
5518-
if (arg.mode == ty::mo_val) { bcx = copy_ty(bcx, val, e_ty).bcx; }
5518+
5519+
// Make a copy here if the type is structural and we're passing by value.
5520+
if (arg.mode == ty::mo_val) {
5521+
if (ty::type_owns_heap_mem(cx.fcx.lcx.ccx.tcx, e_ty)) {
5522+
auto rslt = alloc_ty(bcx, e_ty);
5523+
bcx = rslt.bcx;
5524+
auto dst = rslt.val;
5525+
rslt = copy_val(bcx, INIT, dst, val, e_ty);
5526+
bcx = rslt.bcx;
5527+
val = dst;
5528+
} else {
5529+
bcx = copy_ty(bcx, val, e_ty).bcx;
5530+
}
5531+
}
5532+
55195533
if (ty::type_is_bot(cx.fcx.lcx.ccx.tcx, e_ty)) {
55205534
// For values of type _|_, we generate an
55215535
// "undef" value, as such a value should never
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// xfail-stage0
2+
3+
fn f(int[] a) {}
4+
fn main() { f(~[ 1, 2, 3, 4, 5 ]); }
5+

0 commit comments

Comments
 (0)