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