1 #![allow(dead_code)]
2 
non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u83 fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
4     a
5 }
6 
7 // The incorrect case without `for<'a>` is tested for in `rfc1623-2.rs`
8 static NON_ELIDABLE_FN: &for<'a> fn(&'a u8, &'a u8) -> &'a u8 =
9     &(non_elidable as for<'a> fn(&'a u8, &'a u8) -> &'a u8);
10 
11 struct SomeStruct<'x, 'y, 'z: 'x> {
12     foo: &'x Foo<'z>,
13     bar: &'x Bar<'z>,
14     f: &'y dyn for<'a, 'b> Fn(&'a Foo<'b>) -> &'a Foo<'b>,
15 }
16 
id<T>(t: T) -> T17 fn id<T>(t: T) -> T {
18     t
19 }
20 
21 static SOME_STRUCT: &SomeStruct = &SomeStruct {
22     foo: &Foo { bools: &[false, true] },
23     bar: &Bar { bools: &[true, true] },
24     f: &id,
25     //~^ ERROR implementation of `FnOnce` is not general enough
26 };
27 
28 // very simple test for a 'static static with default lifetime
29 static STATIC_STR: &'static str = "&'static str";
30 const CONST_STR: &'static str = "&'static str";
31 
32 // this should be the same as without default:
33 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
34 const EXPLICIT_CONST_STR: &'static str = "&'static str";
35 
36 // a function that elides to an unbound lifetime for both in- and output
id_u8_slice(arg: &[u8]) -> &[u8]37 fn id_u8_slice(arg: &[u8]) -> &[u8] {
38     arg
39 }
40 
41 // one with a function, argument elided
42 static STATIC_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
43 const CONST_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
44 
45 // this should be the same as without elision
46 static STATIC_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
47     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
48 const CONST_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
49     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
50 
51 // another function that elides, each to a different unbound lifetime
multi_args(a: &u8, b: &u8, c: &u8)52 fn multi_args(a: &u8, b: &u8, c: &u8) {}
53 
54 static STATIC_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
55 const CONST_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
56 
57 struct Foo<'a> {
58     bools: &'a [bool],
59 }
60 
61 static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
62 const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
63 
64 type Bar<'a> = Foo<'a>;
65 
66 static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
67 const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
68 
69 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
70 
baz(e: &[u8]) -> Option<u8>71 fn baz(e: &[u8]) -> Option<u8> {
72     e.first().map(|x| *x)
73 }
74 
75 static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
76 const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
77 
78 static BYTES: &'static [u8] = &[1, 2, 3];
79 
main()80 fn main() {
81     let x = &[1u8, 2, 3];
82     let y = x;
83 
84     // this works, so lifetime < `'static` is valid
85     assert_eq!(Some(1), STATIC_BAZ(y));
86     assert_eq!(Some(1), CONST_BAZ(y));
87 
88     let y = &[1u8, 2, 3];
89 
90     STATIC_BAZ(BYTES); // BYTES has static lifetime
91     CONST_BAZ(y); // interestingly this does not get reported
92 }
93