Skip to content

Commit 69cafc0

Browse files
author
Lukas Markeffsky
committed
always panic for invalid integer logarithm
1 parent c2590e6 commit 69cafc0

File tree

2 files changed

+15
-73
lines changed

2 files changed

+15
-73
lines changed

library/core/src/num/int_macros.rs

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,9 +2279,8 @@ macro_rules! int_impl {
22792279
///
22802280
/// # Panics
22812281
///
2282-
/// When the number is negative, zero, or if the base is not at least 2; it
2283-
/// panics in debug mode and the return value is 0 in release
2284-
/// mode.
2282+
/// This function will panic if `self` is less than or equal to zero,
2283+
/// or if `base` is less then 2.
22852284
///
22862285
/// # Examples
22872286
///
@@ -2297,24 +2296,15 @@ macro_rules! int_impl {
22972296
#[rustc_inherit_overflow_checks]
22982297
#[allow(arithmetic_overflow)]
22992298
pub const fn ilog(self, base: Self) -> u32 {
2300-
match self.checked_ilog(base) {
2301-
Some(n) => n,
2302-
None => {
2303-
// In debug builds, trigger a panic on None.
2304-
// This should optimize completely out in release builds.
2305-
let _ = Self::MAX + 1;
2306-
2307-
0
2308-
},
2309-
}
2299+
assert!(base >= 2, "base of integer logarithm must be at least 2");
2300+
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
23102301
}
23112302

23122303
/// Returns the base 2 logarithm of the number, rounded down.
23132304
///
23142305
/// # Panics
23152306
///
2316-
/// When the number is negative or zero it panics in debug mode and the return value
2317-
/// is 0 in release mode.
2307+
/// This function will panic if `self` is less than or equal to zero.
23182308
///
23192309
/// # Examples
23202310
///
@@ -2330,24 +2320,14 @@ macro_rules! int_impl {
23302320
#[rustc_inherit_overflow_checks]
23312321
#[allow(arithmetic_overflow)]
23322322
pub const fn ilog2(self) -> u32 {
2333-
match self.checked_ilog2() {
2334-
Some(n) => n,
2335-
None => {
2336-
// In debug builds, trigger a panic on None.
2337-
// This should optimize completely out in release builds.
2338-
let _ = Self::MAX + 1;
2339-
2340-
0
2341-
},
2342-
}
2323+
self.checked_ilog2().expect("argument of integer logarithm must be positive")
23432324
}
23442325

23452326
/// Returns the base 10 logarithm of the number, rounded down.
23462327
///
23472328
/// # Panics
23482329
///
2349-
/// When the number is negative or zero it panics in debug mode and the return value
2350-
/// is 0 in release mode.
2330+
/// This function will panic if `self` is less than or equal to zero.
23512331
///
23522332
/// # Example
23532333
///
@@ -2363,16 +2343,7 @@ macro_rules! int_impl {
23632343
#[rustc_inherit_overflow_checks]
23642344
#[allow(arithmetic_overflow)]
23652345
pub const fn ilog10(self) -> u32 {
2366-
match self.checked_ilog10() {
2367-
Some(n) => n,
2368-
None => {
2369-
// In debug builds, trigger a panic on None.
2370-
// This should optimize completely out in release builds.
2371-
let _ = Self::MAX + 1;
2372-
2373-
0
2374-
},
2375-
}
2346+
self.checked_ilog10().expect("argument of integer logarithm must be positive")
23762347
}
23772348

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

library/core/src/num/uint_macros.rs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -692,8 +692,7 @@ macro_rules! uint_impl {
692692
///
693693
/// # Panics
694694
///
695-
/// When the number is zero, or if the base is not at least 2;
696-
/// it panics in debug mode and the return value is 0 in release mode.
695+
/// This function will panic if `self` is zero, or if `base` is less then 2.
697696
///
698697
/// # Examples
699698
///
@@ -709,24 +708,15 @@ macro_rules! uint_impl {
709708
#[rustc_inherit_overflow_checks]
710709
#[allow(arithmetic_overflow)]
711710
pub const fn ilog(self, base: Self) -> u32 {
712-
match self.checked_ilog(base) {
713-
Some(n) => n,
714-
None => {
715-
// In debug builds, trigger a panic on None.
716-
// This should optimize completely out in release builds.
717-
let _ = Self::MAX + 1;
718-
719-
0
720-
},
721-
}
711+
assert!(base >= 2, "base of integer logarithm must be at least 2");
712+
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
722713
}
723714

724715
/// Returns the base 2 logarithm of the number, rounded down.
725716
///
726717
/// # Panics
727718
///
728-
/// When the number is zero it panics in debug mode and
729-
/// the return value is 0 in release mode.
719+
/// This function will panic if `self` is zero.
730720
///
731721
/// # Examples
732722
///
@@ -742,24 +732,14 @@ macro_rules! uint_impl {
742732
#[rustc_inherit_overflow_checks]
743733
#[allow(arithmetic_overflow)]
744734
pub const fn ilog2(self) -> u32 {
745-
match self.checked_ilog2() {
746-
Some(n) => n,
747-
None => {
748-
// In debug builds, trigger a panic on None.
749-
// This should optimize completely out in release builds.
750-
let _ = Self::MAX + 1;
751-
752-
0
753-
},
754-
}
735+
self.checked_ilog2().expect("argument of integer logarithm must be positive")
755736
}
756737

757738
/// Returns the base 10 logarithm of the number, rounded down.
758739
///
759740
/// # Panics
760741
///
761-
/// When the number is zero it panics in debug mode and the
762-
/// return value is 0 in release mode.
742+
/// This function will panic if `self` is zero.
763743
///
764744
/// # Example
765745
///
@@ -775,16 +755,7 @@ macro_rules! uint_impl {
775755
#[rustc_inherit_overflow_checks]
776756
#[allow(arithmetic_overflow)]
777757
pub const fn ilog10(self) -> u32 {
778-
match self.checked_ilog10() {
779-
Some(n) => n,
780-
None => {
781-
// In debug builds, trigger a panic on None.
782-
// This should optimize completely out in release builds.
783-
let _ = Self::MAX + 1;
784-
785-
0
786-
},
787-
}
758+
self.checked_ilog10().expect("argument of integer logarithm must be positive")
788759
}
789760

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

0 commit comments

Comments
 (0)