always panic for invalid integer logarithm

This commit is contained in:
Lukas Markeffsky 2022-10-02 14:24:56 +02:00
parent c2590e6e89
commit 69cafc0699
2 changed files with 15 additions and 73 deletions

View File

@ -2279,9 +2279,8 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
///
/// # Panics
///
/// When the number is negative, zero, or if the base is not at least 2; it
/// panics in debug mode and the return value is 0 in release
/// mode.
/// This function will panic if `self` is less than or equal to zero,
/// or if `base` is less then 2.
///
/// # Examples
///
@ -2297,24 +2296,15 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
assert!(base >= 2, "base of integer logarithm must be at least 2");
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
}
/// Returns the base 2 logarithm of the number, rounded down.
///
/// # Panics
///
/// When the number is negative or zero it panics in debug mode and the return value
/// is 0 in release mode.
/// This function will panic if `self` is less than or equal to zero.
///
/// # Examples
///
@ -2330,24 +2320,14 @@ pub const fn ilog(self, base: Self) -> u32 {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
self.checked_ilog2().expect("argument of integer logarithm must be positive")
}
/// Returns the base 10 logarithm of the number, rounded down.
///
/// # Panics
///
/// When the number is negative or zero it panics in debug mode and the return value
/// is 0 in release mode.
/// This function will panic if `self` is less than or equal to zero.
///
/// # Example
///
@ -2363,16 +2343,7 @@ pub const fn ilog2(self) -> u32 {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
self.checked_ilog10().expect("argument of integer logarithm must be positive")
}
/// Returns the logarithm of the number with respect to an arbitrary base,

View File

@ -692,8 +692,7 @@ pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
///
/// # Panics
///
/// When the number is zero, or if the base is not at least 2;
/// it panics in debug mode and the return value is 0 in release mode.
/// This function will panic if `self` is zero, or if `base` is less then 2.
///
/// # Examples
///
@ -709,24 +708,15 @@ pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
assert!(base >= 2, "base of integer logarithm must be at least 2");
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
}
/// Returns the base 2 logarithm of the number, rounded down.
///
/// # Panics
///
/// When the number is zero it panics in debug mode and
/// the return value is 0 in release mode.
/// This function will panic if `self` is zero.
///
/// # Examples
///
@ -742,24 +732,14 @@ pub const fn ilog(self, base: Self) -> u32 {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
self.checked_ilog2().expect("argument of integer logarithm must be positive")
}
/// Returns the base 10 logarithm of the number, rounded down.
///
/// # Panics
///
/// When the number is zero it panics in debug mode and the
/// return value is 0 in release mode.
/// This function will panic if `self` is zero.
///
/// # Example
///
@ -775,16 +755,7 @@ pub const fn ilog2(self) -> u32 {
#[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)]
pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() {
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
self.checked_ilog10().expect("argument of integer logarithm must be positive")
}
/// Returns the logarithm of the number with respect to an arbitrary base,