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.
17 impl Drop for WithDtor {
21 // This enum will be used to test the following rules:
22 // 1. Variants are safe for static
23 // 2. Expr calls are allowed as long as they arguments are safe
24 // 3. Expr calls with unsafe arguments for statics are rejected
33 static STATIC1: SafeEnum = SafeEnum::Variant1;
34 static STATIC2: SafeEnum = SafeEnum::Variant2(0);
36 // This one should fail
37 static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
38 //~^ ERROR statics are not allowed to have destructors
41 // This enum will be used to test that variants
42 // are considered unsafe if their enum type implements
49 impl Drop for UnsafeEnum {
54 static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
55 //~^ ERROR statics are not allowed to have destructors
56 static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
57 //~^ ERROR statics are not allowed to have destructors
66 // Struct fields are safe, hence this static should be safe
67 static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
69 // field2 has an unsafe value, hence this should fail
70 static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
71 field2: SafeEnum::Variant3(WithDtor)};
72 //~^ ERROR statics are not allowed to have destructors
74 // Test variadic constructor for structs. The base struct should be examined
75 // as well as every field present in the constructor.
76 // This example shouldn't fail because all the fields are safe.
77 static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
78 ..SafeStruct{field1: SafeEnum::Variant1,
79 field2: SafeEnum::Variant1}};
81 // This example should fail because field1 in the base struct is not safe
82 static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
83 ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
84 field2: SafeEnum::Variant1}};
85 //~^^ ERROR statics are not allowed to have destructors
89 impl Drop for UnsafeStruct {
93 // Types with destructors are not allowed for statics
94 static STATIC10: UnsafeStruct = UnsafeStruct;
95 //~^ ERROR statics are not allowed to have destructor
99 static STATIC11: Box<MyOwned> = box MyOwned;
100 //~^ ERROR statics are not allowed to have custom pointers
102 // The following examples test that mutable structs are just forbidden
103 // to have types with destructors
105 static mut STATIC12: UnsafeStruct = UnsafeStruct;
106 //~^ ERROR mutable statics are not allowed to have destructors
107 //~^^ ERROR statics are not allowed to have destructors
109 static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
110 //~^ ERROR mutable statics are not allowed to have destructors
111 field2: SafeEnum::Variant3(WithDtor)};
112 //~^ ERROR: statics are not allowed to have destructors
114 static mut STATIC14: SafeStruct = SafeStruct {
115 //~^ ERROR mutable statics are not allowed to have destructors
116 field1: SafeEnum::Variant1,
117 field2: SafeEnum::Variant4("str".to_string())
120 static STATIC15: &'static [Box<MyOwned>] = &[
121 box MyOwned, //~ ERROR statics are not allowed to have custom pointers
122 box MyOwned, //~ ERROR statics are not allowed to have custom pointers
125 static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
126 &box MyOwned, //~ ERROR statics are not allowed to have custom pointers
127 &box MyOwned, //~ ERROR statics are not allowed to have custom pointers
130 static mut STATIC17: SafeEnum = SafeEnum::Variant1;
131 //~^ ERROR mutable statics are not allowed to have destructors
133 static STATIC19: Box<int> =
135 //~^ ERROR statics are not allowed to have custom pointers
138 let y = { static x: Box<int> = box 3; x };
139 //~^ ERROR statics are not allowed to have custom pointers