]> git.lizzy.rs Git - rust.git/blob - tests/ui/rfcs/rfc1623-2.rs
Auto merge of #103659 - clubby789:improve-partialord-derive, r=nagisa
[rust.git] / tests / ui / rfcs / rfc1623-2.rs
1 #![allow(dead_code)]
2
3 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
17 // Without this, the wf-check will fail early so we'll never see the
18 // error in SOME_STRUCT's body.
19 unsafe impl<'x, 'y, 'z: 'x> Sync for SomeStruct<'x, 'y, 'z> {}
20
21 fn id<T>(t: T) -> T {
22     t
23 }
24
25 static SOME_STRUCT: &SomeStruct = &SomeStruct {
26     foo: &Foo { bools: &[false, true] },
27     bar: &Bar { bools: &[true, true] },
28     f: &id,
29     //~^ ERROR mismatched types
30     //~| ERROR mismatched types
31     //~| ERROR implementation of `FnOnce` is not general enough
32     //~| ERROR implementation of `FnOnce` is not general enough
33 };
34
35 // very simple test for a 'static static with default lifetime
36 static STATIC_STR: &'static str = "&'static str";
37 const CONST_STR: &'static str = "&'static str";
38
39 // this should be the same as without default:
40 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
41 const EXPLICIT_CONST_STR: &'static str = "&'static str";
42
43 // a function that elides to an unbound lifetime for both in- and output
44 fn id_u8_slice(arg: &[u8]) -> &[u8] {
45     arg
46 }
47
48 // one with a function, argument elided
49 static STATIC_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
50 const CONST_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
51
52 // this should be the same as without elision
53 static STATIC_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
54     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
55 const CONST_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
56     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
57
58 // another function that elides, each to a different unbound lifetime
59 fn multi_args(a: &u8, b: &u8, c: &u8) {}
60
61 static STATIC_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
62 const CONST_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
63
64 struct Foo<'a> {
65     bools: &'a [bool],
66 }
67
68 static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
69 const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
70
71 type Bar<'a> = Foo<'a>;
72
73 static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
74 const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
75
76 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
77
78 fn baz(e: &[u8]) -> Option<u8> {
79     e.first().map(|x| *x)
80 }
81
82 static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
83 const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
84
85 static BYTES: &'static [u8] = &[1, 2, 3];
86
87 fn main() {
88     let x = &[1u8, 2, 3];
89     let y = x;
90
91     // this works, so lifetime < `'static` is valid
92     assert_eq!(Some(1), STATIC_BAZ(y));
93     assert_eq!(Some(1), CONST_BAZ(y));
94
95     let y = &[1u8, 2, 3];
96
97     STATIC_BAZ(BYTES); // BYTES has static lifetime
98     CONST_BAZ(y); // interestingly this does not get reported
99 }