Skip to content

Commit

Permalink
Return a better result from blocks. Closes issue #377
Browse files Browse the repository at this point in the history
Blocks return in a copy of the result of their ending expression, not the
direct result of the ending expression, as that may be a local variable which
gets zeroed by drop_slot.
  • Loading branch information
brson committed May 17, 2011
1 parent 4539b3f commit fbfd855
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/comp/middle/trans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6252,6 +6252,9 @@ fn trans_block(&@block_ctxt cx, &ast::block b) -> result {
auto cleanup = bind drop_hoisted_ty(_, res_alloca.val,
r_ty);
find_outer_scope_cx(bcx).cleanups += [clean(cleanup)];

r = res(bcx, load_if_immediate(bcx,
res_alloca.val, r_ty));
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/run-pass/expr-block-slot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// xfail-stage0

// Regression test for issue #377

fn main() {
auto a = {
auto b = tup(3);
b
};

assert a._0 == 3;

auto c = {
auto d = rec(v=3);
d
};

assert c.v == 3;
}

0 comments on commit fbfd855

Please sign in to comment.