]> git.lizzy.rs Git - rust.git/blob - tests/ui/check-static-values-constraints.rs
Rollup merge of #106707 - ehuss:remove-dupe-sha-1, r=Mark-Simulacrum
[rust.git] / tests / ui / check-static-values-constraints.rs
1 // Verifies all possible restrictions for statics values.
2
3 #![allow(warnings)]
4 #![feature(box_syntax)]
5
6 use std::marker;
7
8 struct WithDtor;
9
10 impl Drop for WithDtor {
11     fn drop(&mut self) {}
12 }
13
14 // This enum will be used to test the following rules:
15 // 1. Variants are safe for static
16 // 2. Expr calls are allowed as long as they arguments are safe
17 // 3. Expr calls with unsafe arguments for statics are rejected
18 enum SafeEnum {
19     Variant1,
20     Variant2(isize),
21     Variant3(WithDtor),
22     Variant4(String)
23 }
24
25 // These should be ok
26 static STATIC1: SafeEnum = SafeEnum::Variant1;
27 static STATIC2: SafeEnum = SafeEnum::Variant2(0);
28 static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
29
30 enum UnsafeEnum {
31     Variant5,
32     Variant6(isize)
33 }
34
35 impl Drop for UnsafeEnum {
36     fn drop(&mut self) {}
37 }
38
39
40 static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
41 static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
42
43
44 struct SafeStruct {
45     field1: SafeEnum,
46     field2: SafeEnum,
47 }
48
49
50 // Struct fields are safe, hence this static should be safe
51 static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
52
53 static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
54                                         field2: SafeEnum::Variant3(WithDtor)};
55
56 // Test variadic constructor for structs. The base struct should be examined
57 // as well as every field present in the constructor.
58 // This example shouldn't fail because all the fields are safe.
59 static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
60                                         ..SafeStruct{field1: SafeEnum::Variant1,
61                                                      field2: SafeEnum::Variant1}};
62
63 // This example should fail because field1 in the base struct is not safe
64 static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
65                                         ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
66 //~^ ERROR destructor of
67                                                      field2: SafeEnum::Variant1}};
68
69 struct UnsafeStruct;
70
71 impl Drop for UnsafeStruct {
72     fn drop(&mut self) {}
73 }
74
75 static STATIC10: UnsafeStruct = UnsafeStruct;
76
77 struct MyOwned;
78
79 static STATIC11: Box<MyOwned> = box MyOwned;
80 //~^ ERROR allocations are not allowed in statics
81
82 static mut STATIC12: UnsafeStruct = UnsafeStruct;
83
84 static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
85                                              field2: SafeEnum::Variant3(WithDtor)};
86
87 static mut STATIC14: SafeStruct = SafeStruct {
88     field1: SafeEnum::Variant1,
89     field2: SafeEnum::Variant4("str".to_string())
90 //~^ ERROR cannot call non-const fn
91 };
92
93 static STATIC15: &'static [Box<MyOwned>] = &[
94     box MyOwned, //~ ERROR allocations are not allowed in statics
95     box MyOwned, //~ ERROR allocations are not allowed in statics
96 ];
97
98 static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
99     &box MyOwned, //~ ERROR allocations are not allowed in statics
100     &box MyOwned, //~ ERROR allocations are not allowed in statics
101 );
102
103 static mut STATIC17: SafeEnum = SafeEnum::Variant1;
104
105 static STATIC19: Box<isize> =
106     box 3;
107 //~^ ERROR allocations are not allowed in statics
108
109 pub fn main() {
110     let y = { static x: Box<isize> = box 3; x };
111     //~^ ERROR allocations are not allowed in statics
112     //~| ERROR cannot move out of static item
113 }