@@ -17,18 +17,82 @@ static NON_ELIDABLE_FN : &fn(&u8, &u8) -> &u8 =
17
17
//~^ ERROR: missing lifetime specifier
18
18
& ( non_elidable as fn ( & u8 , & u8 ) -> & u8 ) ;
19
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
+
20
77
type Baz < ' a > = fn ( & ' a [ u8 ] ) -> Option < u8 > ;
21
78
22
79
fn baz ( e : & [ u8 ] ) -> Option < u8 > { e. first ( ) . map ( |x| * x) }
23
80
24
- static STATIC_BAZ : & Baz < ' static > = & ( baz as Baz ) ;
25
- const CONST_BAZ : & Baz < ' static > = & ( baz as Baz ) ;
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 ] ;
26
85
27
86
fn main ( ) {
28
87
let x = & [ 1u8 , 2 , 3 ] ;
29
88
let y = x;
30
89
31
- //surprisingly this appears to work , so lifetime < `'static` is valid
90
+ //this works , so lifetime < `'static` is valid
32
91
assert_eq ! ( Some ( 1 ) , STATIC_BAZ ( y) ) ;
33
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
34
98
}
0 commit comments