Skip to content

Commit

Permalink
Auto merge of rust-lang#132662 - RalfJung:const-panic-inlining, r=<try>
Browse files Browse the repository at this point in the history
tweak attributes for const panic macro

Let's do some random mutations of this macro to see if that can re-gain the perf lost in rust-lang#132542.
  • Loading branch information
bors committed Nov 6, 2024
2 parents cf2b370 + 05178de commit a809d61
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 21 deletions.
2 changes: 2 additions & 0 deletions library/core/src/char/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,7 @@ const fn len_utf16(code: u32) -> usize {
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_char_encode_utf8", since = "1.83.0"))]
#[doc(hidden)]
#[inline]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
let len = len_utf8(code);
match (len, &mut *dst) {
Expand Down Expand Up @@ -1826,6 +1827,7 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
)]
#[doc(hidden)]
#[inline]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
pub const fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
let len = len_utf16(code);
match (len, &mut *dst) {
Expand Down
1 change: 1 addition & 0 deletions library/core/src/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,7 @@ pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8])
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[cold]
#[track_caller]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
const fn from_str_radix_panic(radix: u32) -> ! {
const_panic!(
"from_str_radix_int: must lie in the range `[2, 36]`",
Expand Down
31 changes: 10 additions & 21 deletions library/core/src/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,16 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
#[unstable(feature = "panic_internals", issue = "none")]
#[doc(hidden)]
pub macro const_panic {
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
// Wrap call to `const_eval_select` in a function so that we can
// add the `rustc_allow_const_fn_unstable`. This is okay to do
// because both variants will panic, just with different messages.
#[rustc_allow_const_fn_unstable(const_eval_select)]
#[inline(always)]
#[track_caller]
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
const fn do_panic($($arg: $ty),*) -> ! {
$crate::intrinsics::const_eval_select!(
@capture { $($arg: $ty),* } -> !:
if const #[track_caller] {
$crate::panic!($const_msg)
} else #[track_caller] {
$crate::panic!($runtime_msg)
}
)
}

do_panic($($val),*)
}},
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {
$crate::intrinsics::const_eval_select!(
@capture { $($arg: $ty = $val),* } -> !:
if const #[track_caller] {
$crate::panic!($const_msg)
} else #[track_caller] {
$crate::panic!($runtime_msg)
}
)
},
// We support leaving away the `val` expressions for *all* arguments
// (but not for *some* arguments, that's too tricky).
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {
Expand Down
3 changes: 3 additions & 0 deletions library/core/src/slice/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ where
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[track_caller]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
const_panic!(
"slice start index is out of range for slice",
Expand All @@ -43,6 +44,7 @@ const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[track_caller]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
const_panic!(
"slice end index is out of range for slice",
Expand All @@ -55,6 +57,7 @@ const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
#[cfg_attr(feature = "panic_immediate_abort", inline)]
#[track_caller]
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
const fn slice_index_order_fail(index: usize, end: usize) -> ! {
const_panic!(
"slice index start is larger than end",
Expand Down

0 comments on commit a809d61

Please sign in to comment.