3 fn closure_expecting_bound<F>(_: F)
8 fn closure_expecting_free<'a, F>(_: F)
9 where F: FnOnce(&'a u32)
13 fn expect_bound_supply_nothing() {
14 // Because `x` is inferred to have a bound region, we cannot allow
15 // it to escape into `f`:
16 let mut f: Option<&u32> = None;
17 closure_expecting_bound(|x| {
18 f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure
22 fn expect_bound_supply_bound() {
23 // Because `x` is inferred to have a bound region, we cannot allow
24 // it to escape into `f`, even with an explicit type annotation on
26 let mut f: Option<&u32> = None;
27 closure_expecting_bound(|x: &u32| {
28 f = Some(x); //~ ERROR borrowed data cannot be stored outside of its closure
32 fn expect_bound_supply_named<'x>() {
33 let mut f: Option<&u32> = None;
35 // Here we give a type annotation that `x` should be free. We get
36 // an error because of that.
37 closure_expecting_bound(|x: &'x u32| {
38 //~^ ERROR mismatched types
39 //~| ERROR mismatched types
41 // And we still cannot let `x` escape into `f`.
43 //~^ ERROR borrowed data cannot be stored outside of its closure
47 fn expect_free_supply_nothing() {
48 let mut f: Option<&u32> = None;
49 closure_expecting_free(|x| f = Some(x)); // OK
52 fn expect_free_supply_bound() {
53 let mut f: Option<&u32> = None;
55 // Here, even though the annotation `&u32` could be seen as being
56 // bound in the closure, we permit it to be defined as a free
57 // region (which is inferred to something in the fn body).
58 closure_expecting_free(|x: &u32| f = Some(x)); // OK
61 fn expect_free_supply_named<'x>() {
62 let mut f: Option<&u32> = None;
64 // Here, even though the annotation `&u32` could be seen as being
65 // bound in the closure, we permit it to be defined as a free
66 // region (which is inferred to something in the fn body).
67 closure_expecting_free(|x: &'x u32| f = Some(x)); // OK