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

Implement Neg for signed non-zero integers. #102341

Merged
merged 1 commit into from
Apr 20, 2023

Conversation

jmillikin
Copy link
Contributor

Negating a non-zero integer currently requires unpacking to a primitive and re-wrapping. Since negation of non-zero signed integers always produces a non-zero result, it is safe to implement Neg for NonZeroI{N}.

The new impl is marked as stable because trait impls for two stable types can't be marked unstable.

See discussion on rust-lang/libs-team#105 for additional context.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 27, 2022
@rustbot

This comment was marked as resolved.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 27, 2022
@jmillikin
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 27, 2022
@scottmcm scottmcm added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Sep 30, 2022

assert_eq!((-NonZeroI128::new(1).unwrap()).get(), -1);
assert_eq!((-NonZeroI128::new(-1).unwrap()).get(), 1);
}
Copy link
Member

Choose a reason for hiding this comment

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

Please also add a test for overflow behavior: what happens if you negate the smallest possible value (e.g. NonZeroI64::new(i64::MIN))?

Copy link
Contributor Author

@jmillikin jmillikin Oct 3, 2022

Choose a reason for hiding this comment

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

Done.

The test asserts that for MIN and MAX, the behavior on overflow is the same as for the underlying primitve.

Edit: per discussion, replaced with a UI test to verify correct panic message on overflow.

@joshtriplett joshtriplett added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 3, 2022
@jmillikin
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 3, 2022
@joshtriplett
Copy link
Member

That test is going to fail when overflow panics.

I think you need to add a copy of src/test/ui/numbers-arithmetic/overflowing-neg.rs for a NonZero type, instead.

@jmillikin jmillikin force-pushed the nonzero-impl-neg branch 2 times, most recently from a7bedfc to 60cf899 Compare October 3, 2022 13:05
@jmillikin
Copy link
Contributor Author

That test is going to fail when overflow panics.

I think you need to add a copy of src/test/ui/numbers-arithmetic/overflowing-neg.rs for a NonZero type, instead.

Ah, that's perfect, thanks! I got notified of the failing test, but was having trouble getting it to test with debug assertions locally.

@jmillikin
Copy link
Contributor Author

Gentle ping -- is the current state good enough to start the FCP, or is further work needed?

@jmillikin
Copy link
Contributor Author

What's the next step for me here? I assume there needs to be an FCP, but since there's no unstable version to merge I'm not sure what the trigger would be.

@jmillikin
Copy link
Contributor Author

@joshtriplett Sorry for the direct ping, but could you please let me know what I should be doing to move this PR along?

@fee1-dead
Copy link
Member

fee1-dead commented Feb 19, 2023

r? libs-api

(Also, there is a FIXME in core/src/num/nonzero.rs that should be removed if signed non-zero integers implement Neg)

@jmillikin
Copy link
Contributor Author

@fee1-dead Thanks for pointing out the FIXME. I've updated my change to resolve it.

@jmillikin
Copy link
Contributor Author

Can someone with appropriate permissions please push the merge button for this PR?

@dtolnay dtolnay assigned dtolnay and unassigned m-ou-se Apr 20, 2023
@dtolnay
Copy link
Member

dtolnay commented Apr 20, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Apr 20, 2023

📌 Commit 7e8b265a810090e29ccc4cd141949362a5680d98 has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 20, 2023
@bors
Copy link
Contributor

bors commented Apr 20, 2023

⌛ Testing commit 7e8b265a810090e29ccc4cd141949362a5680d98 with merge 797a2c85e5aee048fd3aae9a0213686f182f26aa...

@bors
Copy link
Contributor

bors commented Apr 20, 2023

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 20, 2023
@rust-log-analyzer

This comment has been minimized.

Negating a non-zero integer currently requires unpacking to a
primitive and re-wrapping. Since negation of non-zero signed
integers always produces a non-zero result, it is safe to
implement `Neg` for `NonZeroI{N}`.

The new `impl` is marked as stable because trait implementations
for two stable types can't be marked unstable.
@jmillikin
Copy link
Contributor Author

There was a conflict with PR #110393 (removing const traits from libcore), merged 14 hours ago. I've rebased to current master HEAD.

@workingjubilee
Copy link
Member

@bors r=dtolnay

@bors
Copy link
Contributor

bors commented Apr 20, 2023

📌 Commit 4e2797d has been approved by dtolnay

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 20, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 20, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#102341 (Implement `Neg` for signed non-zero integers.)
 - rust-lang#110424 (Spelling misc)
 - rust-lang#110448 (cmp doc examples improvements)
 - rust-lang#110516 (bootstrap: Update linux-raw-sys to 0.3.2)
 - rust-lang#110548 (Make `impl Debug for Span` not panic on not having session globals.)
 - rust-lang#110554 (`deny(unsafe_op_in_unsafe_fn)` in `rustc_data_structures`)
 - rust-lang#110575 (fix lint regression in `non_upper_case_globals`)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit df3aca2 into rust-lang:master Apr 20, 2023
@rustbot rustbot added this to the 1.71.0 milestone Apr 20, 2023
@jmillikin jmillikin deleted the nonzero-impl-neg branch April 20, 2023 12:49
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.