Skip to content

Commit c0ed667

Browse files
authored
Rollup merge of rust-lang#115626 - clarfonthey:unchecked-math, r=thomcc
Clean up unchecked_math, separate out unchecked_shifts Tracking issue: rust-lang#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.
2 parents dd24c7b + 91405ab commit c0ed667

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed

Diff for: library/core/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@
134134
#![feature(const_hash)]
135135
#![feature(const_heap)]
136136
#![feature(const_index_range_slice_index)]
137-
#![feature(const_inherent_unchecked_arith)]
138137
#![feature(const_int_unchecked_arith)]
139138
#![feature(const_intrinsic_forget)]
140139
#![feature(const_ipv4)]
@@ -188,6 +187,8 @@
188187
#![feature(str_split_inclusive_remainder)]
189188
#![feature(str_split_remainder)]
190189
#![feature(strict_provenance)]
190+
#![feature(unchecked_math)]
191+
#![feature(unchecked_shifts)]
191192
#![feature(utf16_extra)]
192193
#![feature(utf16_extra_const)]
193194
#![feature(variant_count)]

Diff for: library/core/src/num/int_macros.rs

+34-9
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ macro_rules! int_impl {
471471
)]
472472
#[must_use = "this returns the result of the operation, \
473473
without modifying the original"]
474-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
474+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
475475
#[inline(always)]
476476
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
477477
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
@@ -539,7 +539,7 @@ macro_rules! int_impl {
539539
)]
540540
#[must_use = "this returns the result of the operation, \
541541
without modifying the original"]
542-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
542+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
543543
#[inline(always)]
544544
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
545545
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
@@ -607,7 +607,7 @@ macro_rules! int_impl {
607607
)]
608608
#[must_use = "this returns the result of the operation, \
609609
without modifying the original"]
610-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
610+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
611611
#[inline(always)]
612612
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
613613
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
@@ -740,6 +740,31 @@ macro_rules! int_impl {
740740
if unlikely!(b) {None} else {Some(a)}
741741
}
742742

