]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/check-static-values-constraints.rs
Replace all ~"" with "".to_owned()
[rust.git] / src / test / compile-fail / check-static-values-constraints.rs
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.
4 //
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)]
11
12 // Verifies all possible restrictions for static items values.
13
14 use std::kinds::marker;
15
16 struct WithDtor;
17
18 impl Drop for WithDtor {
19     fn drop(&mut self) {}
20 }
21
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
26 enum SafeEnum {
27     Variant1,
28     Variant2(int),
29     Variant3(WithDtor),
30     Variant4(~str)
31 }
32
33 // These should be ok
34 static STATIC1: SafeEnum = Variant1;
35 static STATIC2: SafeEnum = Variant2(0);
36
37 // This one should fail
38 static STATIC3: SafeEnum = Variant3(WithDtor);
39 //~^ ERROR static items are not allowed to have destructors
40
41
42 // This enum will be used to test that variants
43 // are considered unsafe if their enum type implements
44 // a destructor.
45 enum UnsafeEnum {
46     Variant5,
47     Variant6(int)
48 }
49
50 impl Drop for UnsafeEnum {
51     fn drop(&mut self) {}
52 }
53
54
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
59
60
61 struct SafeStruct {
62     field1: SafeEnum,
63     field2: SafeEnum,
64 }
65
66
67 // Struct fields are safe, hence this static should be safe
68 static STATIC6: SafeStruct = SafeStruct{field1: Variant1, field2: Variant2(0)};
69
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
73
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}};
79
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
84
85 struct UnsafeStruct;
86
87 impl Drop for UnsafeStruct {
88     fn drop(&mut self) {}
89 }
90
91 // Types with destructors are not allowed for statics
92 static STATIC10: UnsafeStruct = UnsafeStruct;
93 //~^ ERROR static items are not allowed to have destructor
94
95 struct MyOwned;
96
97 static STATIC11: ~MyOwned = ~MyOwned;
98 //~^ ERROR static items are not allowed to have owned pointers
99
100 // The following examples test that mutable structs are just forbidden
101 // to have types with destructors
102 // These should fail
103 static mut STATIC12: UnsafeStruct = UnsafeStruct;
104 //~^ ERROR mutable static items are not allowed to have destructors
105
106 static mut STATIC13: SafeStruct = SafeStruct{field1: Variant1, field2: Variant3(WithDtor)};
107 //~^ ERROR mutable static items are not allowed to have destructors
108
109 static mut STATIC14: SafeStruct = SafeStruct{field1: Variant1, field2: Variant4("str".to_owned())};
110 //~^ ERROR mutable static items are not allowed to have destructors
111
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
115
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
119
120 static mut STATIC17: SafeEnum = Variant1;
121 //~^ ERROR mutable static items are not allowed to have destructors
122
123 static STATIC18: @SafeStruct = @SafeStruct{field1: Variant1, field2: Variant2(0)};
124 //~^ ERROR static items are not allowed to have managed pointers
125
126 static STATIC19: ~int = box 3;
127 //~^ ERROR static items are not allowed to have owned pointers
128
129 pub fn main() {
130     let y = { static x: ~int = ~3; x };
131     //~^ ERROR static items are not allowed to have owned pointers
132 }