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