Skip to content

Commit 4d325b1

Browse files
committed
Spill instead of copy when binding a pattern that's only a var binding
The alias rules guarantee that the alt-ed value will outlive the binding's last use, so this is safe. This is a preparation for or-patterns. Joining scoped bindings seems like it would get messy.
1 parent 1e96541 commit 4d325b1

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/comp/middle/trans.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,24 +4843,18 @@ fn trans_pat_match(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
48434843
}
48444844

48454845
fn trans_pat_binding(&@block_ctxt cx, &@ast::pat pat, ValueRef llval,
4846-
bool bind_alias) -> result {
4846+
bool is_mem) -> result {
48474847
alt (pat.node) {
48484848
case (ast::pat_wild) { ret rslt(cx, llval); }
48494849
case (ast::pat_lit(_)) { ret rslt(cx, llval); }
48504850
case (ast::pat_bind(?name)) {
4851-
if (bind_alias) {
4852-
cx.fcx.lllocals.insert(pat.id, llval);
4853-
ret rslt(cx, llval);
4854-
} else {
4855-
auto t = node_id_type(cx.fcx.lcx.ccx, pat.id);
4856-
auto rslt = alloc_ty(cx, t);
4857-
auto dst = rslt.val;
4858-
auto bcx = rslt.bcx;
4859-
maybe_name_value(cx.fcx.lcx.ccx, dst, name);
4860-
bcx.fcx.lllocals.insert(pat.id, dst);
4861-
add_clean(bcx, dst, t);
4862-
ret copy_val(bcx, INIT, dst, llval, t);
4851+
auto val = llval;
4852+
if (!is_mem) {
4853+
val = spill_if_immediate
4854+
(cx, llval, node_id_type(cx.fcx.lcx.ccx, pat.id));
48634855
}
4856+
cx.fcx.lllocals.insert(pat.id, val);
4857+
ret rslt(cx, val);
48644858
}
48654859
case (ast::pat_tag(_, ?subpats)) {
48664860
if (std::ivec::len[@ast::pat](subpats) == 0u) {
@@ -4911,11 +4905,11 @@ fn trans_alt(&@block_ctxt cx, &@ast::expr expr, &ast::arm[] arms,
49114905
auto next_cx = new_sub_block_ctxt(expr_res.bcx, "next");
49124906
auto match_res =
49134907
trans_pat_match(this_cx, arm.pat, expr_res.val, next_cx);
4914-
auto binding_cx = new_scope_block_ctxt(match_res.bcx, "binding");
4915-
match_res.bcx.build.Br(binding_cx.llbb);
49164908
auto binding_res =
4917-
trans_pat_binding(binding_cx, arm.pat, expr_res.val, false);
4918-
auto block_res = trans_block(binding_res.bcx, arm.block, output);
4909+
trans_pat_binding(match_res.bcx, arm.pat, expr_res.val, false);
4910+
auto block_cx = new_scope_block_ctxt(match_res.bcx, "case block");
4911+
binding_res.bcx.build.Br(block_cx.llbb);
4912+
auto block_res = trans_block(block_cx, arm.block, output);
49194913
arm_results += ~[block_res];
49204914
this_cx = next_cx;
49214915
}

0 commit comments

Comments
 (0)