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