Skip to content

Commit

Permalink
Rollup merge of rust-lang#41523 - estebank:moved, r=arielb1
Browse files Browse the repository at this point in the history
Point at variable moved by closure

Fix rust-lang#41482, rust-lang#31752.
  • Loading branch information
frewsxcv committed Apr 27, 2017
2 parents 35227db + 1ca1483 commit 9a9420e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Expand Up @@ -11,6 +11,7 @@
use borrowck::BorrowckCtxt;
use rustc::middle::mem_categorization as mc;
use rustc::middle::mem_categorization::Categorization;
use rustc::middle::mem_categorization::NoteClosureEnv;
use rustc::middle::mem_categorization::InteriorOffsetKind as Kind;
use rustc::ty;
use syntax::ast;
Expand Down Expand Up @@ -71,10 +72,12 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
let mut is_first_note = true;
for move_to in &error.move_to_places {
err = note_move_destination(err, move_to.span,
move_to.name, is_first_note);
err = note_move_destination(err, move_to.span, move_to.name, is_first_note);
is_first_note = false;
}
if let NoteClosureEnv(upvar_id) = error.move_from.note {
err.span_label(bccx.tcx.hir.span(upvar_id.var_id), &"captured outer variable");
}
err.emit();
}
}
Expand Down
Expand Up @@ -11,7 +11,7 @@
// check that borrowck looks inside consts/statics

static FN : &'static (Fn() -> (Box<Fn()->Box<i32>>) + Sync) = &|| {
let x = Box::new(0);
let x = Box::new(0); //~ NOTE moved
Box::new(|| x) //~ ERROR cannot move out of captured outer variable
};

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/borrowck/borrowck-in-static.stderr
@@ -0,0 +1,10 @@
error[E0507]: cannot move out of captured outer variable in an `Fn` closure
--> $DIR/borrowck-in-static.rs:15:17
|
14 | let x = Box::new(0); //~ NOTE moved
| - captured outer variable
15 | Box::new(|| x) //~ ERROR cannot move out of captured outer variable
| ^ cannot move out of captured outer variable in an `Fn` closure

error: aborting due to previous error

Expand Up @@ -16,7 +16,7 @@ fn call<F>(f: F) where F : Fn() {
}

fn main() {
let y = vec![format!("World")];
let y = vec![format!("World")]; //~ NOTE moved
call(|| {
y.into_iter();
//~^ ERROR cannot move out of captured outer variable in an `Fn` closure
Expand Down
@@ -0,0 +1,11 @@
error[E0507]: cannot move out of captured outer variable in an `Fn` closure
--> $DIR/unboxed-closures-move-upvar-from-non-once-ref-closure.rs:21:9
|
19 | let y = vec![format!("World")]; //~ NOTE moved
| - captured outer variable
20 | call(|| {
21 | y.into_iter();
| ^ cannot move out of captured outer variable in an `Fn` closure

error: aborting due to previous error

17 changes: 15 additions & 2 deletions src/test/ui/span/borrowck-call-is-borrow-issue-12224.rs
Expand Up @@ -21,8 +21,11 @@ struct Test<'a> {
fn call<F>(mut f: F) where F: FnMut(Fn) {
f(Box::new(|| {
//~^ ERROR: cannot borrow `f` as mutable more than once
//~| NOTE first mutable borrow occurs here
//~| NOTE second mutable borrow occurs here
f((Box::new(|| {})))
}));
//~^ NOTE first borrow ends here
}

fn test1() {
Expand All @@ -32,15 +35,21 @@ fn test1() {
}

fn test2<F>(f: &F) where F: FnMut() {
(*f)(); //~ ERROR: cannot borrow immutable borrowed content `*f` as mutable
//~^ NOTE use `&mut F` here to make mutable
(*f)();
//~^ ERROR cannot borrow immutable borrowed content `*f` as mutable
//~| NOTE cannot borrow as mutable
}

fn test3<F>(f: &mut F) where F: FnMut() {
(*f)();
}

fn test4(f: &Test) {
f.f.call_mut(()) //~ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
//~^ NOTE use `&mut Test` here to make mutable
f.f.call_mut(())
//~^ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
//~| NOTE cannot borrow as mutable
}

fn test5(f: &mut Test) {
Expand All @@ -57,10 +66,14 @@ fn test6() {
fn test7() {
fn foo<F>(_: F) where F: FnMut(Box<FnMut(isize)>, isize) {}
let mut f = |g: Box<FnMut(isize)>, b: isize| {};
//~^ NOTE moved
f(Box::new(|a| {
//~^ NOTE borrow of `f` occurs here
foo(f);
//~^ ERROR cannot move `f` into closure because it is borrowed
//~| ERROR cannot move out of captured outer variable in an `FnMut` closure
//~| NOTE move into closure occurs here
//~| NOTE cannot move out of captured outer variable in an `FnMut` closure
}), 3);
}

Expand Down
34 changes: 20 additions & 14 deletions src/test/ui/span/borrowck-call-is-borrow-issue-12224.stderr
Expand Up @@ -5,40 +5,46 @@ error[E0499]: cannot borrow `f` as mutable more than once at a time
| - ^^ second mutable borrow occurs here
| |
| first mutable borrow occurs here
23 | //~^ ERROR: cannot borrow `f` as mutable more than once
24 | f((Box::new(|| {})))
...
26 | f((Box::new(|| {})))
| - borrow occurs due to use of `f` in closure
25 | }));
27 | }));
| - first borrow ends here

error: cannot borrow immutable borrowed content `*f` as mutable
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:35:5
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:39:5
|
34 | fn test2<F>(f: &F) where F: FnMut() {
37 | fn test2<F>(f: &F) where F: FnMut() {
| -- use `&mut F` here to make mutable
35 | (*f)(); //~ ERROR: cannot borrow immutable borrowed content `*f` as mutable
38 | //~^ NOTE use `&mut F` here to make mutable
39 | (*f)();
| ^^^^ cannot borrow as mutable

error: cannot borrow immutable `Box` content `*f.f` as mutable
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:43:5
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:50:5
|
42 | fn test4(f: &Test) {
48 | fn test4(f: &Test) {
| ----- use `&mut Test` here to make mutable
43 | f.f.call_mut(()) //~ ERROR: cannot borrow immutable `Box` content `*f.f` as mutable
49 | //~^ NOTE use `&mut Test` here to make mutable
50 | f.f.call_mut(())
| ^^^ cannot borrow as mutable

error[E0504]: cannot move `f` into closure because it is borrowed
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:61:13
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:72:13
|
60 | f(Box::new(|a| {
70 | f(Box::new(|a| {
| - borrow of `f` occurs here
61 | foo(f);
71 | //~^ NOTE borrow of `f` occurs here
72 | foo(f);
| ^ move into closure occurs here

error[E0507]: cannot move out of captured outer variable in an `FnMut` closure
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:61:13
--> $DIR/borrowck-call-is-borrow-issue-12224.rs:72:13
|
61 | foo(f);
68 | let mut f = |g: Box<FnMut(isize)>, b: isize| {};
| ----- captured outer variable
...
72 | foo(f);
| ^ cannot move out of captured outer variable in an `FnMut` closure

error: aborting due to 5 previous errors
Expand Down

0 comments on commit 9a9420e

Please sign in to comment.