]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/pass/rfc1623.rs
Auto merge of #104915 - weihanglo:update-cargo, r=ehuss
[rust.git] / src / tools / miri / tests / pass / rfc1623.rs
1 #![allow(dead_code)] // tons of unused statics here...
2
3 // very simple test for a 'static static with default lifetime
4 static STATIC_STR: &str = "&'static str";
5 const CONST_STR: &str = "&'static str";
6
7 // this should be the same as without default:
8 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
9 const EXPLICIT_CONST_STR: &'static str = "&'static str";
10
11 // a function that elides to an unbound lifetime for both in- and output
12 fn id_u8_slice(arg: &[u8]) -> &[u8] {
13     arg
14 }
15
16 // one with a function, argument elided
17 static STATIC_SIMPLE_FN: &fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
18 const CONST_SIMPLE_FN: &fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
19
20 // this should be the same as without elision
21 static STATIC_NON_ELIDED_FN: &for<'a> fn(&'a [u8]) -> &'a [u8] =
22     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
23 const CONST_NON_ELIDED_FN: &for<'a> fn(&'a [u8]) -> &'a [u8] =
24     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
25
26 // another function that elides, each to a different unbound lifetime
27 fn multi_args(_a: &u8, _b: &u8, _c: &u8) {}
28
29 static STATIC_MULTI_FN: &fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
30 const CONST_MULTI_FN: &fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
31
32 struct Foo<'a> {
33     bools: &'a [bool],
34 }
35
36 static STATIC_FOO: Foo = Foo { bools: &[true, false] };
37 const CONST_FOO: Foo = Foo { bools: &[true, false] };
38
39 type Bar<'a> = Foo<'a>;
40
41 static STATIC_BAR: Bar = Bar { bools: &[true, false] };
42 const CONST_BAR: Bar = Bar { bools: &[true, false] };
43
44 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
45
46 fn baz(e: &[u8]) -> Option<u8> {
47     e.first().map(|x| *x)
48 }
49
50 static STATIC_BAZ: &Baz = &(baz as Baz);
51 const CONST_BAZ: &Baz = &(baz as Baz);
52
53 static BYTES: &[u8] = &[1, 2, 3];
54
55 fn main() {
56     // make sure that the lifetime is actually elided (and not defaulted)
57     let x = &[1u8, 2, 3];
58     STATIC_SIMPLE_FN(x);
59     CONST_SIMPLE_FN(x);
60
61     STATIC_BAZ(BYTES); // neees static lifetime
62     CONST_BAZ(BYTES);
63
64     // make sure this works with different lifetimes
65     let a = &1;
66     {
67         let b = &2;
68         let c = &3;
69         CONST_MULTI_FN(a, b, c);
70     }
71 }