mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-05 19:43:24 +00:00
Point at variable moved by closure
This commit is contained in:
parent
2bd4b5c6db
commit
1ca1483113
@ -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;
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
};
|
||||
|
10
src/test/ui/borrowck/borrowck-in-static.stderr
Normal file
10
src/test/ui/borrowck/borrowck-in-static.stderr
Normal file
@ -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
|
||||
|
@ -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
|
@ -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
|
||||
|
@ -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() {
|
||||
@ -32,7 +35,10 @@ 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() {
|
||||
@ -40,7 +46,10 @@ fn test3<F>(f: &mut F) where F: FnMut() {
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user