diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2018-01-15 10:55:10 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2018-01-15 11:12:25 -0800 |
| commit | 1820da52110cc5cfb40436225c939067049ccf32 (patch) | |
| tree | bef57cf7de150c7d4c47dadfedbcc9568cea9532 /src/test/ui | |
| parent | 1a1afd74a643cead4794094e286adc9cf5ae2009 (diff) | |
| download | rust-1820da52110cc5cfb40436225c939067049ccf32.tar.gz rust-1820da52110cc5cfb40436225c939067049ccf32.zip | |
Move diagnostic logic to its own module
- Move specialized borrow checker diagnostic for bindings escaping its closure to its own module. - Move affected tests to `ui`.
Diffstat (limited to 'src/test/ui')
| -rw-r--r-- | src/test/ui/borrowck/issue-45983.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/borrowck/issue-45983.stderr | 6 | ||||
| -rw-r--r-- | src/test/ui/borrowck/issue-7573.rs | 53 | ||||
| -rw-r--r-- | src/test/ui/borrowck/issue-7573.stderr | 14 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-bound-fn-2.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-bound-fn-2.stderr | 12 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-bound-fn.rs | 20 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-bound-fn.stderr | 12 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-unboxed-closure.rs | 18 | ||||
| -rw-r--r-- | src/test/ui/borrowck/regions-escape-unboxed-closure.stderr | 12 | ||||
| -rw-r--r-- | src/test/ui/closure-expected-type/expect-region-supply-region.rs | 80 | ||||
| -rw-r--r-- | src/test/ui/closure-expected-type/expect-region-supply-region.stderr | 100 |
12 files changed, 345 insertions, 4 deletions
diff --git a/src/test/ui/borrowck/issue-45983.rs b/src/test/ui/borrowck/issue-45983.rs index b2316a6b61c..a6e5067195f 100644 --- a/src/test/ui/borrowck/issue-45983.rs +++ b/src/test/ui/borrowck/issue-45983.rs @@ -15,5 +15,5 @@ fn give_any<F: for<'r> FnOnce(&'r ())>(f: F) { fn main() { let x = None; give_any(|y| x = Some(y)); - //~^ ERROR borrowed data cannot be moved outside of its closure + //~^ ERROR borrowed data cannot be stored outside of its closure } diff --git a/src/test/ui/borrowck/issue-45983.stderr b/src/test/ui/borrowck/issue-45983.stderr index cbc4a557be9..496f15c289c 100644 --- a/src/test/ui/borrowck/issue-45983.stderr +++ b/src/test/ui/borrowck/issue-45983.stderr @@ -1,10 +1,10 @@ -error: borrowed data cannot be moved outside of its closure +error: borrowed data cannot be stored outside of its closure --> $DIR/issue-45983.rs:17:27 | 16 | let x = None; - | - borrowed data cannot be moved into here... + | - borrowed data cannot be stored into here... 17 | give_any(|y| x = Some(y)); - | --- ^ cannot be moved outside of its closure + | --- ^ cannot be stored outside of its closure | | | ...because it cannot outlive this closure diff --git a/src/test/ui/borrowck/issue-7573.rs b/src/test/ui/borrowck/issue-7573.rs new file mode 100644 index 00000000000..b318bd70cf2 --- /dev/null +++ b/src/test/ui/borrowck/issue-7573.rs @@ -0,0 +1,53 @@ +// Copyright 2013 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. + + +pub struct CrateId { + local_path: String, + junk: String +} + +impl CrateId { + fn new(s: &str) -> CrateId { + CrateId { + local_path: s.to_string(), + junk: "wutevs".to_string() + } + } +} + +pub fn remove_package_from_database() { + let mut lines_to_use: Vec<&CrateId> = Vec::new(); + //~^ NOTE cannot infer an appropriate lifetime + let push_id = |installed_id: &CrateId| { + //~^ NOTE borrowed data cannot outlive this closure + lines_to_use.push(installed_id); + //~^ ERROR borrowed data cannot be stored outside of its closure + //~| NOTE cannot be stored outside of its closure + }; + list_database(push_id); + + for l in &lines_to_use { + println!("{}", l.local_path); + } + +} + +pub fn list_database<F>(mut f: F) where F: FnMut(&CrateId) { + let stuff = ["foo", "bar"]; + + for l in &stuff { + f(&CrateId::new(*l)); + } +} + +pub fn main() { + remove_package_from_database(); +} diff --git a/src/test/ui/borrowck/issue-7573.stderr b/src/test/ui/borrowck/issue-7573.stderr new file mode 100644 index 00000000000..6c549a029b6 --- /dev/null +++ b/src/test/ui/borrowck/issue-7573.stderr @@ -0,0 +1,14 @@ +error: borrowed data cannot be stored outside of its closure + --> $DIR/issue-7573.rs:31:27 + | +27 | let mut lines_to_use: Vec<&CrateId> = Vec::new(); + | - cannot infer an appropriate lifetime +28 | //~^ NOTE cannot infer an appropriate lifetime +29 | let push_id = |installed_id: &CrateId| { + | ------------------------ borrowed data cannot outlive this closure +30 | //~^ NOTE borrowed data cannot outlive this closure +31 | lines_to_use.push(installed_id); + | ^^^^^^^^^^^^ cannot be stored outside of its closure + +error: aborting due to previous error + diff --git a/src/test/ui/borrowck/regions-escape-bound-fn-2.rs b/src/test/ui/borrowck/regions-escape-bound-fn-2.rs new file mode 100644 index 00000000000..1c38dee99a7 --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-bound-fn-2.rs @@ -0,0 +1,20 @@ +// Copyright 2012 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. + +fn with_int<F>(f: F) where F: FnOnce(&isize) { + let x = 3; + f(&x); +} + +fn main() { + let mut x = None; + with_int(|y| x = Some(y)); + //~^ ERROR borrowed data cannot be stored outside of its closure +} diff --git a/src/test/ui/borrowck/regions-escape-bound-fn-2.stderr b/src/test/ui/borrowck/regions-escape-bound-fn-2.stderr new file mode 100644 index 00000000000..3d88f4fd52e --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-bound-fn-2.stderr @@ -0,0 +1,12 @@ +error: borrowed data cannot be stored outside of its closure + --> $DIR/regions-escape-bound-fn-2.rs:18:27 + | +17 | let mut x = None; + | ----- borrowed data cannot be stored into here... +18 | with_int(|y| x = Some(y)); + | --- ^ cannot be stored outside of its closure + | | + | ...because it cannot outlive this closure + +error: aborting due to previous error + diff --git a/src/test/ui/borrowck/regions-escape-bound-fn.rs b/src/test/ui/borrowck/regions-escape-bound-fn.rs new file mode 100644 index 00000000000..c22742371ac --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-bound-fn.rs @@ -0,0 +1,20 @@ +// Copyright 2012 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. + +fn with_int<F>(f: F) where F: FnOnce(&isize) { + let x = 3; + f(&x); +} + +fn main() { + let mut x: Option<&isize> = None; + with_int(|y| x = Some(y)); + //~^ ERROR borrowed data cannot be stored outside of its closure +} diff --git a/src/test/ui/borrowck/regions-escape-bound-fn.stderr b/src/test/ui/borrowck/regions-escape-bound-fn.stderr new file mode 100644 index 00000000000..306da8b54e4 --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-bound-fn.stderr @@ -0,0 +1,12 @@ +error: borrowed data cannot be stored outside of its closure + --> $DIR/regions-escape-bound-fn.rs:18:27 + | +18 | with_int(|y| x = Some(y)); + | --- -----^- + | | | | + | | | cannot be stored outside of its closure + | | cannot infer an appropriate lifetime + | borrowed data cannot outlive this closure + +error: aborting due to previous error + diff --git a/src/test/ui/borrowck/regions-escape-unboxed-closure.rs b/src/test/ui/borrowck/regions-escape-unboxed-closure.rs new file mode 100644 index 00000000000..5a214504df4 --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-unboxed-closure.rs @@ -0,0 +1,18 @@ +// Copyright 2012 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. + +fn with_int(f: &mut FnMut(&isize)) { +} + +fn main() { + let mut x: Option<&isize> = None; + with_int(&mut |y| x = Some(y)); + //~^ ERROR borrowed data cannot be stored outside of its closure +} diff --git a/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr b/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr new file mode 100644 index 00000000000..5e51be46295 --- /dev/null +++ b/src/test/ui/borrowck/regions-escape-unboxed-closure.stderr @@ -0,0 +1,12 @@ +error: borrowed data cannot be stored outside of its closure + --> $DIR/regions-escape-unboxed-closure.rs:16:32 + | +16 | with_int(&mut |y| x = Some(y)); + | --- -----^- + | | | | + | | | cannot be stored outside of its closure + | | cannot infer an appropriate lifetime + | borrowed data cannot outlive this closure + +error: aborting due to previous error + diff --git a/src/test/ui/closure-expected-type/expect-region-supply-region.rs b/src/test/ui/closure-expected-type/expect-region-supply-region.rs new file mode 100644 index 00000000000..a464c5853e1 --- /dev/null +++ b/src/test/ui/closure-expected-type/expect-region-supply-region.rs @@ -0,0 +1,80 @@ +// Copyright 2016 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. + +#![allow(warnings)] + +fn closure_expecting_bound<F>(_: F) + where F: FnOnce(&u32) +{ +} + +fn closure_expecting_free<'a, F>(_: F) + where F: FnOnce(&'a u32) +{ +} + +fn expect_bound_supply_nothing() { + // Because `x` is inferred to have a bound region, we cannot allow + // it to escape into `f`: + let mut f: Option<&u32> = None; + closure_expecting_bound(|x| { + f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure + }); +} + +fn expect_bound_supply_bound() { + // Because `x` is inferred to have a bound region, we cannot allow + // it to escape into `f`, even with an explicit type annotation on + // closure: + let mut f: Option<&u32> = None; + closure_expecting_bound(|x: &u32| { + f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure + }); +} + +fn expect_bound_supply_named<'x>() { + let mut f: Option<&u32> = None; + + // Here we give a type annotation that `x` should be free. We get + // an error because of that. + closure_expecting_bound(|x: &'x u32| { + //~^ ERROR mismatched types + //~| ERROR mismatched types + + // And we still cannot let `x` escape into `f`. + f = Some(x); + //~^ ERROR borrowed data cannot be stored outside of its closure + }); +} + +fn expect_free_supply_nothing() { + let mut f: Option<&u32> = None; + closure_expecting_free(|x| f = Some(x)); // OK +} + +fn expect_free_supply_bound() { + let mut f: Option<&u32> = None; + + // Here, even though the annotation `&u32` could be seen as being + // bound in the closure, we permit it to be defined as a free + // region (which is inferred to something in the fn body). + closure_expecting_free(|x: &u32| f = Some(x)); // OK +} + +fn expect_free_supply_named<'x>() { + let mut f: Option<&u32> = None; + + // Here, even though the annotation `&u32` could be seen as being + // bound in the closure, we permit it to be defined as a free + // region (which is inferred to something in the fn body). + closure_expecting_free(|x: &'x u32| f = Some(x)); // OK +} + +fn main() { } diff --git a/src/test/ui/closure-expected-type/expect-region-supply-region.stderr b/src/test/ui/closure-expected-type/expect-region-supply-region.stderr new file mode 100644 index 00000000000..d34b17bb25b --- /dev/null +++ b/src/test/ui/closure-expected-type/expect-region-supply-region.stderr @@ -0,0 +1,100 @@ +error: borrowed data cannot be stored outside of its closure + --> $DIR/expect-region-supply-region.rs:28:18 + | +27 | closure_expecting_bound(|x| { + | --- borrowed data cannot outlive this closure +28 | f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure + | -----^- + | | | + | | cannot be stored outside of its closure + | cannot infer an appropriate lifetime + +error: borrowed data cannot be stored outside of its closure + --> $DIR/expect-region-supply-region.rs:38:18 + | +37 | closure_expecting_bound(|x: &u32| { + | --------- borrowed data cannot outlive this closure +38 | f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure + | -----^- + | | | + | | cannot be stored outside of its closure + | cannot infer an appropriate lifetime + +error[E0308]: mismatched types + --> $DIR/expect-region-supply-region.rs:47:33 + | +47 | closure_expecting_bound(|x: &'x u32| { + | ^^^^^^^ lifetime mismatch + | + = note: expected type `&u32` + found type `&'x u32` +note: the anonymous lifetime #2 defined on the body at 47:29... + --> $DIR/expect-region-supply-region.rs:47:29 + | +47 | closure_expecting_bound(|x: &'x u32| { + | _____________________________^ +48 | | //~^ ERROR mismatched types +49 | | //~| ERROR mismatched types +50 | | +... | +53 | | //~^ ERROR borrowed data cannot be stored outside of its closure +54 | | }); + | |_____^ +note: ...does not necessarily outlive the lifetime 'x as defined on the function body at 42:1 + --> $DIR/expect-region-supply-region.rs:42:1 + | +42 | / fn expect_bound_supply_named<'x>() { +43 | | let mut f: Option<&u32> = None; +44 | | +45 | | // Here we give a type annotation that `x` should be free. We get +... | +54 | | }); +55 | | } + | |_^ + +error[E0308]: mismatched types + --> $DIR/expect-region-supply-region.rs:47:33 + | +47 | closure_expecting_bound(|x: &'x u32| { + | ^^^^^^^ lifetime mismatch + | + = note: expected type `&u32` + found type `&'x u32` +note: the lifetime 'x as defined on the function body at 42:1... + --> $DIR/expect-region-supply-region.rs:42:1 + | +42 | / fn expect_bound_supply_named<'x>() { +43 | | let mut f: Option<&u32> = None; +44 | | +45 | | // Here we give a type annotation that `x` should be free. We get +... | +54 | | }); +55 | | } + | |_^ +note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 47:29 + --> $DIR/expect-region-supply-region.rs:47:29 + | +47 | closure_expecting_bound(|x: &'x u32| { + | _____________________________^ +48 | | //~^ ERROR mismatched types +49 | | //~| ERROR mismatched types +50 | | +... | +53 | | //~^ ERROR borrowed data cannot be stored outside of its closure +54 | | }); + | |_____^ + +error: borrowed data cannot be stored outside of its closure + --> $DIR/expect-region-supply-region.rs:52:18 + | +47 | closure_expecting_bound(|x: &'x u32| { + | ------------ borrowed data cannot outlive this closure +... +52 | f = Some(x); + | -----^- + | | | + | | cannot be stored outside of its closure + | cannot infer an appropriate lifetime + +error: aborting due to 5 previous errors + |