743+
/// Unchecked negation. Computes `-self`, assuming overflow cannot occur.
744+
///
745+
/// # Safety
746+
///
747+
/// This results in undefined behavior when
748+
#[doc = concat!("`self == ", stringify!($SelfT), "::MIN`,")]
749+
/// i.e. when [`checked_neg`] would return `None`.
750+
///
751+
#[doc = concat!("[`checked_neg`]: ", stringify!($SelfT), "::checked_neg")]
752+
#[unstable(
753+
feature = "unchecked_neg",
754+
reason = "niche optimization path",
755+
issue = "85122",
756+
)]
757+
#[must_use = "this returns the result of the operation, \
758+
without modifying the original"]
759+
#[rustc_const_unstable(feature = "unchecked_neg", issue = "85122")]
760+
#[inline(always)]
761+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
762+
pub const unsafe fn unchecked_neg(self) -> Self {
763+
// SAFETY: the caller must uphold the safety contract for
764+
// `unchecked_neg`.
765+
unsafe { intrinsics::unchecked_sub(0, self) }
766+
}
767+
743768
/// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
744769
/// than or equal to the number of bits in `self`.
745770
///
@@ -772,13 +797,13 @@ macro_rules! int_impl {
772797
///
773798
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
774799
#[unstable(
775-
feature = "unchecked_math",
800+
feature = "unchecked_shifts",
776801
reason = "niche optimization path",
777802
issue = "85122",
778803
)]
779804
#[must_use = "this returns the result of the operation, \
780805
without modifying the original"]
781-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
806+
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
782807
#[inline(always)]
783808
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
784809
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
@@ -820,13 +845,13 @@ macro_rules! int_impl {
820845
///
821846
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
822847
#[unstable(
823-
feature = "unchecked_math",
848+
feature = "unchecked_shifts",
824849
reason = "niche optimization path",
825850
issue = "85122",
826851
)]
827852
#[must_use = "this returns the result of the operation, \
828853
without modifying the original"]
829-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
854+
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
830855
#[inline(always)]
831856
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
832857
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
@@ -1404,7 +1429,7 @@ macro_rules! int_impl {
14041429
#[must_use = "this returns the result of the operation, \
14051430
without modifying the original"]
14061431
#[inline(always)]
1407-
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1432+
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
14081433
pub const fn wrapping_shl(self, rhs: u32) -> Self {
14091434
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14101435
// out of bounds
@@ -1434,7 +1459,7 @@ macro_rules! int_impl {
14341459
#[must_use = "this returns the result of the operation, \
14351460
without modifying the original"]
14361461
#[inline(always)]
1437-
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1462+
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
14381463
pub const fn wrapping_shr(self, rhs: u32) -> Self {
14391464
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14401465
// out of bounds

Diff for: library/core/src/num/uint_macros.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ macro_rules! uint_impl {
479479
)]
480480
#[must_use = "this returns the result of the operation, \
481481
without modifying the original"]
482-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
482+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
483483
#[inline(always)]
484484
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
485485
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
@@ -548,7 +548,7 @@ macro_rules! uint_impl {
548548
)]
549549
#[must_use = "this returns the result of the operation, \
550550
without modifying the original"]
551-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
551+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
552552
#[inline(always)]
553553
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
554554
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
@@ -595,7 +595,7 @@ macro_rules! uint_impl {
595595
)]
596596
#[must_use = "this returns the result of the operation, \
597597
without modifying the original"]
598-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
598+
#[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
599599
#[inline(always)]
600600
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
601601
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
@@ -926,13 +926,13 @@ macro_rules! uint_impl {
926926
///
927927
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
928928
#[unstable(
929-
feature = "unchecked_math",
929+
feature = "unchecked_shifts",
930930
reason = "niche optimization path",
931931
issue = "85122",
932932
)]
933933
#[must_use = "this returns the result of the operation, \
934934
without modifying the original"]
935-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
935+
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
936936
#[inline(always)]
937937
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
938938
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
@@ -974,13 +974,13 @@ macro_rules! uint_impl {
974974
///
975975
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
976976
#[unstable(
977-
feature = "unchecked_math",
977+
feature = "unchecked_shifts",
978978
reason = "niche optimization path",
979979
issue = "85122",
980980
)]
981981
#[must_use = "this returns the result of the operation, \
982982
without modifying the original"]
983-
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")]
983+
#[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
984984
#[inline(always)]
985985
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
986986
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
@@ -1418,7 +1418,7 @@ macro_rules! uint_impl {
14181418
#[must_use = "this returns the result of the operation, \
14191419
without modifying the original"]
14201420
#[inline(always)]
1421-
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1421+
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
14221422
pub const fn wrapping_shl(self, rhs: u32) -> Self {
14231423
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14241424
// out of bounds
@@ -1451,7 +1451,7 @@ macro_rules! uint_impl {
14511451
#[must_use = "this returns the result of the operation, \
14521452
without modifying the original"]
14531453
#[inline(always)]
1454-
#[rustc_allow_const_fn_unstable(const_inherent_unchecked_arith)]
1454+
#[rustc_allow_const_fn_unstable(unchecked_shifts)]
14551455
pub const fn wrapping_shr(self, rhs: u32) -> Self {
14561456
// SAFETY: the masking by the bitsize of the type ensures that we do not shift
14571457
// out of bounds

Diff for: src/tools/miri/tests/fail/intrinsics/unchecked_shl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(unchecked_math)]
1+
#![feature(unchecked_shifts)]
22

33
fn main() {
44
unsafe {

Diff for: src/tools/miri/tests/fail/intrinsics/unchecked_shr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(unchecked_math)]
1+
#![feature(unchecked_shifts)]
22

33
fn main() {
44
unsafe {

Diff for: tests/codegen/unchecked_shifts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// ignore-debug (because unchecked is checked in debug)
33

44
#![crate_type = "lib"]
5-
#![feature(unchecked_math)]
5+
#![feature(unchecked_shifts)]
66

77
// CHECK-LABEL: @unchecked_shl_unsigned_same
88
#[no_mangle]

Diff for: tests/mir-opt/inline/unchecked_shifts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// skip-filecheck
22
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
33
#![crate_type = "lib"]
4-
#![feature(unchecked_math)]
4+
#![feature(unchecked_shifts)]
55

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

0 commit comments

Comments
 (0)