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.
12 fn non_elidable
<'a
, 'b
>(a
: &'a
u8, b
: &'b
u8) -> &'a
u8 {
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]
22 struct SomeStruct
<'x
, 'y
, 'z
: 'x
> {
25 f
: &'y
for<'a
, 'b
: 'a
> Fn(&'a Foo
<'b
>) -> &'a Bar
<'b
>,
32 static SOME_STRUCT
: &SomeStruct
= SomeStruct
{
33 foo
: &Foo { bools: &[false, true] }
,
34 bar
: &Bar { bools: &[true, true] }
,
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";
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";
46 // a function that elides to an unbound lifetime for both in- and output
47 fn id_u8_slice(arg
: &[u8]) -> &[u8] {
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]);
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]);
61 // another function that elides, each to a different unbound lifetime
62 fn multi_args(a
: &u8, b
: &u8, c
: &u8) {}
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));
71 static STATIC_FOO
: Foo
<'
static> = Foo { bools: &[true, false] }
;
72 const CONST_FOO
: Foo
<'
static> = Foo { bools: &[true, false] }
;
74 type Bar
<'a
> = Foo
<'a
>;
76 static STATIC_BAR
: Bar
<'
static> = Bar { bools: &[true, false] }
;
77 const CONST_BAR
: Bar
<'
static> = Bar { bools: &[true, false] }
;
79 type Baz
<'a
> = fn(&'a
[u8]) -> Option
<u8>;
81 fn baz(e
: &[u8]) -> Option
<u8> {
85 static STATIC_BAZ
: &'
static Baz
<'
static> = &(baz
as Baz
);
86 const CONST_BAZ
: &'
static Baz
<'
static> = &(baz
as Baz
);
88 static BYTES
: &'
static [u8] = &[1, 2, 3];
94 // this works, so lifetime < `'static` is valid
95 assert_eq
!(Some(1), STATIC_BAZ(y
));
96 assert_eq
!(Some(1), CONST_BAZ(y
));
100 STATIC_BAZ(BYTES
); // BYTES has static lifetime
101 CONST_BAZ(y
); // interestingly this does not get reported