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