Stabilize unchecked_{add,sub,mul}

This commit is contained in:
Scott McMurray 2024-03-14 15:08:53 -07:00
parent fd27e8745f
commit 234e383c34
9 changed files with 70 additions and 52 deletions

View File

@ -165,7 +165,6 @@
#![feature(try_trait_v2)] #![feature(try_trait_v2)]
#![feature(try_with_capacity)] #![feature(try_with_capacity)]
#![feature(tuple_trait)] #![feature(tuple_trait)]
#![feature(unchecked_math)]
#![feature(unicode_internals)] #![feature(unicode_internals)]
#![feature(unsize)] #![feature(unsize)]
#![feature(utf8_chunks)] #![feature(utf8_chunks)]

View File

@ -2224,24 +2224,27 @@ pub const fn unlikely(b: bool) -> bool {
/// Returns the result of an unchecked addition, resulting in /// Returns the result of an unchecked addition, resulting in
/// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`. /// undefined behavior when `x + y > T::MAX` or `x + y < T::MIN`.
/// ///
/// This intrinsic does not have a stable counterpart. /// The stable counterpart of this intrinsic is `unchecked_add` on the various
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] /// integer types, such as [`u16::unchecked_add`] and [`i64::unchecked_add`].
#[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
#[rustc_nounwind] #[rustc_nounwind]
pub fn unchecked_add<T: Copy>(x: T, y: T) -> T; pub fn unchecked_add<T: Copy>(x: T, y: T) -> T;
/// Returns the result of an unchecked subtraction, resulting in /// Returns the result of an unchecked subtraction, resulting in
/// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`. /// undefined behavior when `x - y > T::MAX` or `x - y < T::MIN`.
/// ///
/// This intrinsic does not have a stable counterpart. /// The stable counterpart of this intrinsic is `unchecked_sub` on the various
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] /// integer types, such as [`u16::unchecked_sub`] and [`i64::unchecked_sub`].
#[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
#[rustc_nounwind] #[rustc_nounwind]
pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T; pub fn unchecked_sub<T: Copy>(x: T, y: T) -> T;
/// Returns the result of an unchecked multiplication, resulting in /// Returns the result of an unchecked multiplication, resulting in
/// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`. /// undefined behavior when `x * y > T::MAX` or `x * y < T::MIN`.
/// ///
/// This intrinsic does not have a stable counterpart. /// The stable counterpart of this intrinsic is `unchecked_mul` on the various
#[rustc_const_unstable(feature = "const_int_unchecked_arith", issue = "none")] /// integer types, such as [`u16::unchecked_mul`] and [`i64::unchecked_mul`].
#[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
#[rustc_nounwind] #[rustc_nounwind]
pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T; pub fn unchecked_mul<T: Copy>(x: T, y: T) -> T;

View File

@ -134,7 +134,6 @@
#![feature(const_heap)] #![feature(const_heap)]
#![feature(const_hint_assert_unchecked)] #![feature(const_hint_assert_unchecked)]
#![feature(const_index_range_slice_index)] #![feature(const_index_range_slice_index)]
#![feature(const_int_unchecked_arith)]
#![feature(const_intrinsic_copy)] #![feature(const_intrinsic_copy)]
#![feature(const_intrinsic_forget)] #![feature(const_intrinsic_forget)]
#![feature(const_ipv4)] #![feature(const_ipv4)]
@ -193,7 +192,6 @@
#![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(unchecked_shifts)]
#![feature(utf16_extra)] #![feature(utf16_extra)]
#![feature(utf16_extra_const)] #![feature(utf16_extra_const)]

View File

