diff options
| author | Ralf Jung <post@ralfj.de> | 2018-12-27 09:40:33 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2018-12-27 13:04:53 +0100 |
| commit | fff905bc698c0b6ee8b407273e7151d7b9a7a89a (patch) | |
| tree | 3a0ad6d20a36a7b5401a0cf724c2026b3ca44185 /src/libcore | |
| parent | a7be40c65ae8ace467c9c40b0a22642973e31a13 (diff) | |
| download | rust-fff905bc698c0b6ee8b407273e7151d7b9a7a89a.tar.gz rust-fff905bc698c0b6ee8b407273e7151d7b9a7a89a.zip | |
panic when calling MaybeUninhabited::into_inner on uninhabited type
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/intrinsics.rs | 5 | ||||
| -rw-r--r-- | src/libcore/mem.rs | 6 |
2 files changed, 11 insertions, 0 deletions
diff --git a/src/libcore/intrinsics.rs b/src/libcore/intrinsics.rs index 7508257f780..4f5310f5285 100644 --- a/src/libcore/intrinsics.rs +++ b/src/libcore/intrinsics.rs @@ -690,6 +690,11 @@ extern "rust-intrinsic" { /// crate it is invoked in. pub fn type_id<T: ?Sized + 'static>() -> u64; + /// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited: + /// This will statically either panic, or do nothing. + #[cfg(not(stage0))] + pub fn panic_if_uninhabited<T>(); + /// Creates a value initialized to zero. /// /// `init` is unsafe because it returns a zeroed-out datum, diff --git a/src/libcore/mem.rs b/src/libcore/mem.rs index c024868714c..8fcbb73d9ce 100644 --- a/src/libcore/mem.rs +++ b/src/libcore/mem.rs @@ -492,6 +492,8 @@ pub const fn needs_drop<T>() -> bool { #[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::zeroed` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn zeroed<T>() -> T { + #[cfg(not(stage0))] + intrinsics::panic_if_uninhabited::<T>(); intrinsics::init() } @@ -624,6 +626,8 @@ pub unsafe fn zeroed<T>() -> T { #[rustc_deprecated(since = "2.0.0", reason = "use `mem::MaybeUninit::uninitialized` instead")] #[stable(feature = "rust1", since = "1.0.0")] pub unsafe fn uninitialized<T>() -> T { + #[cfg(not(stage0))] + intrinsics::panic_if_uninhabited::<T>(); intrinsics::uninit() } @@ -1128,6 +1132,8 @@ impl<T> MaybeUninit<T> { #[unstable(feature = "maybe_uninit", issue = "53491")] #[inline(always)] pub unsafe fn into_inner(self) -> T { + #[cfg(not(stage0))] + intrinsics::panic_if_uninhabited::<T>(); ManuallyDrop::into_inner(self.value) } |
