]> git.proxmox.com Git - rustc.git/blame - src/test/ui/check-static-values-constraints.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / src / test / ui / check-static-values-constraints.rs
CommitLineData
1a4d82fc
JJ
1// Verifies all possible restrictions for statics values.
2
3b2f2976 3#![allow(warnings)]
1a4d82fc
JJ
4#![feature(box_syntax)]
5
6use std::marker;
7
8struct WithDtor;
9
10impl 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
18enum SafeEnum {
19 Variant1,
20 Variant2(isize),
21 Variant3(WithDtor),
22 Variant4(String)
23}
24
25// These should be ok
26static STATIC1: SafeEnum = SafeEnum::Variant1;
27static STATIC2: SafeEnum = SafeEnum::Variant2(0);
1a4d82fc 28static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
1a4d82fc 29
1a4d82fc
JJ
30enum UnsafeEnum {
31 Variant5,
32 Variant6(isize)
33}
34
35impl Drop for UnsafeEnum {
36 fn drop(&mut self) {}
37}
38
39
40static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
1a4d82fc 41static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
1a4d82fc
JJ
42
43
44struct SafeStruct {
45 field1: SafeEnum,
46 field2: SafeEnum,
47}
48
49
50// Struct fields are safe, hence this static should be safe
51static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
52
1a4d82fc
JJ
53static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
54 field2: SafeEnum::Variant3(WithDtor)};
1a4d82fc
JJ
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.
59static 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
64static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
65 ..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
ea8adc8c 66//~^ ERROR destructors cannot be evaluated at compile-time
1a4d82fc 67 field2: SafeEnum::Variant1}};
1a4d82fc
JJ
68
69struct UnsafeStruct;
70
71impl Drop for UnsafeStruct {
72 fn drop(&mut self) {}
73}
74
1a4d82fc 75static STATIC10: UnsafeStruct = UnsafeStruct;
1a4d82fc
JJ
76
77struct MyOwned;
78
79static STATIC11: Box<MyOwned> = box MyOwned;
85aaf69f 80//~^ ERROR allocations are not allowed in statics
1a4d82fc 81
1a4d82fc 82static mut STATIC12: UnsafeStruct = UnsafeStruct;
1a4d82fc
JJ
83
84static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
1a4d82fc 85 field2: SafeEnum::Variant3(WithDtor)};
1a4d82fc
JJ
86
87static mut STATIC14: SafeStruct = SafeStruct {
1a4d82fc
JJ
88 field1: SafeEnum::Variant1,
89 field2: SafeEnum::Variant4("str".to_string())
a7813a04 90//~^ ERROR calls in statics are limited to constant functions
1a4d82fc
JJ
91};
92
93static STATIC15: &'static [Box<MyOwned>] = &[
85aaf69f
SL
94 box MyOwned, //~ ERROR allocations are not allowed in statics
95 box MyOwned, //~ ERROR allocations are not allowed in statics
1a4d82fc
JJ
96];
97
98static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
85aaf69f
SL
99 &box MyOwned, //~ ERROR allocations are not allowed in statics
100 &box MyOwned, //~ ERROR allocations are not allowed in statics
1a4d82fc
JJ
101);
102
103static mut STATIC17: SafeEnum = SafeEnum::Variant1;
1a4d82fc
JJ
104
105static STATIC19: Box<isize> =
106 box 3;
85aaf69f 107//~^ ERROR allocations are not allowed in statics
1a4d82fc
JJ
108
109pub fn main() {
110 let y = { static x: Box<isize> = box 3; x };
85aaf69f 111 //~^ ERROR allocations are not allowed in statics
0731742a 112 //~| ERROR cannot move out of static item
1a4d82fc 113}