Skip to content

core: Make funnel shifts panic only if overflow checks are enabled - #161204

Open
tgross35 wants to merge 2 commits into
rust-lang:mainfrom
tgross35:funnel-shift-panic
Open

core: Make funnel shifts panic only if overflow checks are enabled#161204
tgross35 wants to merge 2 commits into
rust-lang:mainfrom
tgross35:funnel-shift-panic

Conversation

@tgross35

Copy link
Copy Markdown
Member

Change from the existing behavior of panicking unconditionally to panicking only if overflow checks are enabled, wrapping otherwise. This is more consistent with other operations, and gives slightly better codegen https://rust.godbolt.org/z/Yz81zhbno.

Suggested in the stabilization PR at #161015 (comment).

The first commit adds some simple tests for overflow behavior that I couldn't find existing anywhere else.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 16, 2026
@rustbot

rustbot commented Aug 16, 2026

Copy link
Copy Markdown
Collaborator

r? @nia-e

rustbot has assigned @nia-e.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, nia-e

@tgross35

Copy link
Copy Markdown
Member Author

See also discussion at the stabilization PR #161015. This could wait for team feedback but I expect it to be reasonably unobjectionable.

I wasn't able to find a test that verifies different behavior based on
whether or not overflow checks are available, so add one here.
@tgross35
tgross35 force-pushed the funnel-shift-panic branch from 9cbfa6f to fc370f6 Compare August 17, 2026 00:03
@pthariensflame

Copy link
Copy Markdown
Contributor

Depending on the decision taken about this, we may add strict_funnel_{shl,shr} to #161119 and rust-lang/libs-team#855 to express the always-panicking semantics.

Comment thread library/core/src/num/uint_macros.rs Outdated
///
/// ## Overflow behavior
///
/// The above panic happens only if overflow checks are enabled (default in debug mode).

@RalfJung RalfJung Aug 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it is quite confusing to first say "panics if X" and then in the next section say "actually only sometimes".

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was mirroring what we have elsewhere, but I agree it's not ideal. I'll drop the separate section.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Does the latest look better to you? I guess others have a # Panics section with only ## Overflow behavior in it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So now we have a "panics" section that entirely consists of an "overflow behavior" subsection? Do we have that anywhere else? It looks a bit strange IMO.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, this is what is done at https://doc.rust-lang.org/std/primitive.u32.html#panics-2 and others. I don't love it either but I guess it easy to find things toggled by overflow checks by searching "overflow behavior".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That does look odd indeed. 🤷

@nia-e

nia-e commented Aug 17, 2026

Copy link
Copy Markdown
Member

This looks good impl-wise ^^ my only comment would be that since the wrapping methods were never added even though the ACP mentions them & this PR already has code for wrapping semantics, I'd be happy to review a follow-up PR adding those as well. r=me once the docs are fixed up per the above

@bors rollup

@pthariensflame

Copy link
Copy Markdown
Contributor

This looks good impl-wise ^^ my only comment would be that since the wrapping methods were never added even though the ACP mentions them & this PR already has code for wrapping semantics, I'd be happy to review a follow-up PR adding those as well. r=me once the docs are fixed up per the above

@nia-e We have that followup PR already at #161119, which we will rebase/update once this merges.

Change from the existing behavior of panicking unconditionally to
panicking only if debug assertions are enabled, wrapping otherwise. This
is more consistent with other operations, and gives slightly better
codegen [1].

Suggested in the stabilization PR, RUST-161015.

[1]: https://rust.godbolt.org/z/Yz81zhbno
@tgross35
tgross35 force-pushed the funnel-shift-panic branch from fc370f6 to 988ecca Compare August 20, 2026 07:06
@tgross35

Copy link
Copy Markdown
Member Author

I suppose with the update it's at least consistent, though there's probably room to improve these sections of the docs across the board.

@bors r=nia-e

@rust-bors

rust-bors Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 988ecca has been approved by nia-e

It is now in the queue for this repository.

🌲 The tree is currently closed for pull requests below priority 10. This pull request will be tested once the tree is reopened.

Reason for tree closure: Main branch is broken

@rust-bors rust-bors Bot 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 Aug 20, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Aug 20, 2026
core: Make funnel shifts panic only if overflow checks are enabled

Change from the existing behavior of panicking unconditionally to panicking only if overflow checks are enabled, wrapping otherwise. This is more consistent with other operations, and gives slightly better codegen https://rust.godbolt.org/z/Yz81zhbno.

Suggested in the stabilization PR at rust-lang#161015 (comment).

The first commit adds some simple tests for overflow behavior that I couldn't find existing anywhere else.
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #161399 (comment)

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 20, 2026
@rust-bors

rust-bors Bot commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#161399), which was unapproved.

View changes since this unapproval

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants