]> git.lizzy.rs Git - rust.git/blob - src/test/ui/rfc1623.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / test / ui / rfc1623.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 #![allow(dead_code)]
11
12 fn non_elidable<'a, 'b>(a: &'a u8, b: &'b u8) -> &'a u8 {
13     a
14 }
15
16 // the boundaries of elision
17 static NON_ELIDABLE_FN: &fn(&u8, &u8) -> &u8 =
18 //~^ ERROR missing lifetime specifier [E0106]
19     &(non_elidable as fn(&u8, &u8) -> &u8);
20     //~^ ERROR missing lifetime specifier [E0106]
21
22 struct SomeStruct<'x, 'y, 'z: 'x> {
23     foo: &'x Foo<'z>,
24     bar: &'x Bar<'z>,
25     f: &'y for<'a, 'b> Fn(&'a Foo<'b>) -> &'a Bar<'b>,
26 }
27
28 fn id<T>(t: T) -> T {
29     t
30 }
31
32 static SOME_STRUCT: &SomeStruct = SomeStruct {
33     foo: &Foo { bools: &[false, true] },
34     bar: &Bar { bools: &[true, true] },
35     f: &id,
36 };
37
38 // very simple test for a 'static static with default lifetime
39 static STATIC_STR: &'static str = "&'static str";
40 const CONST_STR: &'static str = "&'static str";
41
42 // this should be the same as without default:
43 static EXPLICIT_STATIC_STR: &'static str = "&'static str";
44 const EXPLICIT_CONST_STR: &'static str = "&'static str";
45
46 // a function that elides to an unbound lifetime for both in- and output
47 fn id_u8_slice(arg: &[u8]) -> &[u8] {
48     arg
49 }
50
51 // one with a function, argument elided
52 static STATIC_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
53 const CONST_SIMPLE_FN: &'static fn(&[u8]) -> &[u8] = &(id_u8_slice as fn(&[u8]) -> &[u8]);
54
55 // this should be the same as without elision
56 static STATIC_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
57     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
58 const CONST_NON_ELIDED_fN: &'static for<'a> fn(&'a [u8]) -> &'a [u8] =
59     &(id_u8_slice as for<'a> fn(&'a [u8]) -> &'a [u8]);
60
61 // another function that elides, each to a different unbound lifetime
62 fn multi_args(a: &u8, b: &u8, c: &u8) {}
63
64 static STATIC_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
65 const CONST_MULTI_FN: &'static fn(&u8, &u8, &u8) = &(multi_args as fn(&u8, &u8, &u8));
66
67 struct Foo<'a> {
68     bools: &'a [bool],
69 }
70
71 static STATIC_FOO: Foo<'static> = Foo { bools: &[true, false] };
72 const CONST_FOO: Foo<'static> = Foo { bools: &[true, false] };
73
74 type Bar<'a> = Foo<'a>;
75
76 static STATIC_BAR: Bar<'static> = Bar { bools: &[true, false] };
77 const CONST_BAR: Bar<'static> = Bar { bools: &[true, false] };
78
79 type Baz<'a> = fn(&'a [u8]) -> Option<u8>;
80
81 fn baz(e: &[u8]) -> Option<u8> {
82     e.first().map(|x| *x)
83 }
84
85 static STATIC_BAZ: &'static Baz<'static> = &(baz as Baz);
86 const CONST_BAZ: &'static Baz<'static> = &(baz as Baz);
87
88 static BYTES: &'static [u8] = &[1, 2, 3];
89
90 fn main() {
91     let x = &[1u8, 2, 3];
92     let y = x;
93
94     // this works, so lifetime < `'static` is valid
95     assert_eq!(Some(1), STATIC_BAZ(y));
96     assert_eq!(Some(1), CONST_BAZ(y));
97
98     let y = &[1u8, 2, 3];
99
100     STATIC_BAZ(BYTES); // BYTES has static lifetime
101     CONST_BAZ(y); // interestingly this does not get reported
102 }