Skip to content

Commit ea9db66

Browse files
author
Nick Desaulniers
committed
can borrow mut in proc Fixes #10617
1 parent 4176343 commit ea9db66

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

src/librustc/middle/kind.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -184,9 +184,6 @@ fn with_appropriate_checker(cx: &Context,
184184
let id = ast_util::def_id_of_def(fv.def).node;
185185
let var_t = ty::node_id_to_type(cx.tcx, id);
186186

187-
// check that only immutable variables are implicitly copied in
188-
check_imm_free_var(cx, fv.def, fv.span);
189-
190187
check_freevar_bounds(cx, fv.span, var_t, bounds, None);
191188
}
192189

@@ -447,23 +444,6 @@ pub fn check_trait_cast_bounds(cx: &Context, sp: Span, ty: ty::t,
447444
});
448445
}
449446

450-
fn check_imm_free_var(cx: &Context, def: Def, sp: Span) {
451-
match def {
452-
DefLocal(_, BindByValue(MutMutable)) => {
453-
cx.tcx.sess.span_err(
454-
sp,
455-
"mutable variables cannot be implicitly captured");
456-
}
457-
DefLocal(..) | DefArg(..) | DefBinding(..) => { /* ok */ }
458-
DefUpvar(_, def1, _, _) => { check_imm_free_var(cx, *def1, sp); }
459-
_ => {
460-
cx.tcx.sess.span_bug(
461-
sp,
462-
format!("unknown def for free variable: {:?}", def));
463-
}
464-
}
465-
}
466-
467447
fn check_copy(cx: &Context, ty: ty::t, sp: Span, reason: &str) {
468448
debug!("type_contents({})={}",
469449
ty_to_str(cx.tcx, ty),
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -13,38 +13,41 @@ use std::task;
1313
fn user(_i: int) {}
1414

1515
fn foo() {
16-
// Here, i is *moved* into the closure: Not actually OK
16+
// Here, i is *copied* into the proc (heap closure).
17+
// Requires allocation. The proc's copy is not mutable.
1718
let mut i = 0;
1819
do task::spawn {
19-
user(i); //~ ERROR mutable variables cannot be implicitly captured
20+
user(i);
21+
println!("spawned {}", i)
2022
}
23+
i += 1;
24+
println!("original {}", i)
2125
}
2226

2327
fn bar() {
24-
// Here, i would be implicitly *copied* but it
25-
// is mutable: bad
28+
// Here, the original i has not been moved, only copied, so is still
29+
// mutable outside of the proc.
2630
let mut i = 0;
2731
while i < 10 {
2832
do task::spawn {
29-
user(i); //~ ERROR mutable variables cannot be implicitly captured
33+
user(i);
3034
}
3135
i += 1;
3236
}
3337
}
3438

3539
fn car() {
36-
// Here, i is mutable, but *explicitly* shadowed copied:
40+
// Here, i must be shadowed in the proc to be mutable.
3741
let mut i = 0;
3842
while i < 10 {
39-
{
40-
let i = i;
41-
do task::spawn {
42-
user(i);
43-
}
43+
do task::spawn {
44+
let mut i = i;
45+
i += 1;
46+
user(i);
4447
}
4548
i += 1;
4649
}
4750
}
4851

49-
fn main() {
50-
}
52+
pub fn main() {}
53+

0 commit comments

Comments
 (0)