Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can borrow mut in proc Issue 10617 #11626

Merged
merged 1 commit into from
Jan 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {}