diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 5833be8aaff5..0c90de1ac38a 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -1295,6 +1295,9 @@ fn make_take_glue(cx: @block_ctxt, v: ValueRef, t: ty::t) { // NB: v is an *alias* of type t here, not a direct value. if ty::type_is_boxed(bcx_tcx(bcx), t) { bcx = incr_refcnt_of_boxed(bcx, Load(bcx, v)); + } else if ty::type_is_unique_box(bcx_tcx(bcx), t) { + check trans_uniq::type_is_unique_box(bcx, t); + bcx = trans_uniq::duplicate(bcx, v, t); } else if ty::type_is_structural(bcx_tcx(bcx), t) { bcx = iter_structural_ty(bcx, v, t, take_ty); } else if ty::type_is_vec(bcx_tcx(bcx), t) { diff --git a/src/comp/middle/trans_uniq.rs b/src/comp/middle/trans_uniq.rs index 2c75ccc67b9a..a7649961c745 100644 --- a/src/comp/middle/trans_uniq.rs +++ b/src/comp/middle/trans_uniq.rs @@ -15,7 +15,8 @@ import trans::{ new_sub_block_ctxt }; -export trans_uniq, make_free_glue, type_is_unique_box, copy_val, autoderef; +export trans_uniq, make_free_glue, type_is_unique_box, copy_val, +autoderef, duplicate; pure fn type_is_unique_box(bcx: @block_ctxt, ty: ty::t) -> bool { unchecked { @@ -106,4 +107,17 @@ fn autoderef(bcx: @block_ctxt, v: ValueRef, t: ty::t) let content_ty = content_ty(bcx, t); ret {v: v, t: content_ty}; +} + +fn duplicate(bcx: @block_ctxt, v: ValueRef, t: ty::t) + : type_is_unique_box(bcx, t) -> @block_ctxt { + + let content_ty = content_ty(bcx, t); + let {bcx, val: llptr} = alloc_uniq(bcx, t); + + let src = Load(bcx, Load(bcx, v)); + let dst = llptr; + let bcx = trans::copy_val(bcx, INIT, dst, src, content_ty); + Store(bcx, dst, v); + ret bcx; } \ No newline at end of file diff --git a/src/test/run-pass/unique-assign-generic.rs b/src/test/run-pass/unique-assign-generic.rs new file mode 100644 index 000000000000..7a10de278ea9 --- /dev/null +++ b/src/test/run-pass/unique-assign-generic.rs @@ -0,0 +1,11 @@ +fn f<@T>(t: T) -> T { + let t1 = t; + t1 +} + +fn main() { + let t = f(~100); + assert t == ~100; + let t = f(~@[100]); + assert t == ~@[100]; +}