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

rewrite checked_{div,rem} to no contain any reference to panics #37029

Merged
merged 1 commit into from
Oct 8, 2016
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
14 changes: 6 additions & 8 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,10 @@ macro_rules! int_impl {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn checked_div(self, other: Self) -> Option<Self> {
if other == 0 {
if other == 0 || (self == Self::min_value() && other == -1) {
None
} else {
let (a, b) = self.overflowing_div(other);
if b {None} else {Some(a)}
Some(unsafe { intrinsics::unchecked_div(self, other) })
Copy link
Member

@nagisa nagisa Oct 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why intrinsic, not wrapping_div?

EDIT: wrapping_div also has references to panic. I was thinking it used unchecked_div o.O.

}
}

Expand All @@ -541,11 +540,10 @@ macro_rules! int_impl {
#[stable(feature = "wrapping", since = "1.7.0")]
#[inline]
pub fn checked_rem(self, other: Self) -> Option<Self> {
if other == 0 {
if other == 0 || (self == Self::min_value() && other == -1) {
None
} else {
let (a, b) = self.overflowing_rem(other);
if b {None} else {Some(a)}
Some(unsafe { intrinsics::unchecked_rem(self, other) })
}
}

Expand Down Expand Up @@ -1688,7 +1686,7 @@ macro_rules! uint_impl {
pub fn checked_div(self, other: Self) -> Option<Self> {
match other {
0 => None,
other => Some(self / other),
other => Some(unsafe { intrinsics::unchecked_div(self, other) }),
}
}

Expand All @@ -1709,7 +1707,7 @@ macro_rules! uint_impl {
if other == 0 {
None
} else {
Some(self % other)
Some(unsafe { intrinsics::unchecked_rem(self, other) })
}
}

Expand Down