]> git.lizzy.rs Git - rust.git/blob - src/test/ui/closures/closure-expected-type/expect-region-supply-region.rs
28a6ab77a915ee489481ad2461b01b989e2e4f19
[rust.git] / src / test / ui / closures / closure-expected-type / expect-region-supply-region.rs
1 #![allow(warnings)]
2
3 fn closure_expecting_bound<F>(_: F)
4     where F: FnOnce(&u32)
5 {
6 }
7
8 fn closure_expecting_free<'a, F>(_: F)
9     where F: FnOnce(&'a u32)
10 {
11 }
12
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
19     });
20 }
21
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
25     // closure:
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
29     });
30 }
31
32 fn expect_bound_supply_named<'x>() {
33     let mut f: Option<&u32> = None;
34
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
40
41         // And we still cannot let `x` escape into `f`.
42         f = Some(x);
43         //~^ ERROR borrowed data cannot be stored outside of its closure
44     });
45 }
46
47 fn expect_free_supply_nothing() {
48     let mut f: Option<&u32> = None;
49     closure_expecting_free(|x| f = Some(x)); // OK
50 }
51
52 fn expect_free_supply_bound() {
53     let mut f: Option<&u32> = None;
54
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
59 }
60
61 fn expect_free_supply_named<'x>() {
62     let mut f: Option<&u32> = None;
63
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
68 }
69
70 fn main() { }