Skip to content

Checked/wrapping/strict/unbounded funnel shifts - #161119

Open
pthariensflame wants to merge 1 commit into
rust-lang:mainfrom
pthariensflame:wrapping-funnel-shifts
Open

Checked/wrapping/strict/unbounded funnel shifts#161119
pthariensflame wants to merge 1 commit into
rust-lang:mainfrom
pthariensflame:wrapping-funnel-shifts

Conversation

@pthariensflame

@pthariensflame pthariensflame commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

This implements the wrapping funnel shifts requested at rust-lang/libs-team#642 (comment) and also some additional forms of funnel shift proposed at rust-lang/libs-team#855; this might interact with #161015, which is attempting to stabilize the original funnel shift methods.

The work in this PR was done entirely by some people inside a human brain, without the use of any LLMs…or much use of an IDE since ours kept crashing during this process and we had to resort to a plain text editor. 😅

This implements the tracking issue #161798.

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

rustbot commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
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

@tgross35 tgross35 added the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Aug 15, 2026
@tgross35

Copy link
Copy Markdown
Member

ACP at rust-lang/libs-team#855

Comment thread library/core/src/num/uint_macros.rs Outdated
@rust-bors

This comment has been minimized.

@pthariensflame pthariensflame changed the title Checked/wrapping/unbounded funnel shifts Checked/wrapping/strict/unbounded funnel shifts Aug 26, 2026
@pthariensflame
pthariensflame force-pushed the wrapping-funnel-shifts branch from 3382d39 to bdf9037 Compare August 26, 2026 03:57
@rustbot

rustbot commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@tgross35 tgross35 left a comment

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.

Implementations look alright but needs examples and tests.

r? me

View changes since this review

self.wrapping_funnel_shr(right, n)
}

/// Performs a left funnel shift.

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.

All of the summary lines say "Performs a left/right funnel shift." Please update these to be a bit more similar to the {checked,wrapping,strict,unbounded}_{shl,shr}.

Comment on lines +644 to +646
///
/// This function will return `None` if `n` is greater than or equal to the number of
/// bits in `self`, i.e., when [`funnel_shl`](Self::funnel_shl) might panic or wrap.

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.

Please add examples

Comment on lines +645 to +646
/// This function will return `None` if `n` is greater than or equal to the number of
/// bits in `self`, i.e., when [`funnel_shl`](Self::funnel_shl) might panic or wrap.

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.

Use "would" rather than "might" because we guarantee it would do one of those two things.

Comment on lines +677 to +686
/// Performs a left funnel shift.
///
/// This function shifts by `mask(n)`, where `mask` removes any high-order bits of `n`
/// that would cause the shift to exceed the bitwidth of the type. As a result, this
/// function never panics, unlike [`funnel_shl`](Self::funnel_shl).
#[rustc_const_unstable(feature = "wrapping_funnel_shifts", issue = "161798")]
#[unstable(feature = "wrapping_funnel_shifts", issue = "161798")]
#[must_use = "this returns the result of the operation, without modifying the original"]
#[inline(always)]
pub const fn wrapping_funnel_shl(self, right: Self, n: u32) -> Self {

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.

Include the "Beware that, unlike most other wrapping_* methods on integer" from wrapping_shl, adapted to funnel shifts. The result can be extra unintuitive because of the reason mentioned at rust-lang/libs-team#642 (comment), try to capture that in an example.

(same for wrapping_funnel_shr)

Comment on lines +725 to +733
/// # Panics
///
/// This function will always panic if `n` is greater than or equal to the number of
/// bits in `self`, i.e., when [`funnel_shr`](Self::funnel_shr) might panic or wrap.
#[rustc_const_unstable(feature = "wrapping_funnel_shifts", issue = "161798")]
#[unstable(feature = "wrapping_funnel_shifts", issue = "161798")]
#[must_use = "this returns the result of the operation, without modifying the original"]
#[inline(always)]
pub const fn strict_funnel_shr(self, right: Self, n: u32) -> Self {

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.

The strict_* methods have a subsection "overflow behavior". I don't love that, but this should be consistent

https://doc.rust-lang.org/nightly/std/primitive.u32.html#method.strict_shl

#[must_use = "this returns the result of the operation, without modifying the original"]
#[inline(always)]
pub const fn strict_funnel_shr(self, right: Self, n: u32) -> Self {
assert!(n < Self::BITS, "attempt to funnel shift right with overflow");

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.

These should probably go in the overflow_panic module like the other strict_* functions to be consistent. Though I'm not really sure why that module exists.

Comment on lines +256 to +265
#[test]
fn test_wrapping_funnel_shl_overflow() {
let _ = <$T>::wrapping_funnel_shl(A, B, $T::BITS);
}

#[test]
fn test_wrapping_funnel_shr_overflow() {
let _ = <$T>::wrapping_funnel_shr(A, B, $T::BITS);
}

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.

These aren't actually testing anything other than the fact that we don't panic?

These methods should be tested in test_funnel_shifts_runtime and test_funnel_shift

@rustbot rustbot assigned tgross35 and unassigned JohnTitor Aug 27, 2026
@rustbot rustbot 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 Aug 27, 2026
@rustbot

rustbot commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants