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
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
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");
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");
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");
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");
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");
44 let world = format!("World");
46 let hello = format!("He11o");
47 willy_no_annot(&world, &hello)
50 let hello = format!("He22o");
51 willy_ret_type_annot(&world, &hello)
54 let hello = format!("He33o");
55 willy_ret_region_annot(&world, &hello)
58 let hello = format!("He44o");
59 willy_arg_type_ret_type_annot(&world, &hello)
62 let hello = format!("He55o");
63 willy_arg_type_ret_region_annot(&world, &hello)
65 assert_eq!((w1, w2, w3, w4, w5),
66 ("World","World","World","World","World"));