Clean up unchecked_math, separate out unchecked_shifts

This commit is contained in:
ltdk 2023-09-06 23:24:23 -04:00
parent 130ff8cb6c
commit 91405ab74a
7 changed files with 49 additions and 23 deletions

View File

@ -136,7 +136,6 @@
#![feature(const_hash)] #![feature(const_hash)]
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_index_range_slice_index)] #![feature(const_index_range_slice_index)]
#![feature(const_inherent_unchecked_arith)]
#![feature(const_int_unchecked_arith)] #![feature(const_int_unchecked_arith)]
#![feature(const_intrinsic_forget)] #![feature(const_intrinsic_forget)]
#![feature(const_ipv4)] #![feature(const_ipv4)]
@ -190,6 +189,8 @@
#![feature(str_split_inclusive_remainder)] #![feature(str_split_inclusive_remainder)]
#![feature(str_split_remainder)] #![feature(str_split_remainder)]
#![feature(strict_provenance)] #![feature(strict_provenance)]
#![feature(unchecked_math)]
#![feature(unchecked_shifts)]
#![feature(utf16_extra)] #![feature(utf16_extra)]
#![feature(utf16_extra_const)] #![feature(utf16_extra_const)]
#![feature(variant_count)] #![feature(variant_count)]

View File

@ -471,7 +471,7 @@ macro_rules! int_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self { pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
@ -539,7 +539,7 @@ macro_rules! int_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self { pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
@ -607,7 +607,7 @@ macro_rules! int_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self { pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
@ -740,6 +740,31 @@ macro_rules! int_impl {
if unlikely!(b) {None} else {Some(a)} 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 /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger
/// than or equal to the number of bits in `self`. /// than or equal to the number of bits in `self`.
/// ///
@ -772,13 +797,13 @@ macro_rules! int_impl {
/// ///
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")] #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
#[unstable( #[unstable(
feature = "unchecked_math", feature = "unchecked_shifts",
reason = "niche optimization path", reason = "niche optimization path",
issue = "85122", issue = "85122",
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self { pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
@ -820,13 +845,13 @@ macro_rules! int_impl {
/// ///
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")] #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
#[unstable( #[unstable(
feature = "unchecked_math", feature = "unchecked_shifts",
reason = "niche optimization path", reason = "niche optimization path",
issue = "85122", issue = "85122",
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self { pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
@ -1404,7 +1429,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline(always)] #[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 { pub const fn wrapping_shl(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift // SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds // out of bounds
@ -1434,7 +1459,7 @@ macro_rules! int_impl {
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline(always)] #[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 { pub const fn wrapping_shr(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift // SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds // out of bounds

View File

@ -479,7 +479,7 @@ macro_rules! uint_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_add(self, rhs: Self) -> Self { pub const unsafe fn unchecked_add(self, rhs: Self) -> Self {
@ -548,7 +548,7 @@ macro_rules! uint_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self { pub const unsafe fn unchecked_sub(self, rhs: Self) -> Self {
@ -595,7 +595,7 @@ macro_rules! uint_impl {
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_math", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self { pub const unsafe fn unchecked_mul(self, rhs: Self) -> Self {
@ -926,13 +926,13 @@ macro_rules! uint_impl {
/// ///
#[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")] #[doc = concat!("[`checked_shl`]: ", stringify!($SelfT), "::checked_shl")]
#[unstable( #[unstable(
feature = "unchecked_math", feature = "unchecked_shifts",
reason = "niche optimization path", reason = "niche optimization path",
issue = "85122", issue = "85122",
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self { pub const unsafe fn unchecked_shl(self, rhs: u32) -> Self {
@ -974,13 +974,13 @@ macro_rules! uint_impl {
/// ///
#[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")] #[doc = concat!("[`checked_shr`]: ", stringify!($SelfT), "::checked_shr")]
#[unstable( #[unstable(
feature = "unchecked_math", feature = "unchecked_shifts",
reason = "niche optimization path", reason = "niche optimization path",
issue = "85122", issue = "85122",
)] )]
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[rustc_const_unstable(feature = "const_inherent_unchecked_arith", issue = "85122")] #[rustc_const_unstable(feature = "unchecked_shifts", issue = "85122")]
#[inline(always)] #[inline(always)]
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces #[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self { pub const unsafe fn unchecked_shr(self, rhs: u32) -> Self {
@ -1418,7 +1418,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline(always)] #[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 { pub const fn wrapping_shl(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift // SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds // out of bounds
@ -1451,7 +1451,7 @@ macro_rules! uint_impl {
#[must_use = "this returns the result of the operation, \ #[must_use = "this returns the result of the operation, \
without modifying the original"] without modifying the original"]
#[inline(always)] #[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 { pub const fn wrapping_shr(self, rhs: u32) -> Self {
// SAFETY: the masking by the bitsize of the type ensures that we do not shift // SAFETY: the masking by the bitsize of the type ensures that we do not shift
// out of bounds // out of bounds

View File

@ -1,4 +1,4 @@
#![feature(unchecked_math)] #![feature(unchecked_shifts)]
fn main() { fn main() {
unsafe { unsafe {

View File

@ -1,4 +1,4 @@
#![feature(unchecked_math)] #![feature(unchecked_shifts)]
fn main() { fn main() {
unsafe { unsafe {

View File

@ -2,7 +2,7 @@
// ignore-debug (because unchecked is checked in debug) // ignore-debug (because unchecked is checked in debug)
#![crate_type = "lib"] #![crate_type = "lib"]
#![feature(unchecked_math)] #![feature(unchecked_shifts)]
// CHECK-LABEL: @unchecked_shl_unsigned_same // CHECK-LABEL: @unchecked_shl_unsigned_same
#[no_mangle] #[no_mangle]

View File

@ -1,6 +1,6 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY // EMIT_MIR_FOR_EACH_PANIC_STRATEGY
#![crate_type = "lib"] #![crate_type = "lib"]
#![feature(unchecked_math)] #![feature(unchecked_shifts)]
// ignore-debug: the debug assertions prevent the inlining we are testing for // ignore-debug: the debug assertions prevent the inlining we are testing for
// compile-flags: -Zmir-opt-level=2 -Zinline-mir // compile-flags: -Zmir-opt-level=2 -Zinline-mir