Skip to content

Commit fa7db7b

Browse files
committed
Generate a temporary for assign_ops. Issue rust-lang#2581
1 parent 81af21f commit fa7db7b

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,7 @@ fn copy_val_no_check(bcx: block, action: copy_action, dst: ValueRef,
14361436
// doesn't need to be dropped. (Issue #839)
14371437
fn move_val(cx: block, action: copy_action, dst: ValueRef,
14381438
src: lval_result, t: ty::t) -> block {
1439+
assert !cx.terminated;
14391440
let _icx = cx.insn_ctxt("move_val");
14401441
let mut src_val = src.val;
14411442
let tcx = cx.tcx();
@@ -1718,15 +1719,24 @@ fn trans_assign_op(bcx: block, ex: @ast::expr, op: ast::binop,
17181719
some(origin) {
17191720
let callee_id = ast_util::op_expr_callee_id(ex);
17201721
let fty = node_id_type(bcx, callee_id);
1721-
ret trans_call_inner(
1722+
1723+
let dty = expr_ty(bcx, dst);
1724+
let target = alloc_ty(bcx, dty);
1725+
1726+
let bcx = trans_call_inner(
17221727
bcx, ex.info(), fty,
17231728
expr_ty(bcx, ex),
17241729
{|bcx|
17251730
// FIXME provide the already-computed address, not the expr
17261731
// #2528
17271732
impl::trans_method_callee(bcx, callee_id, dst, origin)
17281733
},
1729-
arg_exprs([src]), save_in(lhs_res.val));
1734+
arg_exprs([src]), save_in(target));
1735+
1736+
ret move_val(bcx, DROP_EXISTING, lhs_res.val,
1737+
// FIXME: should kind be owned?
1738+
{bcx: bcx, val: target, kind: owned},
1739+
dty);
17301740
}
17311741
_ {}
17321742
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// The cases commented as "Leaks" need to not leak. Issue #2581
2+
3+
impl methods<T: copy> for [T] {
4+
fn -(x: [T]/&) -> [T] {
5+
[x[0], x[0], x[0]]
6+
}
7+
8+
fn foo(x: [T]/&) -> [T] {
9+
[x[0], x[0], x[0]]
10+
}
11+
}
12+
13+
impl methods<T: copy> for ~T {
14+
fn +(rhs: ~T) -> ~T {
15+
rhs
16+
}
17+
}
18+
19+
impl methods for ~int {
20+
fn -(rhs: ~int) -> ~int {
21+
~(*self - *rhs)
22+
}
23+
}
24+
25+
impl methods for @int {
26+
fn +(rhs: @int) -> @int {
27+
@(*self + *rhs)
28+
}
29+
}
30+
31+
fn main() {
32+
// leaks
33+
let mut bar = [1, 2, 3];
34+
bar -= [3, 2, 1];
35+
bar -= [4, 5, 6];
36+
37+
io::println(#fmt("%?", bar));
38+
39+
// okay
40+
let mut bar = [1, 2, 3];
41+
bar = bar.foo([3, 2, 1]);
42+
bar = bar.foo([4, 5, 6]);
43+
44+
io::println(#fmt("%?", bar));
45+
46+
// okay
47+
let mut bar = [1, 2, 3];
48+
bar = bar - [3, 2, 1];
49+
bar = bar - [4, 5, 6];
50+
51+
io::println(#fmt("%?", bar));
52+
53+
// Leaks
54+
let mut bar = ~1;
55+
bar += ~2;
56+
bar += ~3;
57+
58+
io:: println(#fmt("%?", bar));
59+
60+
// Leaks
61+
let mut bar = ~1;
62+
bar -= ~2;
63+
bar -= ~3;
64+
65+
io:: println(#fmt("%?", bar));
66+
67+
// Leaks
68+
let mut bar = @1;
69+
bar += @2;
70+
bar += @3;
71+
72+
io:: println(#fmt("%?", bar));
73+
74+
}

0 commit comments

Comments
 (0)