Skip to content

Commit

Permalink
auto merge of rust-lang#11626 : nickdesaulniers/rust/issue10617, r=al…
Browse files Browse the repository at this point in the history
…excrichton

rust-lang#10617
  • Loading branch information
bors committed Jan 28, 2014
2 parents daf60f0 + ea9db66 commit 8c6c229
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
22 changes: 1 addition & 21 deletions src/librustc/middle/kind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand Down Expand Up @@ -184,9 +184,6 @@ fn with_appropriate_checker(cx: &Context,
let id = ast_util::def_id_of_def(fv.def).node;
let var_t = ty::node_id_to_type(cx.tcx, id);

// check that only immutable variables are implicitly copied in
check_imm_free_var(cx, fv.def, fv.span);

check_freevar_bounds(cx, fv.span, var_t, bounds, None);
}

Expand Down Expand Up @@ -447,23 +444,6 @@ pub fn check_trait_cast_bounds(cx: &Context, sp: Span, ty: ty::t,
});
}

fn check_imm_free_var(cx: &Context, def: Def, sp: Span) {
match def {
DefLocal(_, BindByValue(MutMutable)) => {
cx.tcx.sess.span_err(
sp,
"mutable variables cannot be implicitly captured");
}
DefLocal(..) | DefArg(..) | DefBinding(..) => { /* ok */ }
DefUpvar(_, def1, _, _) => { check_imm_free_var(cx, *def1, sp); }
_ => {
cx.tcx.sess.span_bug(
sp,
format!("unknown def for free variable: {:?}", def));
}
}
}

fn check_copy(cx: &Context, ty: ty::t, sp: Span, reason: &str) {
debug!("type_contents({})={}",
ty_to_str(cx.tcx, ty),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -13,38 +13,41 @@ use std::task;
fn user(_i: int) {}

fn foo() {
// Here, i is *moved* into the closure: Not actually OK
// Here, i is *copied* into the proc (heap closure).
// Requires allocation. The proc's copy is not mutable.
let mut i = 0;
do task::spawn {
user(i); //~ ERROR mutable variables cannot be implicitly captured
user(i);
println!("spawned {}", i)
}
i += 1;
println!("original {}", i)
}

fn bar() {
// Here, i would be implicitly *copied* but it
// is mutable: bad
// Here, the original i has not been moved, only copied, so is still
// mutable outside of the proc.
let mut i = 0;
while i < 10 {
do task::spawn {
user(i); //~ ERROR mutable variables cannot be implicitly captured
user(i);
}
i += 1;
}
}

fn car() {
// Here, i is mutable, but *explicitly* shadowed copied:
// Here, i must be shadowed in the proc to be mutable.
let mut i = 0;
while i < 10 {
{
let i = i;
do task::spawn {
user(i);
}
do task::spawn {
let mut i = i;
i += 1;
user(i);
}
i += 1;
}
}

fn main() {
}
pub fn main() {}

0 comments on commit 8c6c229

Please sign in to comment.