2019-08-06 21:11:52 +00:00
|
|
|
// This test checks that calling `mem::{uninitialized,zeroed}` with certain types results
|
|
|
|
// in a lint.
|
|
|
|
|
2019-12-11 14:51:28 +00:00
|
|
|
#![feature(never_type, rustc_attrs)]
|
2019-08-06 21:11:52 +00:00
|
|
|
#![allow(deprecated)]
|
|
|
|
#![deny(invalid_value)]
|
|
|
|
|
2019-08-07 06:42:50 +00:00
|
|
|
use std::mem::{self, MaybeUninit};
|
2019-08-17 20:11:43 +00:00
|
|
|
use std::ptr::NonNull;
|
2019-08-17 09:48:30 +00:00
|
|
|
use std::num::NonZeroU32;
|
2019-08-07 06:42:50 +00:00
|
|
|
|
|
|
|
enum Void {}
|
|
|
|
|
|
|
|
struct Ref(&'static i32);
|
2019-08-12 07:24:13 +00:00
|
|
|
struct RefPair((&'static i32, i32));
|
2019-08-07 06:42:50 +00:00
|
|
|
|
|
|
|
struct Wrap<T> { wrapped: T }
|
2019-08-12 07:24:13 +00:00
|
|
|
enum WrapEnum<T> { Wrapped(T) }
|
2019-08-07 06:42:50 +00:00
|
|
|
|
2019-08-17 08:13:47 +00:00
|
|
|
#[rustc_layout_scalar_valid_range_start(0)]
|
|
|
|
#[rustc_layout_scalar_valid_range_end(128)]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub(crate) struct NonBig(u64);
|
|
|
|
|
2020-07-17 13:01:37 +00:00
|
|
|
/// A two-variant enum, thus needs a tag and may not remain uninitialized.
|
|
|
|
enum Fruit {
|
|
|
|
Apple,
|
|
|
|
Banana,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Looks like two variants but really only has one.
|
|
|
|
enum OneFruit {
|
|
|
|
Apple(!),
|
|
|
|
Banana,
|
|
|
|
}
|
|
|
|
|
2022-09-25 20:56:01 +00:00
|
|
|
enum OneFruitNonZero {
|
|
|
|
Apple(!),
|
|
|
|
Banana(NonZeroU32),
|
|
|
|
}
|
|
|
|
|
|
|
|
enum TwoUninhabited {
|
|
|
|
A(!),
|
2022-09-27 13:59:10 +00:00
|
|
|
B(Void),
|
2022-09-25 20:56:01 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 14:37:46 +00:00
|
|
|
#[rustc_layout_scalar_valid_range_start(254)]
|
|
|
|
#[rustc_layout_scalar_valid_range_end(1)]
|
|
|
|
pub(crate) struct WrapAroundRange(u8);
|
|
|
|
|
2019-08-07 06:42:50 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
fn generic<T: 'static>() {
|
|
|
|
unsafe {
|
|
|
|
let _val: &'static T = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: &'static T = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: Wrap<&'static T> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: Wrap<&'static T> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 21:11:52 +00:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
2019-08-17 08:13:47 +00:00
|
|
|
// Things that cannot even be zero.
|
2019-08-06 21:11:52 +00:00
|
|
|
let _val: ! = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: ! = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-07 06:42:50 +00:00
|
|
|
let _val: (i32, !) = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: (i32, !) = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: Void = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: Void = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-06 21:11:52 +00:00
|
|
|
let _val: &'static i32 = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: &'static i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-07 06:42:50 +00:00
|
|
|
let _val: Ref = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: Ref = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-06 21:11:52 +00:00
|
|
|
let _val: fn() = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: fn() = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-07 06:42:50 +00:00
|
|
|
let _val: Wrap<fn()> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: Wrap<fn()> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-12 07:24:13 +00:00
|
|
|
let _val: WrapEnum<fn()> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: WrapEnum<fn()> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: Wrap<(RefPair, i32)> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: Wrap<(RefPair, i32)> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-17 20:11:43 +00:00
|
|
|
let _val: NonNull<i32> = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: NonNull<i32> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
2019-08-17 08:13:47 +00:00
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
let _val: (NonZeroU32, i32) = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: (NonZeroU32, i32) = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-11-02 10:56:06 +00:00
|
|
|
let _val: *const dyn Send = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: *const dyn Send = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-03-27 17:10:34 +00:00
|
|
|
let _val: [fn(); 2] = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: [fn(); 2] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-09-25 20:56:01 +00:00
|
|
|
let _val: TwoUninhabited = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: TwoUninhabited = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: OneFruitNonZero = mem::zeroed(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: OneFruitNonZero = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-17 08:13:47 +00:00
|
|
|
// Things that can be zero, but not uninit.
|
|
|
|
let _val: bool = mem::zeroed();
|
2019-08-17 07:39:25 +00:00
|
|
|
let _val: bool = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
2019-08-17 08:13:47 +00:00
|
|
|
|
|
|
|
let _val: Wrap<char> = mem::zeroed();
|
2019-08-17 07:39:25 +00:00
|
|
|
let _val: Wrap<char> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-17 08:13:47 +00:00
|
|
|
let _val: NonBig = mem::zeroed();
|
|
|
|
let _val: NonBig = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2020-07-17 13:01:37 +00:00
|
|
|
let _val: Fruit = mem::zeroed();
|
|
|
|
let _val: Fruit = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-03-27 17:10:34 +00:00
|
|
|
let _val: [bool; 2] = mem::zeroed();
|
|
|
|
let _val: [bool; 2] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-07-04 22:57:41 +00:00
|
|
|
let _val: i32 = mem::zeroed();
|
|
|
|
let _val: i32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-07-05 07:50:42 +00:00
|
|
|
let _val: f32 = mem::zeroed();
|
|
|
|
let _val: f32 = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: *const () = mem::zeroed();
|
|
|
|
let _val: *const () = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
|
|
|
let _val: *const [()] = mem::zeroed();
|
|
|
|
let _val: *const [()] = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-10-22 14:37:46 +00:00
|
|
|
let _val: WrapAroundRange = mem::zeroed();
|
|
|
|
let _val: WrapAroundRange = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-09-25 20:56:01 +00:00
|
|
|
// Things where 0 is okay due to rustc implementation details,
|
|
|
|
// but that are not guaranteed to keep working.
|
|
|
|
let _val: Result<i32, i32> = mem::zeroed();
|
|
|
|
let _val: Result<i32, i32> = mem::uninitialized(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2022-09-20 09:33:16 +00:00
|
|
|
// Some things that happen to be UB-free due to rustc implementation details,
|
2022-09-25 20:56:01 +00:00
|
|
|
// but are not guaranteed to keep working.
|
|
|
|
let _val: OneFruit = mem::zeroed();
|
|
|
|
let _val: OneFruit = mem::uninitialized();
|
|
|
|
|
2019-08-17 09:48:30 +00:00
|
|
|
// Transmute-from-0
|
|
|
|
let _val: &'static i32 = mem::transmute(0usize); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: &'static [i32] = mem::transmute((0usize, 0usize)); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: NonZeroU32 = mem::transmute(0); //~ ERROR: does not permit zero-initialization
|
|
|
|
|
2019-11-02 15:12:33 +00:00
|
|
|
// `MaybeUninit` cases
|
|
|
|
let _val: NonNull<i32> = MaybeUninit::zeroed().assume_init(); //~ ERROR: does not permit zero-initialization
|
|
|
|
let _val: NonNull<i32> = MaybeUninit::uninit().assume_init(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
let _val: bool = MaybeUninit::uninit().assume_init(); //~ ERROR: does not permit being left uninitialized
|
|
|
|
|
2019-08-17 08:13:47 +00:00
|
|
|
// Some more types that should work just fine.
|
2019-08-06 21:11:52 +00:00
|
|
|
let _val: Option<&'static i32> = mem::zeroed();
|
|
|
|
let _val: Option<fn()> = mem::zeroed();
|
2019-08-07 06:42:50 +00:00
|
|
|
let _val: MaybeUninit<&'static i32> = mem::zeroed();
|
2019-11-02 15:12:33 +00:00
|
|
|
let _val: bool = MaybeUninit::zeroed().assume_init();
|
2022-03-27 17:10:34 +00:00
|
|
|
let _val: [bool; 0] = MaybeUninit::uninit().assume_init();
|
|
|
|
let _val: [!; 0] = MaybeUninit::zeroed().assume_init();
|
2019-08-06 21:11:52 +00:00
|
|
|
}
|
|
|
|
}
|