2 // ignore-compare-mode-nll
3 //[nll] compile-flags: -Z borrowck=mir
7 fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
11 // The incorrect case without `for<'a>` is tested for in `rfc1623-2.rs`
12 static NON_ELIDABLE_FN: &for<'a> fn(&'a u8, &'a u8) -> &'a u8 =
13 &(non_elidable as for<'a> fn(&'a u8, &'a u8) -> &'a u8);
15 struct SomeStruct<'x, 'y, 'z: 'x> {
18 f: &'y dyn for<'a, 'b> Fn(&'a Foo<'b>) -> &'a Foo<'b>,
21 // Without this, the wf-check will fail early so we'll never see the
22 // error in SOME_STRUCT's body.
23 unsafe impl<'x, 'y, 'z: 'x> Sync for SomeStruct<'x, 'y, 'z> {}
29 static SOME_STRUCT: &SomeStruct = &SomeStruct {
30 foo: &Foo { bools: &[false, true] },
31 bar: &Bar { bools: &[true, true] },
33 //[base]~^ ERROR implementation of `FnOnce` is not general enough
34 //[nll]~^^ ERROR mismatched types
35 //[nll]~| ERROR mismatched types
36 //[nll]~| ERROR implementation of `FnOnce` is not general enough
37 //[nll]~| ERROR implementation of `FnOnce` is not general enough
40 // very simple test for a 'static static with default lifetime
41 static STATIC_STR: &'static str = "&'static str";
42 const CONST_STR: &'static str = "&'static str";
44 // this should be the same as without default:
45 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
46 const EXPLICIT_CONST_STR: &'static str = "&'static str";
48 // a function that elides to an unbound lifetime for both in- and output
49 fn id_u8_slice(arg: &[u8]) -> &[u8] {
53 // one with a function, argument elided
54 static STATIC_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
55 const CONST_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
57 // this should be the same as without elision
58 static STATIC_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
59 &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
60 const CONST_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
61 &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
63 // another function that elides, each to a different unbound lifetime
64 fn multi_args(a: &u8, b: &u8, c: &u8) {}
66 static STATIC_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
67 const CONST_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
73 static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
74 const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
76 type Bar<'a> = Foo<'a>;
78 static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
79 const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
81 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
83 fn baz(e: &[u8]) -> Option<u8> {
87 static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
88 const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
90 static BYTES: &'static [u8] = &[1, 2, 3];
96 // this works, so lifetime < `'static` is valid
97 assert_eq!(Some(1), STATIC_BAZ(y));
98 assert_eq!(Some(1), CONST_BAZ(y));
100 let y = &[1u8, 2, 3];
102 STATIC_BAZ(BYTES); // BYTES has static lifetime
103 CONST_BAZ(y); // interestingly this does not get reported