]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/check-static-values-constraints.rs
Merge pull request #20674 from jbcrail/fix-misspelled-comments
[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
11 // Verifies all possible restrictions for statics values.
12
13 use std::marker;
14
15 struct WithDtor;
16
17 impl Drop for WithDtor {
18     fn drop(&mut self) {}
19 }
20
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
25 enum SafeEnum {
26     Variant1,
27     Variant2(int),
28     Variant3(WithDtor),
29     Variant4(String)
30 }
31
32 // These should be ok
33 static STATIC1: SafeEnum = SafeEnum::Variant1;
34 static STATIC2: SafeEnum = SafeEnum::Variant2(0);
35
36 // This one should fail
37 static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
38 //~^ ERROR statics are not allowed to have destructors
39
40
41 // This enum will be used to test that variants
42 // are considered unsafe if their enum type implements
43 // a destructor.
44 enum UnsafeEnum {
45     Variant5,
46     Variant6(int)
47 }
48
49 impl Drop for UnsafeEnum {
50     fn drop(&mut self) {}
51 }
52
53
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
58
59
60 struct SafeStruct {
61     field1: SafeEnum,
62     field2: SafeEnum,
63 }
64
65
66 // Struct fields are safe, hence this static should be safe
67 static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
68
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
73
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}};
80
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
86
87 struct UnsafeStruct;
88
89 impl Drop for UnsafeStruct {
90     fn drop(&mut self) {}
91 }
92
93 // Types with destructors are not allowed for statics
94 static STATIC10: UnsafeStruct = UnsafeStruct;
95 //~^ ERROR statics are not allowed to have destructor
96
97 struct MyOwned;
98
99 static STATIC11: Box<MyOwned> = box MyOwned;
100 //~^ ERROR statics are not allowed to have custom pointers
101
102 // The following examples test that mutable structs are just forbidden
103 // to have types with destructors
104 // These should fail
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
108
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
113
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())
118 };
119
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
123 ];
124
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
128 );
129
130 static mut STATIC17: SafeEnum = SafeEnum::Variant1;
131 //~^ ERROR mutable statics are not allowed to have destructors
132
133 static STATIC19: Box<int> =
134     box 3;
135 //~^ ERROR statics are not allowed to have custom pointers
136
137 pub fn main() {
138     let y = { static x: Box<int> = box 3; x };
139     //~^ ERROR statics are not allowed to have custom pointers
140 }