Skip to content

Conversation

pnkfelix
Copy link
Contributor

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 the wrapping_neg method instead.

This needs to go into beta channel.

@rust-highfive
Copy link
Contributor

r? @eddyb

(rust_highfive has picked a reviewer for you, use r? to override)

@pnkfelix
Copy link
Contributor Author

r? @alexcrichton

@rust-highfive rust-highfive assigned alexcrichton and unassigned eddyb Apr 16, 2015
@pnkfelix
Copy link
Contributor Author

(oh, but I am going to give this a whirl on try...)

@pnkfelix
Copy link
Contributor Author

@bors try

@bors
Copy link
Collaborator

bors commented Apr 16, 2015

⌛ Trying commit f9c6780 with merge 2e51e09...

bors added a commit that referenced this pull request Apr 16, 2015
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.
@bors
Copy link
Collaborator

bors commented Apr 16, 2015

💔 Test failed - try-mac

@pnkfelix
Copy link
Contributor Author

(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.)

@@ -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()
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 is Int::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

@nikomatsakis
Copy link
Contributor

r+ we can settle abs() later

@pnkfelix
Copy link
Contributor Author

@bors r=nikomatsakis

@bors
Copy link
Collaborator

bors commented Apr 17, 2015

📌 Commit b8ec7e8 has been approved by nikomatsakis

@pnkfelix
Copy link
Contributor Author

@bors p=1

bors added a commit that referenced this pull request Apr 17, 2015
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.
@bors
Copy link
Collaborator

bors commented Apr 17, 2015

⌛ Testing commit b8ec7e8 with merge b08d6cf...

@bors
Copy link
Collaborator

bors commented Apr 18, 2015

@bors bors merged commit b8ec7e8 into rust-lang:master Apr 18, 2015
@pnkfelix pnkfelix added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Apr 21, 2015
@pnkfelix
Copy link
Contributor Author

going from nominated to (nominated, accepted)

@pnkfelix pnkfelix added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Apr 23, 2015
@alexcrichton alexcrichton removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Apr 23, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this pull request May 15, 2015
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
alexcrichton added a commit to alexcrichton/rust that referenced this pull request May 19, 2015
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
bors added a commit that referenced this pull request May 19, 2015
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
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Jun 10, 2015
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants