1 // Copyright 2014 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.
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.
11 // Verifies all possible restrictions for statics values.
14 #![feature(box_syntax)]
20 impl Drop for WithDtor {
24 // This enum will be used to test the following rules:
25 // 1. Variants are safe for static
26 // 2. Expr calls are allowed as long as they arguments are safe
27 // 3. Expr calls with unsafe arguments for statics are rejected
36 static STATIC1: SafeEnum = SafeEnum::Variant1;
37 static STATIC2: SafeEnum = SafeEnum::Variant2(0);
38 static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
45 impl Drop for UnsafeEnum {
50 static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
51 static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
60 // Struct fields are safe, hence this static should be safe
61 static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
63 static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
64 field2: SafeEnum::Variant3(WithDtor)};
66 // Test variadic constructor for structs. The base struct should be examined
67 // as well as every field present in the constructor.
68 // This example shouldn't fail because all the fields are safe.
69 static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
70 ..SafeStruct{field1: SafeEnum::Variant1,
71 field2: SafeEnum::Variant1}};
73 // This example should fail because field1 in the base struct is not safe
74 static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
75 ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
76 //~^ ERROR destructors cannot be evaluated at compile-time
77 field2: SafeEnum::Variant1}};
81 impl Drop for UnsafeStruct {
85 static STATIC10: UnsafeStruct = UnsafeStruct;
89 static STATIC11: Box<MyOwned> = box MyOwned;
90 //~^ ERROR allocations are not allowed in statics
92 static mut STATIC12: UnsafeStruct = UnsafeStruct;
94 static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
95 field2: SafeEnum::Variant3(WithDtor)};
97 static mut STATIC14: SafeStruct = SafeStruct {
98 field1: SafeEnum::Variant1,
99 field2: SafeEnum::Variant4("str".to_string())
100 //~^ ERROR calls in statics are limited to constant functions
103 static STATIC15: &'static [Box<MyOwned>] = &[
104 box MyOwned, //~ ERROR allocations are not allowed in statics
105 box MyOwned, //~ ERROR allocations are not allowed in statics
108 static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
109 &box MyOwned, //~ ERROR allocations are not allowed in statics
110 &box MyOwned, //~ ERROR allocations are not allowed in statics
113 static mut STATIC17: SafeEnum = SafeEnum::Variant1;
115 static STATIC19: Box<isize> =
117 //~^ ERROR allocations are not allowed in statics
120 let y = { static x: Box<isize> = box 3; x };
121 //~^ ERROR allocations are not allowed in statics
122 //~^^ ERROR cannot move out of static item