Skip to content

Commit

Permalink
librustc: Also use new alloca if matching on an arg or upvar which we…
Browse files Browse the repository at this point in the history
… reassign in the arm body.
  • Loading branch information
luqmana committed Aug 9, 2014
1 parent d7c0f7d commit 5dca9fb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/librustc/middle/trans/_match.rs
Expand Up @@ -1298,7 +1298,8 @@ pub fn trans_match<'a>(
fn is_discr_reassigned(bcx: &Block, discr: &ast::Expr, body: &ast::Expr) -> bool {
match discr.node {
ast::ExprPath(..) => match bcx.def(discr.id) {
def::DefLocal(vid, _) | def::DefBinding(vid, _) => {
def::DefArg(vid, _) | def::DefBinding(vid, _) |
def::DefLocal(vid, _) | def::DefUpvar(vid, _, _, _) => {
let mut rc = ReassignmentChecker {
node: vid,
reassigned: false
Expand Down Expand Up @@ -1326,9 +1327,11 @@ impl euv::Delegate for ReassignmentChecker {
fn borrow(&mut self, _: ast::NodeId, _: Span, _: mc::cmt, _: ty::Region,
_: ty::BorrowKind, _: euv::LoanCause) {}
fn decl_without_init(&mut self, _: ast::NodeId, _: Span) {}

fn mutate(&mut self, _: ast::NodeId, _: Span, cmt: mc::cmt, _: euv::MutateMode) {
match cmt.cat {
mc::cat_local(vid) => self.reassigned = self.node == vid,
mc::cat_copied_upvar(mc::CopiedUpvar { upvar_id: vid, .. }) |
mc::cat_arg(vid) | mc::cat_local(vid) => self.reassigned = self.node == vid,
_ => {}
}
}
Expand Down
48 changes: 46 additions & 2 deletions src/test/run-pass/issue-15571.rs
Expand Up @@ -8,13 +8,57 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
fn match_on_local() {
let mut foo = Some(box 5i);
match foo {
None => {},
Some(x) => {
foo = Some(x);
}
};
}
println!("'{}'", foo.unwrap());
}

fn match_on_arg(mut foo: Option<Box<int>>) {
match foo {
None => {}
Some(x) => {
foo = Some(x);
}
}
println!("'{}'", foo.unwrap());
}

fn match_on_binding() {
match Some(box 7i) {
mut foo => {
match foo {
None => {},
Some(x) => {
foo = Some(x);
}
}
println!("'{}'", foo.unwrap());
}
}
}

fn match_on_upvar() {
let mut foo = Some(box 8i);
(proc() {
match foo {
None => {},
Some(x) => {
foo = Some(x);
}
}
println!("'{}'", foo.unwrap());
})();
}

fn main() {
match_on_local();
match_on_arg(Some(box 6i));
match_on_binding();
match_on_upvar();
}

0 comments on commit 5dca9fb

Please sign in to comment.