1 // Verifies all possible restrictions for statics values.
4 #![feature(box_syntax)]
10 impl Drop for WithDtor {
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
26 static STATIC1: SafeEnum = SafeEnum::Variant1;
27 static STATIC2: SafeEnum = SafeEnum::Variant2(0);
28 static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
35 impl Drop for UnsafeEnum {
40 static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
41 static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
50 // Struct fields are safe, hence this static should be safe
51 static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
53 static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
54 field2: SafeEnum::Variant3(WithDtor)};
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}};
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),
66 //~^ ERROR destructor of
67 field2: SafeEnum::Variant1}};
71 impl Drop for UnsafeStruct {
75 static STATIC10: UnsafeStruct = UnsafeStruct;
79 static STATIC11: Box<MyOwned> = box MyOwned;
80 //~^ ERROR allocations are not allowed in statics
82 static mut STATIC12: UnsafeStruct = UnsafeStruct;
84 static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
85 field2: SafeEnum::Variant3(WithDtor)};
87 static mut STATIC14: SafeStruct = SafeStruct {
88 field1: SafeEnum::Variant1,
89 field2: SafeEnum::Variant4("str".to_string())
90 //~^ ERROR cannot call non-const fn
93 static STATIC15: &'static [Box<MyOwned>] = &[
94 box MyOwned, //~ ERROR allocations are not allowed in statics
95 box MyOwned, //~ ERROR allocations are not allowed in statics
98 static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
99 &box MyOwned, //~ ERROR allocations are not allowed in statics
100 &box MyOwned, //~ ERROR allocations are not allowed in statics
103 static mut STATIC17: SafeEnum = SafeEnum::Variant1;
105 static STATIC19: Box<isize> =
107 //~^ ERROR allocations are not allowed in statics
110 let y = { static x: Box<isize> = box 3; x };
111 //~^ ERROR allocations are not allowed in statics
112 //~| ERROR cannot move out of static item