Skip to content

Commit 2ec6aeb

Browse files
authored
Rollup merge of #107961 - scottmcm:unify-ilog-panics, r=Mark-Simulacrum
Avoid copy-pasting the `ilog` panic string in a bunch of places I also ended up changing the implementations to `if let` because it doesn't work to ```rust self.checked_ilog2().unwrap_or_else(panic_for_nonpositive_argument) ``` due to the `!`. But as a bonus that meant I could remove the `rustc_allow_const_fn_unstable` too.
2 parents c0d1e32 + 404e9c5 commit 2ec6aeb

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

library/core/src/num/int_log10.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,11 @@ pub const fn i64(val: i64) -> u32 {
138138
pub const fn i128(val: i128) -> u32 {
139139
u128(val as u128)
140140
}
141+
142+
/// Instantiate this panic logic once, rather than for all the ilog methods
143+
/// on every single primitive type.
144+
#[cold]
145+
#[track_caller]
146+
pub const fn panic_for_nonpositive_argument() -> ! {
147+
panic!("argument of integer logarithm must be positive")
148+
}

library/core/src/num/int_macros.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,14 +2331,17 @@ macro_rules! int_impl {
23312331
/// ```
23322332
#[stable(feature = "int_log", since = "1.67.0")]
23332333
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2334-
#[rustc_allow_const_fn_unstable(const_option)]
23352334
#[must_use = "this returns the result of the operation, \
23362335
without modifying the original"]
23372336
#[inline]
23382337
#[track_caller]
23392338
pub const fn ilog(self, base: Self) -> u32 {
23402339
assert!(base >= 2, "base of integer logarithm must be at least 2");
2341-
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
2340+
if let Some(log) = self.checked_ilog(base) {
2341+
log
2342+
} else {
2343+
int_log10::panic_for_nonpositive_argument()
2344+
}
23422345
}
23432346

23442347
/// Returns the base 2 logarithm of the number, rounded down.
@@ -2354,13 +2357,16 @@ macro_rules! int_impl {
23542357
/// ```
23552358
#[stable(feature = "int_log", since = "1.67.0")]
23562359
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2357-
#[rustc_allow_const_fn_unstable(const_option)]
23582360
#[must_use = "this returns the result of the operation, \
23592361
without modifying the original"]
23602362
#[inline]
23612363
#[track_caller]
23622364
pub const fn ilog2(self) -> u32 {
2363-
self.checked_ilog2().expect("argument of integer logarithm must be positive")
2365+
if let Some(log) = self.checked_ilog2() {
2366+
log
2367+
} else {
2368+
int_log10::panic_for_nonpositive_argument()
2369+
}
23642370
}
23652371

23662372
/// Returns the base 10 logarithm of the number, rounded down.
@@ -2376,13 +2382,16 @@ macro_rules! int_impl {
23762382
/// ```
23772383
#[stable(feature = "int_log", since = "1.67.0")]
23782384
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
2379-
#[rustc_allow_const_fn_unstable(const_option)]
23802385
#[must_use = "this returns the result of the operation, \
23812386
without modifying the original"]
23822387
#[inline]
23832388
#[track_caller]
23842389
pub const fn ilog10(self) -> u32 {
2385-
self.checked_ilog10().expect("argument of integer logarithm must be positive")
2390+
if let Some(log) = self.checked_ilog10() {
2391+
log
2392+
} else {
2393+
int_log10::panic_for_nonpositive_argument()
2394+
}
23862395
}
23872396

23882397
/// Returns the logarithm of the number with respect to an arbitrary base,

library/core/src/num/uint_macros.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -705,14 +705,17 @@ macro_rules! uint_impl {
705705
/// ```
706706
#[stable(feature = "int_log", since = "1.67.0")]
707707
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
708-
#[rustc_allow_const_fn_unstable(const_option)]
709708
#[must_use = "this returns the result of the operation, \
710709
without modifying the original"]
711710
#[inline]
712711
#[track_caller]
713712
pub const fn ilog(self, base: Self) -> u32 {
714713
assert!(base >= 2, "base of integer logarithm must be at least 2");
715-
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
714+
if let Some(log) = self.checked_ilog(base) {
715+
log
716+
} else {
717+
int_log10::panic_for_nonpositive_argument()
718+
}
716719
}
717720

718721
/// Returns the base 2 logarithm of the number, rounded down.
@@ -728,13 +731,16 @@ macro_rules! uint_impl {
728731
/// ```
729732
#[stable(feature = "int_log", since = "1.67.0")]
730733
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
731-
#[rustc_allow_const_fn_unstable(const_option)]
732734
#[must_use = "this returns the result of the operation, \
733735
without modifying the original"]
734736
#[inline]
735737
#[track_caller]
736738
pub const fn ilog2(self) -> u32 {
737-
self.checked_ilog2().expect("argument of integer logarithm must be positive")
739+
if let Some(log) = self.checked_ilog2() {
740+
log
741+
} else {
742+
int_log10::panic_for_nonpositive_argument()
743+
}
738744
}
739745

740746
/// Returns the base 10 logarithm of the number, rounded down.
@@ -750,13 +756,16 @@ macro_rules! uint_impl {
750756
/// ```
751757
#[stable(feature = "int_log", since = "1.67.0")]
752758
#[rustc_const_stable(feature = "int_log", since = "1.67.0")]
753-
#[rustc_allow_const_fn_unstable(const_option)]
754759
#[must_use = "this returns the result of the operation, \
755760
without modifying the original"]
756761
#[inline]
757762
#[track_caller]
758763
pub const fn ilog10(self) -> u32 {
759-
self.checked_ilog10().expect("argument of integer logarithm must be positive")
764+
if let Some(log) = self.checked_ilog10() {
765+
log
766+
} else {
767+
int_log10::panic_for_nonpositive_argument()
768+
}
760769
}
761770

762771
/// Returns the logarithm of the number with respect to an arbitrary base,

0 commit comments

Comments
 (0)