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
Contributor

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
Contributor 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.
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 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.

///
/// ## 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
Contributor 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.

@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.

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

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants