librustc: Also use new alloca if matching on an arg or upvar which we reassign in the arm body.

This commit is contained in:
Luqman Aden 2014-08-04 18:58:06 -07:00
parent d7c0f7d1c0
commit 5dca9fb261
2 changed files with 51 additions and 4 deletions

View file

@ -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
@ -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,
_ => {}
}
}

View file

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