diff options
| author | Niko Matsakis <niko@alum.mit.edu> | 2015-04-09 14:49:03 -0400 |
|---|---|---|
| committer | Niko Matsakis <niko@alum.mit.edu> | 2015-04-10 06:11:28 -0400 |
| commit | e313b3334b82676bbdcf90cd48f08b87fc0d03da (patch) | |
| tree | a7abe6b39044c1e8449986ceb8f591b3b99ef8ad /src/test | |
| parent | 906a9728ffd4cd9f40c96d7704260baf17845651 (diff) | |
| download | rust-e313b3334b82676bbdcf90cd48f08b87fc0d03da.tar.gz rust-e313b3334b82676bbdcf90cd48f08b87fc0d03da.zip | |
Improve error message where a closure escapes fn while trying to borrow
from the current fn. Employ the new `span_suggestion` to show how you can use `move`.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/compile-fail/borrowck-escaping-closure-error-1.rs | 25 | ||||
| -rw-r--r-- | src/test/compile-fail/borrowck-escaping-closure-error-2.rs | 25 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-16747.rs | 4 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-4335.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/regions-nested-fns-2.rs | 2 |
5 files changed, 54 insertions, 4 deletions
diff --git a/src/test/compile-fail/borrowck-escaping-closure-error-1.rs b/src/test/compile-fail/borrowck-escaping-closure-error-1.rs new file mode 100644 index 00000000000..87e40df7663 --- /dev/null +++ b/src/test/compile-fail/borrowck-escaping-closure-error-1.rs @@ -0,0 +1,25 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::thread::spawn; + +// Test that we give a custom error (E0373) for the case where a +// closure is escaping current frame, and offer a suggested code edit. +// I refrained from including the precise message here, but the +// original text as of the time of this writing is: +// +// closure may outlive the current function, but it borrows `books`, +// which is owned by the current function + +fn main() { + let mut books = vec![1,2,3]; + spawn(|| books.push(4)); + //~^ ERROR E0373 +} diff --git a/src/test/compile-fail/borrowck-escaping-closure-error-2.rs b/src/test/compile-fail/borrowck-escaping-closure-error-2.rs new file mode 100644 index 00000000000..67700be890b --- /dev/null +++ b/src/test/compile-fail/borrowck-escaping-closure-error-2.rs @@ -0,0 +1,25 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we give a custom error (E0373) for the case where a +// closure is escaping current frame, and offer a suggested code edit. +// I refrained from including the precise message here, but the +// original text as of the time of this writing is: +// +// closure may outlive the current function, but it borrows `books`, +// which is owned by the current function + +fn foo<'a>(x: &'a i32) -> Box<FnMut()+'a> { + let mut books = vec![1,2,3]; + Box::new(|| books.push(4)) + //~^ ERROR E0373 +} + +fn main() { } diff --git a/src/test/compile-fail/issue-16747.rs b/src/test/compile-fail/issue-16747.rs index 64334fe4392..b4abef0bd28 100644 --- a/src/test/compile-fail/issue-16747.rs +++ b/src/test/compile-fail/issue-16747.rs @@ -18,10 +18,10 @@ trait Collection { fn len(&self) -> usize; } struct List<'a, T: ListItem<'a>> { //~^ ERROR the parameter type `T` may not live long enough -//~^^ NOTE ...so that the reference type `&'a [T]` does not outlive the data it points at +//~| HELP consider adding an explicit lifetime bound +//~| NOTE ...so that the reference type `&'a [T]` does not outlive the data it points at slice: &'a [T] } -//~^ HELP consider adding an explicit lifetime bound impl<'a, T: ListItem<'a>> Collection for List<'a, T> { fn len(&self) -> usize { 0 diff --git a/src/test/compile-fail/issue-4335.rs b/src/test/compile-fail/issue-4335.rs index 55a793f7480..0089bff3e8f 100644 --- a/src/test/compile-fail/issue-4335.rs +++ b/src/test/compile-fail/issue-4335.rs @@ -15,7 +15,7 @@ fn id<T>(t: T) -> T { t } fn f<'r, T>(v: &'r T) -> Box<FnMut() -> T + 'r> { // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. id(Box::new(|| *v)) - //~^ ERROR `v` does not live long enough + //~^ ERROR E0373 //~| ERROR cannot move out of borrowed content } diff --git a/src/test/compile-fail/regions-nested-fns-2.rs b/src/test/compile-fail/regions-nested-fns-2.rs index bdebadb2832..948dc8cd219 100644 --- a/src/test/compile-fail/regions-nested-fns-2.rs +++ b/src/test/compile-fail/regions-nested-fns-2.rs @@ -13,7 +13,7 @@ fn ignore<F>(_f: F) where F: for<'z> FnOnce(&'z isize) -> &'z isize {} fn nested() { let y = 3; ignore( - |z| { //~ ERROR `y` does not live long enough + |z| { //~ ERROR E0373 if false { &y } else { z } }); } |
