]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Verifies all possible restrictions for statics values. |
2 | ||
3b2f2976 | 3 | #![allow(warnings)] |
1a4d82fc JJ |
4 | #![feature(box_syntax)] |
5 | ||
6 | use std::marker; | |
7 | ||
8 | struct WithDtor; | |
9 | ||
10 | impl Drop for WithDtor { | |
11 | fn drop(&mut self) {} | |
12 | } | |
13 | ||
14 | // This enum will be used to test the following rules: | |
15 | // 1. Variants are safe for static | |
16 | // 2. Expr calls are allowed as long as they arguments are safe | |
17 | // 3. Expr calls with unsafe arguments for statics are rejected | |
18 | enum SafeEnum { | |
19 | Variant1, | |
20 | Variant2(isize), | |
21 | Variant3(WithDtor), | |
22 | Variant4(String) | |
23 | } | |
24 | ||
25 | // These should be ok | |
26 | static STATIC1: SafeEnum = SafeEnum::Variant1; | |
27 | static STATIC2: SafeEnum = SafeEnum::Variant2(0); | |
1a4d82fc | 28 | static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor); |
1a4d82fc | 29 | |
1a4d82fc JJ |
30 | enum UnsafeEnum { |
31 | Variant5, | |
32 | Variant6(isize) | |
33 | } | |
34 | ||
35 | impl Drop for UnsafeEnum { | |
36 | fn drop(&mut self) {} | |
37 | } | |
38 | ||
39 | ||
40 | static STATIC4: UnsafeEnum = UnsafeEnum::Variant5; | |
1a4d82fc | 41 | static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0); |
1a4d82fc JJ |
42 | |
43 | ||
44 | struct SafeStruct { | |
45 | field1: SafeEnum, | |
46 | field2: SafeEnum, | |
47 | } | |
48 | ||
49 | ||
50 | // Struct fields are safe, hence this static should be safe | |
51 | static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)}; | |
52 | ||
1a4d82fc JJ |
53 | static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, |
54 | field2: SafeEnum::Variant3(WithDtor)}; | |
1a4d82fc JJ |
55 | |
56 | // Test variadic constructor for structs. The base struct should be examined | |
57 | // as well as every field present in the constructor. | |
58 | // This example shouldn't fail because all the fields are safe. | |
59 | static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, | |
60 | ..SafeStruct{field1: SafeEnum::Variant1, | |
61 | field2: SafeEnum::Variant1}}; | |
62 | ||
63 | // This example should fail because field1 in the base struct is not safe | |
64 | static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, | |
65 | ..SafeStruct{field1: SafeEnum::Variant3(WithDtor), | |
ea8adc8c | 66 | //~^ ERROR destructors cannot be evaluated at compile-time |
1a4d82fc | 67 | field2: SafeEnum::Variant1}}; |
1a4d82fc JJ |
68 | |
69 | struct UnsafeStruct; | |
70 | ||
71 | impl Drop for UnsafeStruct { | |
72 | fn drop(&mut self) {} | |
73 | } | |
74 | ||
1a4d82fc | 75 | static STATIC10: UnsafeStruct = UnsafeStruct; |
1a4d82fc JJ |
76 | |
77 | struct MyOwned; | |
78 | ||
79 | static STATIC11: Box<MyOwned> = box MyOwned; | |
85aaf69f | 80 | //~^ ERROR allocations are not allowed in statics |
1a4d82fc | 81 | |
1a4d82fc | 82 | static mut STATIC12: UnsafeStruct = UnsafeStruct; |
1a4d82fc JJ |
83 | |
84 | static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, | |
1a4d82fc | 85 | field2: SafeEnum::Variant3(WithDtor)}; |
1a4d82fc JJ |
86 | |
87 | static mut STATIC14: SafeStruct = SafeStruct { | |
1a4d82fc JJ |
88 | field1: SafeEnum::Variant1, |
89 | field2: SafeEnum::Variant4("str".to_string()) | |
a7813a04 | 90 | //~^ ERROR calls in statics are limited to constant functions |
1a4d82fc JJ |
91 | }; |
92 | ||
93 | static STATIC15: &'static [Box<MyOwned>] = &[ | |
85aaf69f SL |
94 | box MyOwned, //~ ERROR allocations are not allowed in statics |
95 | box MyOwned, //~ ERROR allocations are not allowed in statics | |
1a4d82fc JJ |
96 | ]; |
97 | ||
98 | static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = ( | |
85aaf69f SL |
99 | &box MyOwned, //~ ERROR allocations are not allowed in statics |
100 | &box MyOwned, //~ ERROR allocations are not allowed in statics | |
1a4d82fc JJ |
101 | ); |
102 | ||
103 | static mut STATIC17: SafeEnum = SafeEnum::Variant1; | |
1a4d82fc JJ |
104 | |
105 | static STATIC19: Box<isize> = | |
106 | box 3; | |
85aaf69f | 107 | //~^ ERROR allocations are not allowed in statics |
1a4d82fc JJ |
108 | |
109 | pub fn main() { | |
110 | let y = { static x: Box<isize> = box 3; x }; | |
85aaf69f | 111 | //~^ ERROR allocations are not allowed in statics |
0731742a | 112 | //~| ERROR cannot move out of static item |
1a4d82fc | 113 | } |