Skip to content

Commit 2317c9b

Browse files
committed
Auto merge of rust-lang#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 eb21b92 + f3de59a commit 2317c9b

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

core/src/mem/maybe_uninit.rs

Lines changed: 3 additions & 0 deletions
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

core/src/mem/mod.rs

Lines changed: 2 additions & 0 deletions
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 {

0 commit comments

Comments
 (0)