Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8f1d47d

Browse files
committedNov 6, 2024·
Auto merge of rust-lang#132662 - RalfJung:const-panic-inlining, r=<try>
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.
2 parents 2796048 + 6e3a61b commit 8f1d47d

File tree

5 files changed

+16
-23
lines changed

5 files changed

+16
-23
lines changed
 

‎library/core/src/char/methods.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ const fn len_utf16(code: u32) -> usize {
17741774
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_char_encode_utf8", since = "1.83.0"))]
17751775
#[doc(hidden)]
17761776
#[inline]
1777+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
17771778
pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
17781779
let len = len_utf8(code);
17791780
match (len, &mut *dst) {
@@ -1826,6 +1827,7 @@ pub const fn encode_utf8_raw(code: u32, dst: &mut [u8]) -> &mut [u8] {
18261827
)]
18271828
#[doc(hidden)]
18281829
#[inline]
1830+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
18291831
pub const fn encode_utf16_raw(mut code: u32, dst: &mut [u16]) -> &mut [u16] {
18301832
let len = len_utf16(code);
18311833
match (len, &mut *dst) {

‎library/core/src/intrinsics.rs

-2
Original file line numberDiff line numberDiff line change
@@ -2966,13 +2966,11 @@ pub(crate) macro const_eval_select {
29662966
else
29672967
$(#[$runtime_attr:meta])* $runtime:block
29682968
) => {{
2969-
#[inline] // avoid the overhead of an extra fn call
29702969
$(#[$runtime_attr])*
29712970
fn runtime($($arg: $ty),*) $( -> $ret )? {
29722971
$runtime
29732972
}
29742973

2975-
#[inline] // prevent codegen on this function
29762974
$(#[$compiletime_attr])*
29772975
const fn compiletime($($arg: $ty),*) $( -> $ret )? {
29782976
// Don't warn if one of the arguments is unused.

‎library/core/src/num/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,7 @@ pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8])
14571457
#[cfg_attr(feature = "panic_immediate_abort", inline)]
14581458
#[cold]
14591459
#[track_caller]
1460+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
14601461
const fn from_str_radix_panic(radix: u32) -> ! {
14611462
const_panic!(
14621463
"from_str_radix_int: must lie in the range `[2, 36]`",

‎library/core/src/panic.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -201,27 +201,16 @@ pub unsafe trait PanicPayload: crate::fmt::Display {
201201
#[unstable(feature = "panic_internals", issue = "none")]
202202
#[doc(hidden)]
203203
pub macro const_panic {
204-
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {{
205-
// Wrap call to `const_eval_select` in a function so that we can
206-
// add the `rustc_allow_const_fn_unstable`. This is okay to do
207-
// because both variants will panic, just with different messages.
208-
#[rustc_allow_const_fn_unstable(const_eval_select)]
209-
#[inline(always)]
210-
#[track_caller]
211-
#[cfg_attr(bootstrap, rustc_const_stable(feature = "const_panic", since = "CURRENT_RUSTC_VERSION"))]
212-
const fn do_panic($($arg: $ty),*) -> ! {
213-
$crate::intrinsics::const_eval_select!(
214-
@capture { $($arg: $ty),* } -> !:
215-
if const #[track_caller] {
216-
$crate::panic!($const_msg)
217-
} else #[track_caller] {
218-
$crate::panic!($runtime_msg)
219-
}
220-
)
221-
}
222-
223-
do_panic($($val),*)
224-
}},
204+
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty = $val:expr),* $(,)?) => {
205+
$crate::intrinsics::const_eval_select!(
206+
@capture { $($arg: $ty = $val),* } -> !:
207+
if const #[track_caller] {
208+
$crate::panic!($const_msg)
209+
} else #[track_caller] {
210+
$crate::panic!($runtime_msg)
211+
}
212+
)
213+
},
225214
// We support leaving away the `val` expressions for *all* arguments
226215
// (but not for *some* arguments, that's too tricky).
227216
($const_msg:literal, $runtime_msg:literal, $($arg:ident : $ty:ty),* $(,)?) => {

‎library/core/src/slice/index.rs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ where
3131
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
3232
#[cfg_attr(feature = "panic_immediate_abort", inline)]
3333
#[track_caller]
34+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
3435
const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
3536
const_panic!(
3637
"slice start index is out of range for slice",
@@ -43,6 +44,7 @@ const fn slice_start_index_len_fail(index: usize, len: usize) -> ! {
4344
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
4445
#[cfg_attr(feature = "panic_immediate_abort", inline)]
4546
#[track_caller]
47+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
4648
const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
4749
const_panic!(
4850
"slice end index is out of range for slice",
@@ -55,6 +57,7 @@ const fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
5557
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
5658
#[cfg_attr(feature = "panic_immediate_abort", inline)]
5759
#[track_caller]
60+
#[rustc_allow_const_fn_unstable(const_eval_select)] // just used for panics
5861
const fn slice_index_order_fail(index: usize, end: usize) -> ! {
5962
const_panic!(
6063
"slice index start is larger than end",

0 commit comments

Comments
 (0)
Please sign in to comment.