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.
This commit is contained in:
Tim Chevalier 2011-06-10 13:39:13 -07:00
parent f1431adb40
commit e5eacf8ea9
2 changed files with 15 additions and 0 deletions

View File

@ -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;
}

View File

@ -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);
}