@ -492,21 +492,25 @@ pub const fn strict_add(self, rhs: Self) -> Self {
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_add(y)` is semantically equivalent to calling
/// `x.`[`checked_add`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_add`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_add`] would return `None`. /// i.e. when [`checked_add`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")] #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
#[unstable( #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {
@ -630,21 +634,25 @@ pub const fn strict_sub(self, rhs: Self) -> Self {
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_sub(y)` is semantically equivalent to calling
/// `x.`[`checked_sub`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_sub`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_sub`] would return `None`. /// i.e. when [`checked_sub`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")] #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
#[unstable( #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {
@ -768,21 +776,25 @@ pub const fn strict_mul(self, rhs: Self) -> Self {
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_mul(y)` is semantically equivalent to calling
/// `x.`[`checked_mul`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_mul`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_mul`] would return `None`. /// i.e. when [`checked_mul`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")] #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
#[unstable( #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {

View File

@ -500,21 +500,25 @@ pub const fn strict_add(self, rhs: Self) -> Self {
/// Unchecked integer addition. Computes `self + rhs`, assuming overflow /// Unchecked integer addition. Computes `self + rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_add(y)` is semantically equivalent to calling
/// `x.`[`checked_add`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_add`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self + rhs > ", stringify!($SelfT), "::MAX` or `self + rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_add`] would return `None`. /// i.e. when [`checked_add`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")] #[doc = concat!("[`checked_add`]: ", stringify!($SelfT), "::checked_add")]
#[unstable( #[doc = concat!("[`wrapping_add`]: ", stringify!($SelfT), "::wrapping_add")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {
@ -644,21 +648,25 @@ pub const fn strict_sub(self, rhs: Self) -> Self {
/// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_sub(y)` is semantically equivalent to calling
/// `x.`[`checked_sub`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_sub`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self - rhs > ", stringify!($SelfT), "::MAX` or `self - rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_sub`] would return `None`. /// i.e. when [`checked_sub`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")] #[doc = concat!("[`checked_sub`]: ", stringify!($SelfT), "::checked_sub")]
#[unstable( #[doc = concat!("[`wrapping_sub`]: ", stringify!($SelfT), "::wrapping_sub")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {
@ -726,21 +734,25 @@ pub const fn strict_mul(self, rhs: Self) -> Self {
/// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow
/// cannot occur. /// cannot occur.
/// ///
/// Calling `x.unchecked_mul(y)` is semantically equivalent to calling
/// `x.`[`checked_mul`]`(y).`[`unwrap_unchecked`]`()`.
///
/// If you're just trying to avoid the panic in debug mode, then **do not**
/// use this. Instead, you're looking for [`wrapping_mul`].
///
/// # Safety /// # Safety
/// ///
/// This results in undefined behavior when /// This results in undefined behavior when
#[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")] #[doc = concat!("`self * rhs > ", stringify!($SelfT), "::MAX` or `self * rhs < ", stringify!($SelfT), "::MIN`,")]
/// i.e. when [`checked_mul`] would return `None`. /// i.e. when [`checked_mul`] would return `None`.
/// ///
/// [`unwrap_unchecked`]: Option::unwrap_unchecked
#[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")] #[doc = concat!("[`checked_mul`]: ", stringify!($SelfT), "::checked_mul")]
#[unstable( #[doc = concat!("[`wrapping_mul`]: ", stringify!($SelfT), "::wrapping_mul")]
feature = "unchecked_math", #[stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
reason = "niche optimization path", #[rustc_const_stable(feature = "unchecked_math", since = "CURRENT_RUSTC_VERSION")]
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 = "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 {

View File

@ -1038,8 +1038,6 @@ pub const fn guaranteed_ne(self, other: *const T) -> Option<bool>
#[stable(feature = "pointer_methods", since = "1.26.0")] #[stable(feature = "pointer_methods", since = "1.26.0")]
#[must_use = "returns a new pointer rather than modifying its argument"] #[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
// We could always go back to wrapping if unchecked becomes unacceptable
#[rustc_allow_const_fn_unstable(const_int_unchecked_arith)]
#[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 sub(self, count: usize) -> Self pub const unsafe fn sub(self, count: usize) -> Self

View File

@ -1123,8 +1123,6 @@ pub const fn guaranteed_ne(self, other: *mut T) -> Option<bool>
#[stable(feature = "pointer_methods", since = "1.26.0")] #[stable(feature = "pointer_methods", since = "1.26.0")]
#[must_use = "returns a new pointer rather than modifying its argument"] #[must_use = "returns a new pointer rather than modifying its argument"]
#[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")] #[rustc_const_stable(feature = "const_ptr_offset", since = "1.61.0")]
// We could always go back to wrapping if unchecked becomes unacceptable
#[rustc_allow_const_fn_unstable(const_int_unchecked_arith)]
#[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 sub(self, count: usize) -> Self pub const unsafe fn sub(self, count: usize) -> Self

View File

@ -702,8 +702,6 @@ pub const fn cast<U>(self) -> NonNull<U> {
#[unstable(feature = "non_null_convenience", issue = "117691")] #[unstable(feature = "non_null_convenience", issue = "117691")]
#[rustc_const_unstable(feature = "non_null_convenience", issue = "117691")] #[rustc_const_unstable(feature = "non_null_convenience", issue = "117691")]
#[must_use = "returns a new pointer rather than modifying its argument"] #[must_use = "returns a new pointer rather than modifying its argument"]
// We could always go back to wrapping if unchecked becomes unacceptable
#[rustc_allow_const_fn_unstable(const_int_unchecked_arith)]
#[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 sub(self, count: usize) -> Self pub const unsafe fn sub(self, count: usize) -> Self

View File

@ -1,5 +1,5 @@
#![feature(core_intrinsics)] #![feature(core_intrinsics)]
#![feature(const_int_unchecked_arith)]
use std::intrinsics; use std::intrinsics;