-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Check for overflow in arithmetic negation #24500
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
(oh, but I am going to give this a whirl on try...) |
@bors try |
Add conditional overflow-checking to signed negate operator. I argue this can land independently of #24420 , because one can write the implementation of `wrapped_neg()` inline if necessary (as illustrated in two cases on this PR). This needs to go into beta channel.
💔 Test failed - try-mac |
f9c6780
to
a8b1776
Compare
(I'm going to rebase so that I can make use of the methods that landed in #24420 ; after that's done, this should be ready to land.) |
a8b1776
to
5e7785c
Compare
@@ -1321,7 +1321,11 @@ macro_rules! int_impl { | |||
#[stable(feature = "rust1", since = "1.0.0")] | |||
#[inline] | |||
pub fn abs(self) -> $T { | |||
if self.is_negative() { -self } else { self } | |||
if self.is_negative() { | |||
self.wrapping_neg() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I guess this is assuming we want abs(INT_IN) to be INT_MIN? I kind of feel like I want it to be the original def'n -- but I guess this is a question for the RFC as much as anything. Still, given that -self
was apparently unchecked before, it feels like we can "change" the behavior here and (legitimately) call it a bug fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, pnkfelix points out the result for min-value is documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the docs that lie just out of reach of the diff say:
Int::min_value()
will be returned if the number isInt::min_value()
.
So I was just preserving that. (Plus I think there are tests that check it.)
But yeah, seems like a Q for rust-lang/rfcs#1017
r+ we can settle abs() later |
@bors r=nikomatsakis |
📌 Commit b8ec7e8 has been approved by |
@bors p=1 |
Add conditional overflow-checking to signed negate operator. I argue this can land independently of #24420 , because one can write the implementation of `wrapped_neg()` inline if necessary (as illustrated in two cases on this PR). This needs to go into beta channel.
⌛ Testing commit b8ec7e8 with merge b08d6cf... |
going from nominated to (nominated, accepted) |
Debug overflow checks for arithmetic negation landed in rust-lang#24500, at which time the `abs` method on signed integers was changed to using `wrapping_neg` to ensure that the function never panicked. This implied that `abs` of `INT_MIN` would return `INT_MIN`, another negative value. When this change was back-ported to beta, however, in rust-lang#24708, the `wrapping_neg` function had not yet been backported, so the implementation was changed in rust-lang#24785 to `!self + 1`. This change had the unintended side effect of enabling debug overflow checks for the `abs` function. Consequently, the current state of affairs is that the beta branch checks for overflow in debug mode for `abs` and the nightly branch does not. This commit alters the behavior of nightly to have `abs` always check for overflow in debug mode. This change is more consistent with the way the standard library treats overflow as well, and it is also not a breaking change as it's what the beta branch currently does (albeit if by accident). cc rust-lang#25378
Debug overflow checks for arithmetic negation landed in rust-lang#24500, at which time the `abs` method on signed integers was changed to using `wrapping_neg` to ensure that the function never panicked. This implied that `abs` of `INT_MIN` would return `INT_MIN`, another negative value. When this change was back-ported to beta, however, in rust-lang#24708, the `wrapping_neg` function had not yet been backported, so the implementation was changed in rust-lang#24785 to `!self + 1`. This change had the unintended side effect of enabling debug overflow checks for the `abs` function. Consequently, the current state of affairs is that the beta branch checks for overflow in debug mode for `abs` and the nightly branch does not. This commit alters the behavior of nightly to have `abs` always check for overflow in debug mode. This change is more consistent with the way the standard library treats overflow as well, and it is also not a breaking change as it's what the beta branch currently does (albeit if by accident). cc rust-lang#25378
Debug overflow checks for arithmetic negation landed in #24500, at which time the `abs` method on signed integers was changed to using `wrapping_neg` to ensure that the function never panicked. This implied that `abs` of `INT_MIN` would return `INT_MIN`, another negative value. When this change was back-ported to beta, however, in #24708, the `wrapping_neg` function had not yet been backported, so the implementation was changed in #24785 to `!self + 1`. This change had the unintended side effect of enabling debug overflow checks for the `abs` function. Consequently, the current state of affairs is that the beta branch checks for overflow in debug mode for `abs` and the nightly branch does not. This commit alters the behavior of nightly to have `abs` always check for overflow in debug mode. This change is more consistent with the way the standard library treats overflow as well, and it is also not a breaking change as it's what the beta branch currently does (albeit if by accident). cc #25378
Debug overflow checks for arithmetic negation landed in rust-lang#24500, at which time the `abs` method on signed integers was changed to using `wrapping_neg` to ensure that the function never panicked. This implied that `abs` of `INT_MIN` would return `INT_MIN`, another negative value. When this change was back-ported to beta, however, in rust-lang#24708, the `wrapping_neg` function had not yet been backported, so the implementation was changed in rust-lang#24785 to `!self + 1`. This change had the unintended side effect of enabling debug overflow checks for the `abs` function. Consequently, the current state of affairs is that the beta branch checks for overflow in debug mode for `abs` and the nightly branch does not. This commit alters the behavior of nightly to have `abs` always check for overflow in debug mode. This change is more consistent with the way the standard library treats overflow as well, and it is also not a breaking change as it's what the beta branch currently does (albeit if by accident). cc rust-lang#25378
Add conditional overflow-checking to signed negate operator.
I argue this can land independently of #24420 , because one can write the implementation of
wrapped_neg()
inline if necessary (as illustrated in two cases on this PR).[breaking-change]
If you are relying on the prior behavior of negate on
Integer::MIN_VALUE
, use thewrapping_neg
method instead.This needs to go into beta channel.