]> git.lizzy.rs Git - rust.git/blob - tests/ui/regions/issue-56537-closure-uses-region-from-container.rs
internally change regions to be covariant
[rust.git] / tests / ui / regions / issue-56537-closure-uses-region-from-container.rs
1 // This is a collection of examples where a function's formal
2 // parameter has an explicit lifetime and a closure within that
3 // function returns that formal parameter. The closure's return type,
4 // to be correctly inferred, needs to include the lifetime introduced
5 // by the function.
6 //
7 // This works today, which precludes changing things so that closures
8 // follow the same lifetime-elision rules used elsewhere. See
9 // rust-lang/rust#56537
10
11 // check-pass
12
13 fn willy_no_annot<'w>(p: &'w str, q: &str) -> &'w str {
14     let free_dumb = |_x| { p }; // no type annotation at all
15     let hello = format!("Hello");
16     free_dumb(&hello)
17 }
18
19 fn willy_ret_type_annot<'w>(p: &'w str, q: &str) -> &'w str {
20     let free_dumb = |_x| -> &str { p }; // type annotation on the return type
21     let hello = format!("Hello");
22     free_dumb(&hello)
23 }
24
25 fn willy_ret_region_annot<'w>(p: &'w str, q: &str) -> &'w str {
26     let free_dumb = |_x| -> &'w str { p }; // type+region annotation on return type
27     let hello = format!("Hello");
28     free_dumb(&hello)
29 }
30
31 fn willy_arg_type_ret_type_annot<'w>(p: &'w str, q: &str) -> &'w str {
32     let free_dumb = |_x: &str| -> &str { p }; // type annotation on arg and return types
33     let hello = format!("Hello");
34     free_dumb(&hello)
35 }
36
37 fn willy_arg_type_ret_region_annot<'w>(p: &'w str, q: &str) -> &'w str {
38     let free_dumb = |_x: &str| -> &'w str { p }; // fully annotated
39     let hello = format!("Hello");
40     free_dumb(&hello)
41 }
42
43 fn main() {
44     let world = format!("World");
45     let w1: &str = {
46         let hello = format!("He11o");
47         willy_no_annot(&world, &hello)
48     };
49     let w2: &str = {
50         let hello = format!("He22o");
51         willy_ret_type_annot(&world, &hello)
52     };
53     let w3: &str = {
54         let hello = format!("He33o");
55         willy_ret_region_annot(&world, &hello)
56     };
57     let w4: &str = {
58         let hello = format!("He44o");
59         willy_arg_type_ret_type_annot(&world, &hello)
60     };
61     let w5: &str = {
62         let hello = format!("He55o");
63         willy_arg_type_ret_region_annot(&world, &hello)
64     };
65     assert_eq!((w1, w2, w3, w4, w5),
66                ("World","World","World","World","World"));
67 }