Skip to content

Commit 91405ab

Browse files
committed
Clean up unchecked_math, separate out unchecked_shifts
1 parent 130ff8c commit 91405ab

File tree

7 files changed

+49
-23
lines changed

7 files changed

+49
-23
lines changed

library/core/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@
136136
#![feature(const_hash)]
137137
#![feature(const_heap)]
138138
#![feature(const_index_range_slice_index)]
139-
#![feature(const_inherent_unchecked_arith)]
140139
#![feature(const_int_unchecked_arith)]
141140
#![feature(const_intrinsic_forget)]
142141
#![feature(const_ipv4)]
@@ -190,6 +189,8 @@
190189
#![feature(str_split_inclusive_remainder)]
191190
#![feature(str_split_remainder)]
192191
#![feature(strict_provenance)]
192+
#![feature(unchecked_math)]
193+
#![feature(unchecked_shifts)]
193194
#![feature(utf16_extra)]
194195
#![feature(utf16_extra_const)]
195196
#![feature(variant_count)]

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

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

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 {

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 {

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]

tests/mir-opt/inline/unchecked_shifts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
22
#![crate_type = "lib"]
3-
#![feature(unchecked_math)]
3+
#![feature(unchecked_shifts)]
44

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

0 commit comments

Comments
 (0)