]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc1623.rs
Rollup merge of #97317 - GuillaumeGomez:gui-settings-text-click, r=jsha
[rust.git] / src / test / ui / rfc1623.rs
1 // revisions: base nll
2 // ignore-compare-mode-nll
3 //[nll] compile-flags: -Z borrowck=mir
4
5 #![allow(dead_code)]
6
7 fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
8     a
9 }
10
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);
14
15 struct SomeStruct<'x, 'y, 'z: 'x> {
16     foo: &'x Foo<'z>,
17     bar: &'x Bar<'z>,
18     f: &'y dyn for<'a, 'b> Fn(&'a Foo<'b>) -> &'a Foo<'b>,
19 }
20
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> {}
24
25 fn id<T>(t: T) -> T {
26     t
27 }
28
29 static SOME_STRUCT: &SomeStruct = &SomeStruct {
30     foo: &Foo { bools: &[false, true] },
31     bar: &Bar { bools: &[true, true] },
32     f: &id,
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
38 };
39
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";
43
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";
47
48 // a function that elides to an unbound lifetime for both in- and output
49 fn id_u8_slice(arg: &[u8]) -> &[u8] {
50     arg
51 }
52
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]);
56
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]);
62
63 // another function that elides, each to a different unbound lifetime
64 fn multi_args(a: &u8, b: &u8, c: &u8) {}
65
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));
68
69 struct Foo<'a> {
70     bools: &'a [bool],
71 }
72
73 static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
74 const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
75
76 type Bar<'a> = Foo<'a>;
77
78 static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
79 const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
80
81 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
82
83 fn baz(e: &[u8]) -> Option<u8> {
84     e.first().map(|x| *x)
85 }
86
87 static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
88 const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
89
90 static BYTES: &'static [u8] = &[1, 2, 3];
91
92 fn main() {
93     let x = &[1u8, 2, 3];
94     let y = x;
95
96     // this works, so lifetime < `'static` is valid
97     assert_eq!(Some(1), STATIC_BAZ(y));
98     assert_eq!(Some(1), CONST_BAZ(y));
99
100     let y = &[1u8, 2, 3];
101
102     STATIC_BAZ(BYTES); // BYTES has static lifetime
103     CONST_BAZ(y); // interestingly this does not get reported
104 }