2014-10-07 04:16:35 +00:00
|
|
|
// Verifies all possible restrictions for statics values.
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
#![allow(warnings)]
|
2015-01-08 02:53:58 +00:00
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2015-01-06 22:33:42 +00:00
|
|
|
use std::marker;
|
2014-02-21 19:31:50 +00:00
|
|
|
|
2014-02-26 18:22:41 +00:00
|
|
|
struct WithDtor;
|
|
|
|
|
|
|
|
impl Drop for WithDtor {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This enum will be used to test the following rules:
|
|
|
|
// 1. Variants are safe for static
|
|
|
|
// 2. Expr calls are allowed as long as they arguments are safe
|
2014-10-07 04:16:35 +00:00
|
|
|
// 3. Expr calls with unsafe arguments for statics are rejected
|
2014-02-26 18:22:41 +00:00
|
|
|
enum SafeEnum {
|
|
|
|
Variant1,
|
2015-01-08 10:54:35 +00:00
|
|
|
Variant2(isize),
|
2014-02-26 18:22:41 +00:00
|
|
|
Variant3(WithDtor),
|
2014-05-22 23:57:53 +00:00
|
|
|
Variant4(String)
|
2014-02-26 18:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// These should be ok
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC1: SafeEnum = SafeEnum::Variant1;
|
|
|
|
static STATIC2: SafeEnum = SafeEnum::Variant2(0);
|
|
|
|
static STATIC3: SafeEnum = SafeEnum::Variant3(WithDtor);
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
enum UnsafeEnum {
|
|
|
|
Variant5,
|
2015-01-08 10:54:35 +00:00
|
|
|
Variant6(isize)
|
2014-02-26 18:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for UnsafeEnum {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC4: UnsafeEnum = UnsafeEnum::Variant5;
|
|
|
|
static STATIC5: UnsafeEnum = UnsafeEnum::Variant6(0);
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct SafeStruct {
|
|
|
|
field1: SafeEnum,
|
|
|
|
field2: SafeEnum,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Struct fields are safe, hence this static should be safe
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC6: SafeStruct = SafeStruct{field1: SafeEnum::Variant1, field2: SafeEnum::Variant2(0)};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC7: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
|
|
|
|
field2: SafeEnum::Variant3(WithDtor)};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
// Test variadic constructor for structs. The base struct should be examined
|
2014-08-01 23:42:13 +00:00
|
|
|
// as well as every field present in the constructor.
|
2014-02-26 18:22:41 +00:00
|
|
|
// This example shouldn't fail because all the fields are safe.
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC8: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
|
|
|
|
..SafeStruct{field1: SafeEnum::Variant1,
|
|
|
|
field2: SafeEnum::Variant1}};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
// This example should fail because field1 in the base struct is not safe
|
2014-11-06 08:05:53 +00:00
|
|
|
static STATIC9: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
|
|
|
|
..SafeStruct{field1: SafeEnum::Variant3(WithDtor),
|
2022-09-23 14:22:36 +00:00
|
|
|
//~^ ERROR destructor of
|
2014-11-06 08:05:53 +00:00
|
|
|
field2: SafeEnum::Variant1}};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
struct UnsafeStruct;
|
|
|
|
|
|
|
|
impl Drop for UnsafeStruct {
|
|
|
|
fn drop(&mut self) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
static STATIC10: UnsafeStruct = UnsafeStruct;
|
|
|
|
|
2014-04-16 01:17:48 +00:00
|
|
|
struct MyOwned;
|
|
|
|
|
2014-05-06 01:56:44 +00:00
|
|
|
static STATIC11: Box<MyOwned> = box MyOwned;
|
2014-12-30 20:36:03 +00:00
|
|
|
//~^ ERROR allocations are not allowed in statics
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
static mut STATIC12: UnsafeStruct = UnsafeStruct;
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
static mut STATIC13: SafeStruct = SafeStruct{field1: SafeEnum::Variant1,
|
|
|
|
field2: SafeEnum::Variant3(WithDtor)};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2014-05-13 00:56:43 +00:00
|
|
|
static mut STATIC14: SafeStruct = SafeStruct {
|
2014-11-06 08:05:53 +00:00
|
|
|
field1: SafeEnum::Variant1,
|
|
|
|
field2: SafeEnum::Variant4("str".to_string())
|
2021-12-09 17:10:05 +00:00
|
|
|
//~^ ERROR cannot call non-const fn
|
2014-05-13 00:56:43 +00:00
|
|
|
};
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2014-10-07 04:16:35 +00:00
|
|
|
static STATIC15: &'static [Box<MyOwned>] = &[
|
2014-12-30 20:36:03 +00:00
|
|
|
box MyOwned, //~ ERROR allocations are not allowed in statics
|
|
|
|
box MyOwned, //~ ERROR allocations are not allowed in statics
|
2014-10-07 04:16:35 +00:00
|
|
|
];
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2014-10-07 04:16:35 +00:00
|
|
|
static STATIC16: (&'static Box<MyOwned>, &'static Box<MyOwned>) = (
|
2014-12-30 20:36:03 +00:00
|
|
|
&box MyOwned, //~ ERROR allocations are not allowed in statics
|
|
|
|
&box MyOwned, //~ ERROR allocations are not allowed in statics
|
2014-10-07 04:16:35 +00:00
|
|
|
);
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
static mut STATIC17: SafeEnum = SafeEnum::Variant1;
|
2014-02-26 18:22:41 +00:00
|
|
|
|
2015-01-08 10:54:35 +00:00
|
|
|
static STATIC19: Box<isize> =
|
2014-10-07 04:16:35 +00:00
|
|
|
box 3;
|
2014-12-30 20:36:03 +00:00
|
|
|
//~^ ERROR allocations are not allowed in statics
|
2014-02-26 18:22:41 +00:00
|
|
|
|
|
|
|
pub fn main() {
|
2015-01-08 10:54:35 +00:00
|
|
|
let y = { static x: Box<isize> = box 3; x };
|
2014-12-30 20:36:03 +00:00
|
|
|
//~^ ERROR allocations are not allowed in statics
|
2018-12-28 19:05:22 +00:00
|
|
|
//~| ERROR cannot move out of static item
|
2014-02-26 18:22:41 +00:00
|
|
|
}
|