Skip to content

Commit b744f52

Browse files
committed
Make heap closures always require a capture clause to capture mutable variables. Closes #2446.
1 parent 736ff34 commit b744f52

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/rustc/middle/kind.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ fn with_appropriate_checker(cx: ctx, id: node_id, b: fn(check_fn)) {
9696
if !is_move { check_copy(cx, var_t, sp, is_implicit); }
9797

9898
// check that only immutable variables are implicitly copied in
99-
if !is_move {
100-
for fv.each { |fv|
101-
check_imm_free_var(cx, fv.def, fv.span);
102-
}
99+
for fv.each { |fv|
100+
check_imm_free_var(cx, fv.def, fv.span);
103101
}
104102
}
105103

@@ -110,10 +108,8 @@ fn with_appropriate_checker(cx: ctx, id: node_id, b: fn(check_fn)) {
110108
if !is_move { check_copy(cx, var_t, sp, is_implicit); }
111109

112110
// check that only immutable variables are implicitly copied in
113-
if !is_move {
114-
for fv.each { |fv|
115-
check_imm_free_var(cx, fv.def, fv.span);
116-
}
111+
for fv.each { |fv|
112+
check_imm_free_var(cx, fv.def, fv.span);
117113
}
118114
}
119115

@@ -391,8 +387,8 @@ fn check_imm_free_var(cx: ctx, def: def, sp: span) {
391387
}
392388
def_arg(_, mode) {
393389
alt ty::resolved_mode(cx.tcx, mode) {
394-
by_ref | by_val { /* ok */ }
395-
by_mutbl_ref | by_move | by_copy {
390+
by_ref | by_val | by_move | by_copy { /* ok */ }
391+
by_mutbl_ref {
396392
cx.tcx.sess.span_err(sp, msg);
397393
}
398394
}

src/test/compile-fail/kindck-implicit-close-over-mut-var.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
fn use(_i: int) {}
22

33
fn foo() {
4-
// Here, i is *moved* into the closure: OK
4+
// Here, i is *moved* into the closure: Not actually OK
55
let mut i = 0;
66
task::spawn {||
7-
use(i);
7+
use(i); //! ERROR mutable variables cannot be implicitly captured
88
}
99
}
1010

@@ -32,4 +32,4 @@ fn car() {
3232
}
3333

3434
fn main() {
35-
}
35+
}

0 commit comments

Comments
 (0)