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

Point at variable moved by closure #41523

Merged
merged 1 commit into from
Apr 28, 2017
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
7 changes: 5 additions & 2 deletions src/librustc_borrowck/borrowck/gather_loans/move_error.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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