]> git.lizzy.rs Git - rust.git/commitdiff
Initialize unique box locals from other locals
authorBrian Anderson <banderson@mozilla.com>
Thu, 22 Sep 2011 20:22:53 +0000 (13:22 -0700)
committerBrian Anderson <banderson@mozilla.com>
Thu, 22 Sep 2011 20:53:33 +0000 (13:53 -0700)
Issue #409

src/comp/middle/trans.rs
src/comp/middle/trans_uniq.rs
src/test/run-pass/unique-decl-init-copy.rs [new file with mode: 0644]
src/test/run-pass/unique-decl-init.rs [new file with mode: 0644]

index 12be2f3f54a96dcc93c3e0920542673d3139b2f9..555236e6415a1a57d2e80b6bdab3f4781164f619 100644 (file)
@@ -2030,11 +2030,11 @@ fn copy_val_no_check(cx: @block_ctxt, action: copy_action, dst: ValueRef,
         ret take_ty(bcx, dst, t);
     }
     if ty::type_is_unique_box(ccx.tcx, t) {
-        //let bcx = cx;
+        let bcx = cx;
         // FIXME (409): Write a test and uncomment
         //if action == DROP_EXISTING { bcx = drop_ty(cx, dst, t); }
-        //ret trans_uniq::copy_val(bcx, dst, src, t);
-        fail;
+        check trans_uniq::type_is_unique_box(bcx, t);
+        ret trans_uniq::copy_val(bcx, dst, src, t);
     }
     if type_is_structural_or_param(ccx.tcx, t) || ty::type_is_vec(ccx.tcx, t)
         {
index ec285c57074217dfcdc0221316b6009b96856331..a26e322472327e36284ac2b0a3802e49c3eadb16 100644 (file)
@@ -15,7 +15,7 @@
     new_sub_block_ctxt
 };
 
-export trans_uniq, make_free_glue, type_is_unique_box;
+export trans_uniq, make_free_glue, type_is_unique_box, copy_val;
 
 pure fn type_is_unique_box(bcx: @block_ctxt, ty: ty::t) -> bool {
     unchecked {
@@ -35,6 +35,8 @@ fn trans_uniq(cx: @block_ctxt, contents: @ast::expr,
     let content_ty = content_ty(bcx, uniq_ty);
     let {bcx, val: llptr} = alloc_uniq(bcx, uniq_ty);
 
+    add_clean_temp(bcx, llptr, uniq_ty);
+
     bcx = move_val_if_temp(bcx, INIT, llptr, lv,
                            content_ty);
 
@@ -56,8 +58,6 @@ fn alloc_uniq(cx: @block_ctxt, uniq_ty: ty::t)
     bcx = r.bcx;
     let llptr = r.val;
 
-    add_clean_temp(bcx, llptr, uniq_ty);
-
     ret rslt(bcx, llptr);
 }
 
@@ -86,4 +86,18 @@ fn content_ty(bcx: @block_ctxt, t: ty::t)
     alt ty::struct(bcx_tcx(bcx), t) {
       ty::ty_uniq({ty: ct, _}) { ct }
     }
+}
+
+fn copy_val(cx: @block_ctxt, dst: ValueRef, src: ValueRef,
+            ty: ty::t) : type_is_unique_box(cx, ty) -> @block_ctxt {
+
+    let content_ty = content_ty(cx, ty);
+    let {bcx, val: llptr} = alloc_uniq(cx, ty);
+    Store(bcx, llptr, dst);
+
+    let src = Load(bcx, src);
+    let dst = Load(bcx, dst);
+    let bcx = trans::copy_val(bcx, INIT, dst, src, content_ty);
+    Store(bcx, src, llptr);
+    ret bcx;
 }
\ No newline at end of file
diff --git a/src/test/run-pass/unique-decl-init-copy.rs b/src/test/run-pass/unique-decl-init-copy.rs
new file mode 100644 (file)
index 0000000..d9808b6
--- /dev/null
@@ -0,0 +1,9 @@
+fn main() {
+    let i = ~mutable 1;
+    // Should be a copy
+    let j = i;
+    *i = 2;
+    *j = 3;
+    assert *i == 2;
+    assert *j == 3;
+}
\ No newline at end of file
diff --git a/src/test/run-pass/unique-decl-init.rs b/src/test/run-pass/unique-decl-init.rs
new file mode 100644 (file)
index 0000000..d8c6447
--- /dev/null
@@ -0,0 +1,5 @@
+fn main() {
+    let i = ~1;
+    let j = i;
+    assert *j == 1;
+}
\ No newline at end of file