-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add debug_assert_nounwind
and convert assert_unsafe_precondition
#110303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,28 +82,43 @@ pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { | |
// and unwinds anyway, we will hit the "unwinding out of nounwind function" guard, | ||
// which causes a "panic in a function that cannot unwind". | ||
#[rustc_nounwind] | ||
pub fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: bool) -> ! { | ||
if cfg!(feature = "panic_immediate_abort") { | ||
super::intrinsics::abort() | ||
} | ||
#[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) -> ! { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably have at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in #118362 |
||
if cfg!(feature = "panic_immediate_abort") { | ||
super::intrinsics::abort() | ||
} | ||
|
||
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call | ||
// that gets resolved to the `#[panic_handler]` function. | ||
extern "Rust" { | ||
#[lang = "panic_impl"] | ||
fn panic_impl(pi: &PanicInfo<'_>) -> !; | ||
// NOTE This function never crosses the FFI boundary; it's a Rust-to-Rust call | ||
// that gets resolved to the `#[panic_handler]` function. | ||
extern "Rust" { | ||
#[lang = "panic_impl"] | ||
fn panic_impl(pi: &PanicInfo<'_>) -> !; | ||
} | ||
|
||
// PanicInfo with the `can_unwind` flag set to false forces an abort. | ||
let pi = PanicInfo::internal_constructor( | ||
Some(&fmt), | ||
Location::caller(), | ||
/* can_unwind */ false, | ||
force_no_backtrace, | ||
); | ||
|
||
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. | ||
unsafe { panic_impl(&pi) } | ||
} | ||
|
||
// PanicInfo with the `can_unwind` flag set to false forces an abort. | ||
let pi = PanicInfo::internal_constructor( | ||
Some(&fmt), | ||
Location::caller(), | ||
/* can_unwind */ false, | ||
force_no_backtrace, | ||
); | ||
#[inline] | ||
#[track_caller] | ||
const fn comptime(fmt: fmt::Arguments<'_>, _force_no_backtrace: bool) -> ! { | ||
panic_fmt(fmt); | ||
} | ||
|
||
// SAFETY: `panic_impl` is defined in safe Rust code and thus is safe to call. | ||
unsafe { panic_impl(&pi) } | ||
// SAFETY: const panic does not care about unwinding | ||
unsafe { | ||
super::intrinsics::const_eval_select((fmt, force_no_backtrace), comptime, runtime); | ||
} | ||
} | ||
|
||
// Next we define a bunch of higher-level wrappers that all bottom out in the two core functions | ||
|
@@ -132,7 +147,8 @@ pub const fn panic(expr: &'static str) -> ! { | |
#[cfg_attr(feature = "panic_immediate_abort", inline)] | ||
#[lang = "panic_nounwind"] // needed by codegen for non-unwinding panics | ||
#[rustc_nounwind] | ||
pub fn panic_nounwind(expr: &'static str) -> ! { | ||
#[rustc_const_unstable(feature = "core_panic", issue = "none")] | ||
pub const fn panic_nounwind(expr: &'static str) -> ! { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the impact of adding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That will require codegen backend changes to add the implicit caller location argument when it is manually called (as opposed through a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's fine? Debug assertions are expected to have overhead, sometimes a lot of overhead. If people are concerned about debug assertions, they should turn them off. Or we should factor these assertions out to a separate flag. Either option is preferable to sacrificing UX for those who can afford the overhead, which as far as I can tell is most people? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The comment above the function (helpfully hidden by github) explains this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Callers that do not care about code size can just call |
||
panic_nounwind_fmt(fmt::Arguments::new_const(&[expr]), /* force_no_backtrace */ false); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This macro should have documentation that explains that it should be used in unsafe contexts because it cannot compromise unwind safety. (and that
debug_assert!
shouldn't be because it can)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, actually no unwind safety is compromised and
debug_assert!
can be used in these contexts, because if unwinding would only happen when there's an UB, and unwinding happening at undesirable place is an allowed behaviour for UB.That said, causing UB by unwinding certainly defeats the purpose of having these assertions for debugging.