-
Couldn't load subscription status.
- Fork 13.9k
Add panic_unreachable_unchecked feature flag to the standard library
#139196
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -53,6 +53,10 @@ const _: () = assert!(cfg!(panic = "abort"), "panic_immediate_abort requires -C | |||||
| #[rustc_do_not_const_check] // hooked by const-eval | ||||||
| #[rustc_const_stable_indirect] // must follow stable const rules since it is exposed to stable | ||||||
| pub const fn panic_fmt(fmt: fmt::Arguments<'_>) -> ! { | ||||||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||||||
| // SAFETY: it's not... | ||||||
|
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 safety comment is inadequate, the condition of soundness relies on the programmer asserting panics will never happen, so a more accurate comment is:
Suggested change
if you want to do april fools, go all in :^) |
||||||
| unsafe { super::intrinsics::unreachable() } | ||||||
| } | ||||||
| if cfg!(feature = "panic_immediate_abort") { | ||||||
| super::intrinsics::abort() | ||||||
| } | ||||||
|
|
@@ -94,6 +98,10 @@ pub const fn panic_nounwind_fmt(fmt: fmt::Arguments<'_>, force_no_backtrace: boo | |||||
| // We don't unwind anyway at compile-time so we can call the regular `panic_fmt`. | ||||||
| panic_fmt(fmt) | ||||||
| } else #[track_caller] { | ||||||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||||||
| // SAFETY: it's not... | ||||||
| unsafe { super::intrinsics::unreachable() } | ||||||
| } | ||||||
| if cfg!(feature = "panic_immediate_abort") { | ||||||
| super::intrinsics::abort() | ||||||
| } | ||||||
|
|
@@ -266,6 +274,10 @@ pub const fn panic_display<T: fmt::Display>(x: &T) -> ! { | |||||
| #[track_caller] | ||||||
| #[lang = "panic_bounds_check"] // needed by codegen for panic on OOB array/slice access | ||||||
| fn panic_bounds_check(index: usize, len: usize) -> ! { | ||||||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||||||
| // SAFETY: it's not... | ||||||
| unsafe { super::intrinsics::unreachable() } | ||||||
| } | ||||||
| if cfg!(feature = "panic_immediate_abort") { | ||||||
| super::intrinsics::abort() | ||||||
| } | ||||||
|
|
@@ -279,6 +291,10 @@ fn panic_bounds_check(index: usize, len: usize) -> ! { | |||||
| #[lang = "panic_misaligned_pointer_dereference"] // needed by codegen for panic on misaligned pointer deref | ||||||
| #[rustc_nounwind] // `CheckAlignment` MIR pass requires this function to never unwind | ||||||
| fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! { | ||||||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||||||
| // SAFETY: it's not... | ||||||
| unsafe { super::intrinsics::unreachable() } | ||||||
| } | ||||||
| if cfg!(feature = "panic_immediate_abort") { | ||||||
| super::intrinsics::abort() | ||||||
| } | ||||||
|
|
@@ -297,6 +313,10 @@ fn panic_misaligned_pointer_dereference(required: usize, found: usize) -> ! { | |||||
| #[lang = "panic_null_pointer_dereference"] // needed by codegen for panic on null pointer deref | ||||||
| #[rustc_nounwind] // `CheckNull` MIR pass requires this function to never unwind | ||||||
| fn panic_null_pointer_dereference() -> ! { | ||||||
| if cfg!(feature = "panic_unreachable_unchecked") { | ||||||
| // SAFETY: it's not... | ||||||
| unsafe { super::intrinsics::unreachable() } | ||||||
| } | ||||||
| if cfg!(feature = "panic_immediate_abort") { | ||||||
| super::intrinsics::abort() | ||||||
| } | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I propose to rename this feature to
unsafe_panic_unreachable_unchecked, so that its name includes wordunsafe, because this is actually an unsafe API (like#[no_mangle]attribute), and AFAIU all unsafe APIs tend to includeunsafein their usage (as part of their name, as a surroundingunsafeblock, etc).Alternatively, if this PR is a joke, I'd consider
crabs_never_panicas an alternative name.