Skip to content

Commit d8091f8

Browse files
authored
Rollup merge of rust-lang#102578 - lukas-code:ilog-panic, r=m-ou-se
Panic for invalid arguments of `{integer primitive}::ilog{,2,10}` in all modes Decision made in rust-lang#100422 (comment) resolves rust-lang#100422 tracking issue: rust-lang#70887 r? `@m-ou-se`
2 parents f8723f9 + b7dae8a commit d8091f8

File tree

3 files changed

+45
-85
lines changed

3 files changed

+45
-85
lines changed

library/core/src/num/int_macros.rs

+8-43
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
///
@@ -2294,27 +2293,16 @@ macro_rules! int_impl {
22942293
without modifying the original"]
22952294
#[inline]
22962295
#[track_caller]
2297-
#[rustc_inherit_overflow_checks]
2298-
#[allow(arithmetic_overflow)]
22992296
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-
}
2297+
assert!(base >= 2, "base of integer logarithm must be at least 2");
2298+
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
23102299
}
23112300

23122301
/// Returns the base 2 logarithm of the number, rounded down.
23132302
///
23142303
/// # Panics
23152304
///
2316-
/// When the number is negative or zero it panics in debug mode and the return value
2317-
/// is 0 in release mode.
2305+
/// This function will panic if `self` is less than or equal to zero.
23182306
///
23192307
/// # Examples
23202308
///
@@ -2327,27 +2315,15 @@ macro_rules! int_impl {
23272315
without modifying the original"]
23282316
#[inline]
23292317
#[track_caller]
2330-
#[rustc_inherit_overflow_checks]
2331-
#[allow(arithmetic_overflow)]
23322318
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-
}
2319+
self.checked_ilog2().expect("argument of integer logarithm must be positive")
23432320
}
23442321

23452322
/// Returns the base 10 logarithm of the number, rounded down.
23462323
///
23472324
/// # Panics
23482325
///
2349-
/// When the number is negative or zero it panics in debug mode and the return value
2350-
/// is 0 in release mode.
2326+
/// This function will panic if `self` is less than or equal to zero.
23512327
///
23522328
/// # Example
23532329
///
@@ -2360,19 +2336,8 @@ macro_rules! int_impl {
23602336
without modifying the original"]
23612337
#[inline]
23622338
#[track_caller]
2363-
#[rustc_inherit_overflow_checks]
2364-
#[allow(arithmetic_overflow)]
23652339
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-
}
2340+
self.checked_ilog10().expect("argument of integer logarithm must be positive")
23762341
}
23772342

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

library/core/src/num/uint_macros.rs

+7-42
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
///
@@ -706,27 +705,16 @@ macro_rules! uint_impl {
706705
without modifying the original"]
707706
#[inline]
708707
#[track_caller]
709-
#[rustc_inherit_overflow_checks]
710-
#[allow(arithmetic_overflow)]
711708
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-
}
709+
assert!(base >= 2, "base of integer logarithm must be at least 2");
710+
self.checked_ilog(base).expect("argument of integer logarithm must be positive")
722711
}
723712

724713
/// Returns the base 2 logarithm of the number, rounded down.
725714
///
726715
/// # Panics
727716
///
728-
/// When the number is zero it panics in debug mode and
729-
/// the return value is 0 in release mode.
717+
/// This function will panic if `self` is zero.
730718
///
731719
/// # Examples
732720
///
@@ -739,27 +727,15 @@ macro_rules! uint_impl {
739727
without modifying the original"]
740728
#[inline]
741729
#[track_caller]
742-
#[rustc_inherit_overflow_checks]
743-
#[allow(arithmetic_overflow)]
744730
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-
}
731+
self.checked_ilog2().expect("argument of integer logarithm must be positive")
755732
}
756733

757734
/// Returns the base 10 logarithm of the number, rounded down.
758735
///
759736
/// # Panics
760737
///
761-
/// When the number is zero it panics in debug mode and the
762-
/// return value is 0 in release mode.
738+
/// This function will panic if `self` is zero.
763739
///
764740
/// # Example
765741
///
@@ -772,19 +748,8 @@ macro_rules! uint_impl {
772748
without modifying the original"]
773749
#[inline]
774750
#[track_caller]
775-
#[rustc_inherit_overflow_checks]
776-
#[allow(arithmetic_overflow)]
777751
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-
}
752+
self.checked_ilog10().expect("argument of integer logarithm must be positive")
788753
}
789754

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

library/core/tests/num/int_log.rs

+30
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,33 @@ fn ilog10_u64() {
164164
fn ilog10_u128() {
165165
ilog10_loop! { u128, 38 }
166166
}
167+
168+
#[test]
169+
#[should_panic(expected = "argument of integer logarithm must be positive")]
170+
fn ilog2_of_0_panic() {
171+
let _ = 0u32.ilog2();
172+
}
173+
174+
#[test]
175+
#[should_panic(expected = "argument of integer logarithm must be positive")]
176+
fn ilog10_of_0_panic() {
177+
let _ = 0u32.ilog10();
178+
}
179+
180+
#[test]
181+
#[should_panic(expected = "argument of integer logarithm must be positive")]
182+
fn ilog3_of_0_panic() {
183+
let _ = 0u32.ilog(3);
184+
}
185+
186+
#[test]
187+
#[should_panic(expected = "base of integer logarithm must be at least 2")]
188+
fn ilog0_of_1_panic() {
189+
let _ = 1u32.ilog(0);
190+
}
191+
192+
#[test]
193+
#[should_panic(expected = "base of integer logarithm must be at least 2")]
194+
fn ilog1_of_1_panic() {
195+
let _ = 1u32.ilog(1);
196+
}

0 commit comments

Comments
 (0)