-
Notifications
You must be signed in to change notification settings - Fork 13.5k
De-LLVM the unchecked shifts [MCP#693] #123226
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2224,18 +2224,20 @@ extern "rust-intrinsic" { | |
/// Safe wrappers for this intrinsic are available on the integer | ||
/// primitives via the `checked_shl` method. For example, | ||
/// [`u32::checked_shl`] | ||
#[cfg(not(bootstrap))] | ||
#[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")] | ||
#[rustc_nounwind] | ||
pub fn unchecked_shl<T: Copy>(x: T, y: T) -> T; | ||
pub fn unchecked_shl<T: Copy, U: Copy>(x: T, y: U) -> T; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to make shift amount generic? Why not just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Making the shift amount always u32 was actually what I did first. However, MIR actually supports arbitrary heterogeneous shifts, even though for the unchecked once those weren't accessible. So when the RHS was always So supporting mixed types here is helpful in that we can still write tests that exercise those codepaths in CTFE and Codegen. And it doesn't make the lowering to MIR any harder, nor add any extra work to what the backends already needed to handle. It also turned out convenient in that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit weird (given that we don't publicly expose this), but given this does not make backend more complex, ig this is fine. |
||
/// Performs an unchecked right shift, resulting in undefined behavior when | ||
/// `y < 0` or `y >= N`, where N is the width of T in bits. | ||
/// | ||
/// Safe wrappers for this intrinsic are available on the integer | ||
/// primitives via the `checked_shr` method. For example, | ||
/// [`u32::checked_shr`] | ||
#[cfg(not(bootstrap))] | ||
#[rustc_const_stable(feature = "const_int_unchecked", since = "1.40.0")] | ||
#[rustc_nounwind] | ||
pub fn unchecked_shr<T: Copy>(x: T, y: T) -> T; | ||
pub fn unchecked_shr<T: Copy, U: Copy>(x: T, y: U) -> T; | ||
|
||
/// Returns the result of an unchecked addition, resulting in | ||
/// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`. | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) }; | |
|
||
const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) }; | ||
const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_i16, 16) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is a fun typo. So much so that I opened a clippy issue for a lint about it *looks* wow, 6 years ago now rust-lang/rust-clippy#3091 |
||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
@@ -40,7 +40,7 @@ const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) }; | |
|
||
const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) }; | ||
const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_i16, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
@@ -54,7 +54,7 @@ const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) }; | |
|
||
const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) }; | ||
const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_i16, -13) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
@@ -82,7 +82,7 @@ const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) }; | |
|
||
const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) }; | ||
const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_i16, 16) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
@@ -95,7 +95,7 @@ const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) }; | |
|
||
const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) }; | ||
const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_i16, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
@@ -109,7 +109,7 @@ const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) }; | |
|
||
const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) }; | ||
const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_i16, -13) }; | ||
//~^ ERROR evaluation of constant value failed | ||
const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) }; | ||
//~^ ERROR evaluation of constant value failed | ||
|
Uh oh!
There was an error while loading. Please reload this page.