From 268a9fe5fb3e6ac1f5bc3f1a7784da751cb56af8 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 22 Sep 2011 13:22:53 -0700 Subject: [PATCH] Initialize unique box locals from other locals Issue #409 --- src/comp/middle/trans.rs | 6 +++--- src/comp/middle/trans_uniq.rs | 20 +++++++++++++++++--- src/test/run-pass/unique-decl-init-copy.rs | 9 +++++++++ src/test/run-pass/unique-decl-init.rs | 5 +++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/test/run-pass/unique-decl-init-copy.rs create mode 100644 src/test/run-pass/unique-decl-init.rs diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 12be2f3f54a..555236e6415 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -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) { diff --git a/src/comp/middle/trans_uniq.rs b/src/comp/middle/trans_uniq.rs index ec285c57074..a26e3224723 100644 --- a/src/comp/middle/trans_uniq.rs +++ b/src/comp/middle/trans_uniq.rs @@ -15,7 +15,7 @@ import trans::{ 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 index 00000000000..d9808b6f50d --- /dev/null +++ b/src/test/run-pass/unique-decl-init-copy.rs @@ -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 index 00000000000..d8c6447396c --- /dev/null +++ b/src/test/run-pass/unique-decl-init.rs @@ -0,0 +1,5 @@ +fn main() { + let i = ~1; + let j = i; + assert *j == 1; +} \ No newline at end of file