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

unchecked_{shl|shr} should use u32 as the RHS #103456

Merged
merged 2 commits into from
Nov 19, 2022

Conversation

scottmcm
Copy link
Member

The other shift methods, such as https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.checked_shr and https://doc.rust-lang.org/nightly/std/primitive.i16.html#method.wrapping_shl, use u32 for the shift amount. That's consistent with other things, like count_ones, which also always use u32 for a bit count, regardless of the size of the type.

This PR changes unchecked_shl and unchecked_shr to also use u32 for the shift amount (rather than Self).

cc #85122, the unchecked_math tracking issue

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Oct 24, 2022
@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rustbot
Copy link
Collaborator

rustbot commented Oct 24, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 24, 2022
#[no_mangle]
pub unsafe fn unchecked_shl_unsigned_same(a: u32, b: u32) -> u32 {
// CHECK-NOT: and i32
// CHECK: shl i32 %a, %b
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: the LLVM instruction always gives poison by default for too-high shift amounts (https://llvm.org/docs/LangRef.html#shl-instruction), so no extra flags are expected in the IR.

@@ -1383,11 +1386,12 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[inline(always)]
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: this was previously using the intrinsic on stable, so the rustc_allow_const_fn_unstable isn't exposing anything new to stable here.

// SAFETY: the caller must uphold the safety contract for
// `unchecked_shl`.
unsafe { intrinsics::unchecked_shl(self, rhs) }
// Any legal shift amount is losslessly representable in the self type.
unsafe { intrinsics::unchecked_shl(self, rhs.try_into().ok().unwrap_unchecked()) }
Copy link
Member Author

Choose a reason for hiding this comment

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

annot: the .ok() looks silly here, but Result::unwrap_unchecked isn't const, whereas Option::unwrap_unchecked is const.

@scottmcm scottmcm force-pushed the fix-unchecked-shifts branch from debadbc to 3b16c04 Compare October 24, 2022 00:33
@scottmcm scottmcm 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 Oct 25, 2022
@scottmcm scottmcm added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Nov 15, 2022
@scottmcm
Copy link
Member Author

Hi libs-api! I'm nominating this one to get a 👍/👎 on the signature change, in hopes of then being able to move it to a less-busy reviewer once the API decision is made.

@m-ou-se
Copy link
Member

m-ou-se commented Nov 15, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Nov 15, 2022

📌 Commit 3b16c04 has been approved by m-ou-se

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 Nov 15, 2022
@m-ou-se m-ou-se removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Nov 15, 2022
@scottmcm scottmcm assigned m-ou-se and unassigned joshtriplett Nov 16, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Nov 16, 2022
…-ou-se

`unchecked_{shl|shr}` should use `u32` as the RHS

The other shift methods, such as https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.checked_shr and https://doc.rust-lang.org/nightly/std/primitive.i16.html#method.wrapping_shl, use `u32` for the shift amount.  That's consistent with other things, like `count_ones`, which also always use `u32` for a bit count, regardless of the size of the type.

This PR changes `unchecked_shl` and `unchecked_shr` to also use `u32` for the shift amount (rather than Self).

cc rust-lang#85122, the `unchecked_math` tracking issue
@scottmcm
Copy link
Member Author

scottmcm commented Nov 16, 2022

@bors rollup=iffy (failed in a rollup once, but with the update that config worked in CI)
@bors r-

Failed in rollup https://github.com/rust-lang-ci/rust/actions/runs/3477142652/jobs/5813017785

@bors bors 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 Nov 16, 2022
@rustbot rustbot added the A-testsuite Area: The testsuite used to check the correctness of rustc label Nov 17, 2022
@rustbot rustbot added the T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. label Nov 17, 2022
@scottmcm scottmcm force-pushed the fix-unchecked-shifts branch 2 times, most recently from 1e783ac to 9d4b1f9 Compare November 17, 2022 00:44
@scottmcm
Copy link
Member Author

Did the test-in-CI trick, and it doesn't fail that config any more: https://github.com/rust-lang/rust/actions/runs/3484161910/jobs/5828408349

@bors r+

@bors
Copy link
Contributor

bors commented Nov 17, 2022

📌 Commit 9d4b1f9 has been approved by scottmcm

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Nov 17, 2022

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

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 17, 2022
Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 18, 2022
…cottmcm

`unchecked_{shl|shr}` should use `u32` as the RHS

The other shift methods, such as https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.checked_shr and https://doc.rust-lang.org/nightly/std/primitive.i16.html#method.wrapping_shl, use `u32` for the shift amount.  That's consistent with other things, like `count_ones`, which also always use `u32` for a bit count, regardless of the size of the type.

This PR changes `unchecked_shl` and `unchecked_shr` to also use `u32` for the shift amount (rather than Self).

cc rust-lang#85122, the `unchecked_math` tracking issue
Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 18, 2022
…cottmcm

`unchecked_{shl|shr}` should use `u32` as the RHS

The other shift methods, such as https://doc.rust-lang.org/nightly/std/primitive.u64.html#method.checked_shr and https://doc.rust-lang.org/nightly/std/primitive.i16.html#method.wrapping_shl, use `u32` for the shift amount.  That's consistent with other things, like `count_ones`, which also always use `u32` for a bit count, regardless of the size of the type.

This PR changes `unchecked_shl` and `unchecked_shr` to also use `u32` for the shift amount (rather than Self).

cc rust-lang#85122, the `unchecked_math` tracking issue
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 18, 2022
…earth

Rollup of 8 pull requests

Successful merges:

 - rust-lang#102977 (remove HRTB from `[T]::is_sorted_by{,_key}`)
 - rust-lang#103378 (Fix mod_inv termination for the last iteration)
 - rust-lang#103456 (`unchecked_{shl|shr}` should use `u32` as the RHS)
 - rust-lang#103701 (Simplify some pointer method implementations)
 - rust-lang#104047 (Diagnostics `icu4x` based list formatting.)
 - rust-lang#104338 (Enforce that `dyn*` coercions are actually pointer-sized)
 - rust-lang#104498 (Edit docs for `rustc_errors::Handler::stash_diagnostic`)
 - rust-lang#104556 (rustdoc: use `code-header` class to format enum variants)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e230115 into rust-lang:master Nov 19, 2022
@rustbot rustbot added this to the 1.67.0 milestone Nov 19, 2022
@scottmcm scottmcm deleted the fix-unchecked-shifts branch November 19, 2022 03:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-infra Relevant to the infrastructure team, which will review and decide on the PR/issue. 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.

6 participants