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.
This commit is contained in:
commit
5fb3394cbd
3 changed files with 7 additions and 10 deletions
|
@ -619,6 +619,7 @@ impl<T> MaybeUninit<T> {
|
||||||
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
|
#[rustc_const_unstable(feature = "const_maybe_uninit_assume_init", issue = "none")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
#[rustc_diagnostic_item = "assume_init"]
|
#[rustc_diagnostic_item = "assume_init"]
|
||||||
|
#[track_caller]
|
||||||
pub const unsafe fn assume_init(self) -> T {
|
pub const unsafe fn assume_init(self) -> T {
|
||||||
// SAFETY: the caller must guarantee that `self` is initialized.
|
// SAFETY: the caller must guarantee that `self` is initialized.
|
||||||
// This also means that `self` must be a `value` variant.
|
// This also means that `self` must be a `value` variant.
|
||||||
|
@ -690,6 +691,7 @@ impl<T> MaybeUninit<T> {
|
||||||
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
#[unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
||||||
#[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
#[rustc_const_unstable(feature = "maybe_uninit_extra", issue = "63567")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
#[track_caller]
|
||||||
pub const unsafe fn assume_init_read(&self) -> T {
|
pub const unsafe fn assume_init_read(&self) -> T {
|
||||||
// SAFETY: the caller must guarantee that `self` is initialized.
|
// SAFETY: the caller must guarantee that `self` is initialized.
|
||||||
// Reading from `self.as_ptr()` is safe since `self` should be initialized.
|
// Reading from `self.as_ptr()` is safe since `self` should be initialized.
|
||||||
|
@ -937,6 +939,7 @@ impl<T> MaybeUninit<T> {
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
|
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
#[track_caller]
|
||||||
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
|
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// * The caller guarantees that all elements of the array are initialized
|
// * The caller guarantees that all elements of the array are initialized
|
||||||
|
|
|
@ -622,6 +622,7 @@ pub const fn needs_drop<T>() -> bool {
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
#[rustc_diagnostic_item = "mem_zeroed"]
|
#[rustc_diagnostic_item = "mem_zeroed"]
|
||||||
|
#[track_caller]
|
||||||
pub unsafe fn zeroed<T>() -> T {
|
pub unsafe fn zeroed<T>() -> T {
|
||||||
// SAFETY: the caller must guarantee that an all-zero value is valid for `T`.
|
// SAFETY: the caller must guarantee that an all-zero value is valid for `T`.
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -657,6 +658,7 @@ pub unsafe fn zeroed<T>() -> T {
|
||||||
#[allow(deprecated_in_future)]
|
#[allow(deprecated_in_future)]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
#[rustc_diagnostic_item = "mem_uninitialized"]
|
#[rustc_diagnostic_item = "mem_uninitialized"]
|
||||||
|
#[track_caller]
|
||||||
pub unsafe fn uninitialized<T>() -> T {
|
pub unsafe fn uninitialized<T>() -> T {
|
||||||
// SAFETY: the caller must guarantee that an unitialized value is valid for `T`.
|
// SAFETY: the caller must guarantee that an unitialized value is valid for `T`.
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -1,17 +1,9 @@
|
||||||
error: any use of this value will cause an error
|
error: any use of this value will cause an error
|
||||||
--> $SRC_DIR/core/src/mem/maybe_uninit.rs:LL:COL
|
--> $DIR/assume-type-intrinsics.rs:11:9
|
||||||
|
|
|
||||||
LL | intrinsics::assert_inhabited::<T>();
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
||||||
| |
|
|
||||||
| 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
|
|
||||||
|
|
|
|
||||||
LL | / const _BAD: () = unsafe {
|
LL | / const _BAD: () = unsafe {
|
||||||
LL | | MaybeUninit::<!>::uninit().assume_init();
|
LL | | MaybeUninit::<!>::uninit().assume_init();
|
||||||
|
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ aborted execution: attempted to instantiate uninhabited type `!`
|
||||||
LL | | };
|
LL | | };
|
||||||
| |______-
|
| |______-
|
||||||
|
|
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue