Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document panicking cases for integer division and remainder #82683

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions library/core/src/ops/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,13 @@ pub trait Div<Rhs = Self> {
}

macro_rules! div_impl_integer {
($($t:ty)*) => ($(
($(($($t:ty)*) => $panic:expr),*) => ($($(
/// This operation rounds towards zero, truncating any
/// fractional part of the exact result.
///
/// # Panics
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
type Output = $t;
Expand All @@ -468,10 +472,13 @@ macro_rules! div_impl_integer {
}

forward_ref_binop! { impl Div, div for $t, $t }
)*)
)*)*)
}

div_impl_integer! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
div_impl_integer! {
(usize u8 u16 u32 u64 u128) => "This operation will panic if `other == 0`.",
(isize i8 i16 i32 i64 i128) => "This operation will panic if `other == 0` or the division results in overflow."
}

macro_rules! div_impl_float {
($($t:ty)*) => ($(
Expand Down Expand Up @@ -549,9 +556,13 @@ pub trait Rem<Rhs = Self> {
}

macro_rules! rem_impl_integer {
($($t:ty)*) => ($(
($(($($t:ty)*) => $panic:expr),*) => ($($(
/// This operation satisfies `n % d == n - (n / d) * d`. The
/// result has the same sign as the left operand.
///
/// # Panics
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Rem for $t {
type Output = $t;
Expand All @@ -561,10 +572,13 @@ macro_rules! rem_impl_integer {
}

forward_ref_binop! { impl Rem, rem for $t, $t }
)*)
)*)*)
}

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

macro_rules! rem_impl_float {
($($t:ty)*) => ($(
Expand Down