diff options
| author | bors <bors@rust-lang.org> | 2024-11-03 16:20:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-11-03 16:20:51 +0000 |
| commit | e3a918ece026cec748fc64af5b4983095b46097e (patch) | |
| tree | 342db9dd6cf4d8cf8a1d4a79c6df4d8dea178e46 /library/core/src/num/mod.rs | |
| parent | 7028d9318fadc20e5e3058d52e44d785d31a6aaa (diff) | |
| parent | bc757f9034c75a0e812c637f97432b222a150781 (diff) | |
| download | rust-e3a918ece026cec748fc64af5b4983095b46097e.tar.gz rust-e3a918ece026cec748fc64af5b4983095b46097e.zip | |
Auto merge of #132542 - RalfJung:const_panic, r=tgross35
add const_panic macro to make it easier to fall back to non-formatting panic in const Suggested by `@tgross35` r? `@tgross35`
Diffstat (limited to 'library/core/src/num/mod.rs')
| -rw-r--r-- | library/core/src/num/mod.rs | 21 |
1 files changed, 7 insertions, 14 deletions
diff --git a/library/core/src/num/mod.rs b/library/core/src/num/mod.rs index 9e0af8b2ace..f4930ca5c7d 100644 --- a/library/core/src/num/mod.rs +++ b/library/core/src/num/mod.rs @@ -2,6 +2,7 @@ #![stable(feature = "rust1", since = "1.0.0")] +use crate::macros::const_panic; use crate::str::FromStr; use crate::ub_checks::assert_unsafe_precondition; use crate::{ascii, intrinsics, mem}; @@ -1452,24 +1453,16 @@ pub const fn can_not_overflow<T>(radix: u32, is_signed_ty: bool, digits: &[u8]) radix <= 16 && digits.len() <= mem::size_of::<T>() * 2 - is_signed_ty as usize } -#[track_caller] -const fn from_str_radix_panic_ct(_radix: u32) -> ! { - panic!("from_str_radix_int: must lie in the range `[2, 36]`"); -} - -#[track_caller] -fn from_str_radix_panic_rt(radix: u32) -> ! { - panic!("from_str_radix_int: must lie in the range `[2, 36]` - found {}", radix); -} - #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))] #[cfg_attr(feature = "panic_immediate_abort", inline)] #[cold] #[track_caller] -#[rustc_allow_const_fn_unstable(const_eval_select)] -const fn from_str_radix_panic(radix: u32) { - // The only difference between these two functions is their panic message. - intrinsics::const_eval_select((radix,), from_str_radix_panic_ct, from_str_radix_panic_rt); +const fn from_str_radix_panic(radix: u32) -> ! { + const_panic!( + "from_str_radix_int: must lie in the range `[2, 36]`", + "from_str_radix_int: must lie in the range `[2, 36]` - found {radix}", + radix: u32 = radix, + ) } macro_rules! from_str_radix { |
