Skip to content

Commit b458550

Browse files
committed
Document panicking cases for integer remainder
The panic when the right operand is `0` is expected, but the overflow-related panic (which occurs only for `MIN % -1`) is somewhat surprising for two reasons: a return value of `0` would be reasonable in this case, and, for most arithmetic operations, overflow results in panic only when `debug_assertions` is enabled. As a result, it's helpful to document this behavior.
1 parent 9278b4c commit b458550

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

library/core/src/ops/arith.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,13 @@ pub trait Rem<Rhs = Self> {
556556
}
557557

558558
macro_rules! rem_impl_integer {
559-
($($t:ty)*) => ($(
559+
($(($($t:ty)*) => $panic:expr),*) => ($($(
560560
/// This operation satisfies `n % d == n - (n / d) * d`. The
561561
/// result has the same sign as the left operand.
562+
///
563+
/// # Panics
564+
///
565+
#[doc = $panic]
562566
#[stable(feature = "rust1", since = "1.0.0")]
563567
impl Rem for $t {
564568
type Output = $t;
@@ -568,10 +572,13 @@ macro_rules! rem_impl_integer {
568572
}
569573

570574
forward_ref_binop! { impl Rem, rem for $t, $t }
571-
)*)
575+
)*)*)
572576
}
573577

574-
rem_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
578+
rem_impl_integer! {
579+
(usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
580+
(isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or if `self / other` results in overflow."
581+
}
575582

576583
macro_rules! rem_impl_float {
577584
($($t:ty)*) => ($(

0 commit comments

Comments
 (0)