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