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.
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)]
13 fn non_elidable
<'a
, 'b
>(a
: &'a
u8, b
: &'b
u8) -> &'a
u8 {
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]
23 struct SomeStruct
<'x
, 'y
, 'z
: 'x
> {
26 f
: &'y
for<'a
, 'b
: 'a
> Fn(&'a Foo
<'b
>) -> &'a Bar
<'b
>,
33 static SOME_STRUCT
: &SomeStruct
= SomeStruct
{
34 foo
: &Foo { bools: &[false, true] }
,
35 bar
: &Bar { bools: &[true, true] }
,
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";
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";
47 // a function that elides to an unbound lifetime for both in- and output
48 fn id_u8_slice(arg
: &[u8]) -> &[u8] {
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]);
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]);
62 // another function that elides, each to a different unbound lifetime
63 fn multi_args(a
: &u8, b
: &u8, c
: &u8) {}
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));
72 static STATIC_FOO
: Foo
<'
static> = Foo { bools: &[true, false] }
;
73 const CONST_FOO
: Foo
<'
static> = Foo { bools: &[true, false] }
;
75 type Bar
<'a
> = Foo
<'a
>;
77 static STATIC_BAR
: Bar
<'
static> = Bar { bools: &[true, false] }
;
78 const CONST_BAR
: Bar
<'
static> = Bar { bools: &[true, false] }
;
80 type Baz
<'a
> = fn(&'a
[u8]) -> Option
<u8>;
82 fn baz(e
: &[u8]) -> Option
<u8> {
86 static STATIC_BAZ
: &'
static Baz
<'
static> = &(baz
as Baz
);
87 const CONST_BAZ
: &'
static Baz
<'
static> = &(baz
as Baz
);
89 static BYTES
: &'
static [u8] = &[1, 2, 3];
95 // this works, so lifetime < `'static` is valid
96 assert_eq
!(Some(1), STATIC_BAZ(y
));
97 assert_eq
!(Some(1), CONST_BAZ(y
));
101 STATIC_BAZ(BYTES
); // BYTES has static lifetime
102 CONST_BAZ(y
); // interestingly this does not get reported