]> git.lizzy.rs Git - rust.git/blob - src/test/ui/closures/closure-expected-type/expect-region-supply-region.rs
Update tests
[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
5     F: FnOnce(&u32),
6 {
7 }
8
9 fn closure_expecting_free<'a, F>(_: F)
10 where
11     F: FnOnce(&'a u32),
12 {
13 }
14
15 fn expect_bound_supply_nothing() {
16     // Because `x` is inferred to have a bound region, we cannot allow
17     // it to escape into `f`:
18     let mut f: Option<&u32> = None;
19     closure_expecting_bound(|x| {
20         f = Some(x); //~ ERROR borrowed data escapes outside of closure
21     });
22 }
23
24 fn expect_bound_supply_bound() {
25     // Because `x` is inferred to have a bound region, we cannot allow
26     // it to escape into `f`, even with an explicit type annotation on
27     // closure:
28     let mut f: Option<&u32> = None;
29     closure_expecting_bound(|x: &u32| {
30         f = Some(x); //~ ERROR borrowed data escapes outside of closure
31     });
32 }
33
34 fn expect_free_supply_nothing() {
35     let mut f: Option<&u32> = None;
36     closure_expecting_free(|x| f = Some(x)); // OK
37 }
38
39 fn expect_free_supply_bound() {
40     let mut f: Option<&u32> = None;
41
42     // Here, even though the annotation `&u32` could be seen as being
43     // bound in the closure, we permit it to be defined as a free
44     // region (which is inferred to something in the fn body).
45     closure_expecting_free(|x: &u32| f = Some(x)); // OK
46 }
47
48 fn expect_free_supply_named<'x>() {
49     let mut f: Option<&u32> = None;
50
51     // Here, even though the annotation `&u32` could be seen as being
52     // bound in the closure, we permit it to be defined as a free
53     // region (which is inferred to something in the fn body).
54     closure_expecting_free(|x: &'x u32| f = Some(x)); // OK
55 }
56
57 fn main() {}