can borrow mut in proc Fixes #10617
This commit is contained in:
parent
4176343073
commit
ea9db66c50
2 changed files with 18 additions and 35 deletions
|
|
@ -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.
|
||||
//
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
//
|
||||
|
|
@ -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() {}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue