Skip to content

Commit 5fb3394

Browse files
committed
Auto merge of #86664 - m-ou-se:uninit-track-caller, r=JohnTitor
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.
2 parents 6e0a8bf + 3f6dc9a commit 5fb3394

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

library/core/src/mem/maybe_uninit.rs

+3
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ impl<T> MaybeUninit<T> {
619619
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
620620
#[inline(always)]
621621
#[rustc_diagnostic_item = "assume_init"]
622+
#[track_caller]
622623
pub const unsafe fn assume_init(self) -> T {
623624
// SAFETY: the caller must guarantee that `self` is initialized.
624625
// This also means that `self` must be a `value` variant.
@@ -690,6 +691,7 @@ impl<T> MaybeUninit<T> {
690691
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
691692
#[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
692693
#[inline(always)]
694+
#[track_caller]
693695
pub const unsafe fn assume_init_read(&self) -> T {
694696
// SAFETY: the caller must guarantee that `self` is initialized.
695697
// Reading from `self.as_ptr()` is safe since `self` should be initialized.
@@ -937,6 +939,7 @@ impl<T> MaybeUninit<T> {
937939
/// ```
938940
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
939941
#[inline(always)]
942+
#[track_caller]
940943
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
941944
// SAFETY:
942945
// * The caller guarantees that all elements of the array are initialized

library/core/src/mem/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ pub const fn needs_drop<T>() -> bool {
622622
#[allow(deprecated_in_future)]
623623
#[allow(deprecated)]
624624
#[rustc_diagnostic_item = "mem_zeroed"]
625+
#[track_caller]
625626
pub unsafe fn zeroed<T>() -> T {
626627
// SAFETY: the caller must guarantee that an all-zero value is valid for `T`.
627628
unsafe {
@@ -657,6 +658,7 @@ pub unsafe fn zeroed<T>() -> T {
657658
#[allow(deprecated_in_future)]
658659
#[allow(deprecated)]
659660
#[rustc_diagnostic_item = "mem_uninitialized"]
661+
#[track_caller]
660662
pub unsafe fn uninitialized<T>() -> T {
661663
// SAFETY: the caller must guarantee that an unitialized value is valid for `T`.
662664
unsafe {

src/test/ui/consts/assume-type-intrinsics.stderr

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
error: any use of this value will cause an error
2-
--> $SRC_DIR/core/src/mem/maybe_uninit.rs:LL:COL
3-
|
4-
LL | intrinsics::assert_inhabited::<T>();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| aborted execution: attempted to instantiate uninhabited type `!`
8-
| inside `MaybeUninit::<!>::assume_init` at $SRC_DIR/core/src/mem/maybe_uninit.rs:LL:COL
9-
| inside `_BAD` at $DIR/assume-type-intrinsics.rs:11:9
10-
|
11-
::: $DIR/assume-type-intrinsics.rs:10:5
2+
--> $DIR/assume-type-intrinsics.rs:11:9
123
|
134
LL | / const _BAD: () = unsafe {
145
LL | | MaybeUninit::<!>::uninit().assume_init();
6+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!`
157
LL | | };
168
| |______-
179
|

0 commit comments

Comments
 (0)