From e5eacf8ea9e59777d6c7d4939d91d2e96dfff838 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Fri, 10 Jun 2011 13:39:13 -0700 Subject: [PATCH] Use RHS type when translating assignments In code like "auto foo = fail", a type gets inferred for foo depending on how it's used. However, fail still has type _|_ and still should be treated that way: particularly, its value shouldn't be copied. Fixed trans to reflect that. --- src/comp/middle/trans.rs | 6 ++++++ src/test/run-fail/rhs-type.rs | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 src/test/run-fail/rhs-type.rs diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs index 32bd47dc0c9..2c0965ead2f 100644 --- a/src/comp/middle/trans.rs +++ b/src/comp/middle/trans.rs @@ -3286,6 +3286,7 @@ fn copy_val(&@block_ctxt cx, ValueRef dst, ValueRef src, &ty::t t) -> result { + if (ty::type_is_scalar(cx.fcx.lcx.ccx.tcx, t) || ty::type_is_native(cx.fcx.lcx.ccx.tcx, t)) { ret res(cx, cx.build.Store(src, dst)); @@ -6646,6 +6647,11 @@ fn init_local(&@block_ctxt cx, &@ast::local local) -> result { case (some(?init)) { alt (init.op) { case (ast::init_assign) { + // Use the type of the RHS because if it's _|_, the LHS + // type might be something else, but we don't want to copy + // the value. + ty = node_ann_type(cx.fcx.lcx.ccx, + ty::expr_ann(init.expr)); auto sub = trans_expr(bcx, init.expr); bcx = copy_val(sub.bcx, INIT, llptr, sub.val, ty).bcx; } diff --git a/src/test/run-fail/rhs-type.rs b/src/test/run-fail/rhs-type.rs new file mode 100644 index 00000000000..593822be3cb --- /dev/null +++ b/src/test/run-fail/rhs-type.rs @@ -0,0 +1,9 @@ +// Tests that trans treats the rhs of pth's decl +// as a _|_-typed thing, not a str-typed thing +// error-pattern:bye +fn main() { + auto pth = fail "bye"; + + let rec(str t) res = rec(t=pth); + +}