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.
10 #![feature(managed_boxes)]
12 // Verifies all possible restrictions for static items values.
14 use std::kinds::marker;
18 impl Drop for WithDtor {
22 // This enum will be used to test the following rules:
23 // 1. Variants are safe for static
24 // 2. Expr calls are allowed as long as they arguments are safe
25 // 3. Expr calls with unsafe arguments for static items are rejected
34 static STATIC1: SafeEnum = Variant1;
35 static STATIC2: SafeEnum = Variant2(0);
37 // This one should fail
38 static STATIC3: SafeEnum = Variant3(WithDtor);
39 //~^ ERROR static items are not allowed to have destructors
42 // This enum will be used to test that variants
43 // are considered unsafe if their enum type implements
50 impl Drop for UnsafeEnum {
55 static STATIC4: UnsafeEnum = Variant5;
56 //~^ ERROR static items are not allowed to have destructors
57 static STATIC5: UnsafeEnum = Variant6(0);
58 //~^ ERROR static items are not allowed to have destructors
67 // Struct fields are safe, hence this static should be safe
68 static STATIC6: SafeStruct = SafeStruct{field1: Variant1, field2: Variant2(0)};
70 // field2 has an unsafe value, hence this should fail
71 static STATIC7: SafeStruct = SafeStruct{field1: Variant1, field2: Variant3(WithDtor)};
72 //~^ ERROR static items are not allowed to have destructors
74 // Test variadic constructor for structs. The base struct should be examined
75 // as well as every field persent in the constructor.
76 // This example shouldn't fail because all the fields are safe.
77 static STATIC8: SafeStruct = SafeStruct{field1: Variant1,
78 ..SafeStruct{field1: Variant1, field2: Variant1}};
80 // This example should fail because field1 in the base struct is not safe
81 static STATIC9: SafeStruct = SafeStruct{field1: Variant1,
82 ..SafeStruct{field1: Variant3(WithDtor), field2: Variant1}};
83 //~^ ERROR static items are not allowed to have destructors
87 impl Drop for UnsafeStruct {
91 // Types with destructors are not allowed for statics
92 static STATIC10: UnsafeStruct = UnsafeStruct;
93 //~^ ERROR static items are not allowed to have destructor
97 static STATIC11: ~MyOwned = ~MyOwned;
98 //~^ ERROR static items are not allowed to have owned pointers
100 // The following examples test that mutable structs are just forbidden
101 // to have types with destructors
103 static mut STATIC12: UnsafeStruct = UnsafeStruct;
104 //~^ ERROR mutable static items are not allowed to have destructors
106 static mut STATIC13: SafeStruct = SafeStruct{field1: Variant1, field2: Variant3(WithDtor)};
107 //~^ ERROR mutable static items are not allowed to have destructors
109 static mut STATIC14: SafeStruct = SafeStruct{field1: Variant1, field2: Variant4("str".to_owned())};
110 //~^ ERROR mutable static items are not allowed to have destructors
112 static STATIC15: &'static [~MyOwned] = &'static [~MyOwned, ~MyOwned];
113 //~^ ERROR static items are not allowed to have owned pointers
114 //~^^ ERROR static items are not allowed to have owned pointers
116 static STATIC16: (&'static ~MyOwned, &'static ~MyOwned) = (&'static ~MyOwned, &'static ~MyOwned);
117 //~^ ERROR static items are not allowed to have owned pointers
118 //~^^ ERROR static items are not allowed to have owned pointers
120 static mut STATIC17: SafeEnum = Variant1;
121 //~^ ERROR mutable static items are not allowed to have destructors
123 static STATIC18: @SafeStruct = @SafeStruct{field1: Variant1, field2: Variant2(0)};
124 //~^ ERROR static items are not allowed to have managed pointers
126 static STATIC19: ~int = box 3;
127 //~^ ERROR static items are not allowed to have owned pointers
130 let y = { static x: ~int = ~3; x };
131 //~^ ERROR static items are not allowed to have owned pointers