Skip to content

Commit 51468b6

Browse files
committed
Properly cleanup slice literals. Closes #2705.
1 parent f17ca3f commit 51468b6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/rustc/middle/trans/tvec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,15 @@ fn trans_evec(bcx: block, args: [@ast::expr]/~,
152152
}
153153
ast::vstore_slice(_) {
154154
let n = vec::len(args);
155+
// Make a fake type to use for the cleanup
156+
let ty = ty::mk_evec(bcx.tcx(),
157+
{ty: unit_ty, mutbl: ast::m_mutbl},
158+
ty::vstore_fixed(n));
159+
155160
let n = C_uint(ccx, n);
156161
let vp = base::arrayalloca(bcx, llunitty, n);
162+
add_clean(bcx, vp, ty);
163+
157164
let len = Mul(bcx, n, unit_sz);
158165

159166
let p = base::alloca(bcx, T_struct([T_ptr(llunitty),

src/test/run-pass/vec-slice-drop.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Make sure that destructors get run on slice literals
2+
class foo {
3+
let x: @mut int;
4+
new(x: @mut int) { self.x = x; }
5+
drop { *self.x += 1; }
6+
}
7+
8+
fn main() {
9+
let x = @mut 0;
10+
{
11+
let l = [foo(x)]/&;
12+
assert *l[0].x == 0;
13+
}
14+
assert *x == 1;
15+
}

0 commit comments

Comments
 (0)