Skip to content

Commit 1a48023

Browse files
committed
Add take glue for unique boxes
Closes #962 Issue #409
1 parent 77fcab0 commit 1a48023

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/comp/middle/trans.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,9 @@ fn make_take_glue(cx: @block_ctxt, v: ValueRef, t: ty::t) {
12951295
// NB: v is an *alias* of type t here, not a direct value.
12961296
if ty::type_is_boxed(bcx_tcx(bcx), t) {
12971297
bcx = incr_refcnt_of_boxed(bcx, Load(bcx, v));
1298+
} else if ty::type_is_unique_box(bcx_tcx(bcx), t) {
1299+
check trans_uniq::type_is_unique_box(bcx, t);
1300+
bcx = trans_uniq::duplicate(bcx, v, t);
12981301
} else if ty::type_is_structural(bcx_tcx(bcx), t) {
12991302
bcx = iter_structural_ty(bcx, v, t, take_ty);
13001303
} else if ty::type_is_vec(bcx_tcx(bcx), t) {

src/comp/middle/trans_uniq.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import trans::{
1515
new_sub_block_ctxt
1616
};
1717

18-
export trans_uniq, make_free_glue, type_is_unique_box, copy_val, autoderef;
18+
export trans_uniq, make_free_glue, type_is_unique_box, copy_val,
19+
autoderef, duplicate;
1920

2021
pure fn type_is_unique_box(bcx: @block_ctxt, ty: ty::t) -> bool {
2122
unchecked {
@@ -106,4 +107,17 @@ fn autoderef(bcx: @block_ctxt, v: ValueRef, t: ty::t)
106107

107108
let content_ty = content_ty(bcx, t);
108109
ret {v: v, t: content_ty};
110+
}
111+
112+
fn duplicate(bcx: @block_ctxt, v: ValueRef, t: ty::t)
113+
: type_is_unique_box(bcx, t) -> @block_ctxt {
114+
115+
let content_ty = content_ty(bcx, t);
116+
let {bcx, val: llptr} = alloc_uniq(bcx, t);
117+
118+
let src = Load(bcx, Load(bcx, v));
119+
let dst = llptr;
120+
let bcx = trans::copy_val(bcx, INIT, dst, src, content_ty);
121+
Store(bcx, dst, v);
122+
ret bcx;
109123
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn f<@T>(t: T) -> T {
2+
let t1 = t;
3+
t1
4+
}
5+
6+
fn main() {
7+
let t = f(~100);
8+
assert t == ~100;
9+
let t = f(~@[100]);
10+
assert t == ~@[100];
11+
}

0 commit comments

Comments
 (0)