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