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