diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index 8e23d6bea3b..ff177c70d39 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -98,13 +98,12 @@ use crate::intrinsics; #[rustc_const_stable(feature = "const_unreachable_unchecked", since = "1.57.0")] #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces pub const unsafe fn unreachable_unchecked() -> ! { - crate::panic::debug_assert_nounwind!( - false, - "hint::unreachable_unchecked must never be reached" - ); // SAFETY: the safety contract for `intrinsics::unreachable` must // be upheld by the caller. - unsafe { intrinsics::unreachable() } + unsafe { + intrinsics::assert_unsafe_precondition!("hint::unreachable_unchecked must never be reached", () => false); + intrinsics::unreachable() + } } /// Emits a machine instruction to signal the processor that it is running in diff --git a/library/core/src/panic.rs b/library/core/src/panic.rs index f5d18ca0b3f..4ca5af1eaea 100644 --- a/library/core/src/panic.rs +++ b/library/core/src/panic.rs @@ -139,6 +139,11 @@ pub macro unreachable_2021 { ), } +/// Asserts that a boolean expression is `true`, and perform a non-unwinding panic otherwise. +/// +/// This macro is similar to `debug_assert!`, but is intended to be used in code that should not +/// unwind. For example, checks in `_unchecked` functions that are intended for debugging but should +/// not compromise unwind safety. #[doc(hidden)] #[unstable(feature = "core_panic", issue = "none")] #[allow_internal_unstable(core_panic, const_format_args)] diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs index 1c906b7d3b4..553e18c6883 100644 --- a/library/core/src/panicking.rs +++ b/library/core/src/panicking.rs @@ -84,6 +84,7 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { #[rustc_nounwind] #[rustc_const_unstable(feature = "core_panic", issue = "none")] pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! { + #[track_caller] fn runtime(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! { if cfg!(feature = "panic_immediate_abort") { super::intrinsics::abort() @@ -109,6 +110,7 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo } #[inline] + #[track_caller] const fn comptime(fmt: fmt::Arguments<'_>, _force_no_backtrace: bool) -> ! { panic_fmt(fmt); }