From 3f6dc9a8abe9ffd804fed2ddaabb1c67de4c8fbd Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 27 Jun 2021 12:41:50 +0000 Subject: [PATCH] Add #[track_caller] for some function in core::mem. These functions can panic for some types. This makes the panic point to the code that calls e.g. mem::uninitialized(), instead of inside the definition of mem::uninitialized. --- library/core/src/mem/maybe_uninit.rs | 3 +++ library/core/src/mem/mod.rs | 2 ++ src/test/ui/consts/assume-type-intrinsics.stderr | 12 ++---------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index d3ebc1cebb6..9c88a623361 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -619,6 +619,7 @@ impl MaybeUninit { #[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")] #[inline(always)] #[rustc_diagnostic_item = "assume_init"] + #[track_caller] pub const unsafe fn assume_init(self) -> T { // SAFETY: the caller must guarantee that `self` is initialized. // This also means that `self` must be a `value` variant. @@ -690,6 +691,7 @@ impl MaybeUninit { #[unstable(feature = "maybe_uninit_extra", issue = "63567")] #[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")] #[inline(always)] + #[track_caller] pub const unsafe fn assume_init_read(&self) -> T { // SAFETY: the caller must guarantee that `self` is initialized. // Reading from `self.as_ptr()` is safe since `self` should be initialized. @@ -937,6 +939,7 @@ impl MaybeUninit { /// ``` #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")] #[inline(always)] + #[track_caller] pub unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { // SAFETY: // * The caller guarantees that all elements of the array are initialized diff --git a/library/core/src/mem/mod.rs b/library/core/src/mem/mod.rs index 2c75de39ffa..aeb5d238162 100644 --- a/library/core/src/mem/mod.rs +++ b/library/core/src/mem/mod.rs @@ -622,6 +622,7 @@ pub const fn needs_drop() -> bool { #[allow(deprecated_in_future)] #[allow(deprecated)] #[rustc_diagnostic_item = "mem_zeroed"] +#[track_caller] pub unsafe fn zeroed() -> T { // SAFETY: the caller must guarantee that an all-zero value is valid for `T`. unsafe { @@ -657,6 +658,7 @@ pub unsafe fn zeroed() -> T { #[allow(deprecated_in_future)] #[allow(deprecated)] #[rustc_diagnostic_item = "mem_uninitialized"] +#[track_caller] pub unsafe fn uninitialized() -> T { // SAFETY: the caller must guarantee that an unitialized value is valid for `T`. unsafe { diff --git a/src/test/ui/consts/assume-type-intrinsics.stderr b/src/test/ui/consts/assume-type-intrinsics.stderr index d46ce44b7ba..e660730396f 100644 --- a/src/test/ui/consts/assume-type-intrinsics.stderr +++ b/src/test/ui/consts/assume-type-intrinsics.stderr @@ -1,17 +1,9 @@ error: any use of this value will cause an error - --> $SRC_DIR/core/src/mem/maybe_uninit.rs:LL:COL - | -LL | intrinsics::assert_inhabited::(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | aborted execution: attempted to instantiate uninhabited type `!` - | inside `MaybeUninit::::assume_init` at $SRC_DIR/core/src/mem/maybe_uninit.rs:LL:COL - | inside `_BAD` at $DIR/assume-type-intrinsics.rs:11:9 - | - ::: $DIR/assume-type-intrinsics.rs:10:5 + --> $DIR/assume-type-intrinsics.rs:11:9 | LL | / const _BAD: () = unsafe { LL | | MaybeUninit::::uninit().assume_init(); + | | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!` LL | | }; | |______- |