Skip to content

Commit

Permalink
Rollup merge of #115626 - clarfonthey:unchecked-math, r=thomcc
Browse files Browse the repository at this point in the history
Clean up unchecked_math, separate out unchecked_shifts

Tracking issue: #85122

Changes:

1. Remove `const_inherent_unchecked_arith` flag and make const-stability flags the same as the method feature flags. Given the number of other unsafe const fns already stabilised, it makes sense to just stabilise these in const context when they're stabilised.
2. Move `unchecked_shl` and `unchecked_shr` into a separate `unchecked_shifts` flag, since the semantics for them are unclear and they'll likely be stabilised separately as a result.
3. Add an `unchecked_neg` method exclusively to signed integers, under the `unchecked_neg` flag. This is because it's a new API and probably needs some time to marinate before it's stabilised, and while it *would* make sense to have a similar version for unsigned integers since `checked_neg` also exists for those there is absolutely no case where that would be a good idea, IMQHO.

The longer-term goal here is to prepare the `unchecked_math` methods for an FCP and stabilisation since they've existed for a while, their semantics are clear, and people seem in favour of stabilising them.
  • Loading branch information
matthiaskrgr authored Nov 1, 2023
2 parents 815b3ae + 91405ab commit 260e07b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 23 deletions.
3 changes: 2 additions & 1 deletion library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@
#![feature(const_hash)]
#![feature(const_heap)]
#![feature(const_index_range_slice_index)]
#![feature(const_inherent_unchecked_arith)]
#![feature(const_int_unchecked_arith)]
#![feature(const_intrinsic_forget)]
#![feature(const_ipv4)]
Expand Down Expand Up @@ -188,6 +187,8 @@
#![feature(str_split_inclusive_remainder)]
#![feature(str_split_remainder)]
#![feature(strict_provenance)]
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]
#![feature(utf16_extra)]
#![feature(utf16_extra_const)]
#![feature(variant_count)]
Expand Down
43 changes: 34 additions & 9 deletions library/core/src/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ macro_rules! int_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -539,7 +539,7 @@ macro_rules! int_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -607,7 +607,7 @@ macro_rules! int_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -740,6 +740,31 @@ macro_rules! int_impl {
if unlikely!(b) {None} else {Some(a)}
}

/// Unchecked negation. Computes `-self`, assuming overflow cannot occur.
///
/// # Safety
///
/// This results in undefined behavior when
#[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_neg`] would return `None`.
///
#[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
#[unstable(
feature = "unchecked_neg",
reason = "niche optimization path",
issue = "85122",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "unchecked_neg", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_neg(self) -> Self {
// SAFETY: the caller must uphold the safety contract for
// `unchecked_neg`.
unsafe { intrinsics::unchecked_sub(0, self) }
}

/// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
/// than or equal to the number of bits in `self`.
///
Expand Down Expand Up @@ -772,13 +797,13 @@ macro_rules! int_impl {
///
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
#[unstable(
feature = "unchecked_math",
feature = "unchecked_shifts",
reason = "niche optimization path",
issue = "85122",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
Expand Down Expand Up @@ -820,13 +845,13 @@ macro_rules! int_impl {
///
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
#[unstable(
feature = "unchecked_math",
feature = "unchecked_shifts",
reason = "niche optimization path",
issue = "85122",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
Expand Down Expand Up @@ -1404,7 +1429,7 @@ 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)]
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
pub const fn wrapping_shl(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
Expand Down Expand Up @@ -1434,7 +1459,7 @@ 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)]
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
pub const fn wrapping_shr(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
Expand Down
18 changes: 9 additions & 9 deletions library/core/src/num/uint_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ macro_rules! uint_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -548,7 +548,7 @@ macro_rules! uint_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -595,7 +595,7 @@ macro_rules! uint_impl {
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
Expand Down Expand Up @@ -926,13 +926,13 @@ macro_rules! uint_impl {
///
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
#[unstable(
feature = "unchecked_math",
feature = "unchecked_shifts",
reason = "niche optimization path",
issue = "85122",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
Expand Down Expand Up @@ -974,13 +974,13 @@ macro_rules! uint_impl {
///
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
#[unstable(
feature = "unchecked_math",
feature = "unchecked_shifts",
reason = "niche optimization path",
issue = "85122",
)]
#[must_use = "this returns the result of the operation, \
without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
Expand Down Expand Up @@ -1418,7 +1418,7 @@ macro_rules! uint_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)]
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
pub const fn wrapping_shl(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
Expand Down Expand Up @@ -1451,7 +1451,7 @@ macro_rules! uint_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)]
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
pub const fn wrapping_shr(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/intrinsics/unchecked_shl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]

fn main() {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/intrinsics/unchecked_shr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]

fn main() {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen/unchecked_shifts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// ignore-debug (because unchecked is checked in debug)

#![crate_type = "lib"]
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]

// CHECK-LABEL: @unchecked_shl_unsigned_same
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/inline/unchecked_shifts.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// skip-filecheck
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
#![crate_type = "lib"]
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]

// ignore-debug: the debug assertions prevent the inlining we are testing for
// compile-flags: -Zmir-opt-level=2 -Zinline-mir
Expand Down

0 comments on commit 260e07b

Please sign in to comment.