From 35571f26d45d1884cc08154472944c103937baa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Sanz=20Gonz=C3=A1lez?= <80487270+hsanzg@users.noreply.github.com> Date: Sat, 17 Oct 2026 20:54:02 +0200 Subject: [PATCH] Stabilize unchecked `nonzero_ops` The original `nonzero_ops` feature (https://github.com/rust-lang/rust/issues/84186) was partly stabilized as `nonzero_checked_ops`, leaving the `NonZero::unchecked_{add,mul}` operations feature-gated until their primitive counterparts (`{i,u}*::unchecked_{add,mul}`) were stabilized. The latter operations were (const) stabilized in 1.79, but the unchecked `NonZero` operations have stayed in limbo until now. --- library/core/src/num/nonzero.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index d0cc0e3b31da7..444a4f59d0729 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -1280,8 +1280,6 @@ macro_rules! nonzero_integer { /// # Examples /// /// ``` - /// #![feature(nonzero_ops)] - /// /// # use std::num::NonZero; /// # /// # fn main() { test().unwrap(); } @@ -1295,12 +1293,17 @@ macro_rules! nonzero_integer { /// ``` /// /// [`checked_mul`]: Self::checked_mul - #[unstable(feature = "nonzero_ops", issue = "84186")] + #[stable(feature = "nonzero_ops", since = "CURRENT_RUSTC_VERSION")] + #[rustc_const_stable(feature = "nonzero_ops", since = "CURRENT_RUSTC_VERSION")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] pub const unsafe fn unchecked_mul(self, other: Self) -> Self { - // SAFETY: The caller ensures there is no overflow. + // SAFETY: + // - the caller promises that the multiplication does not overflow; + // - `self` and `other` are non-zero; + // - the only way to get zero from a multiplication without overflow + // is for either factor to be zero. unsafe { Self::new_unchecked(self.get().unchecked_mul(other.get())) } } @@ -1872,8 +1875,6 @@ macro_rules! nonzero_integer_signedness_dependent_methods { /// # Examples /// /// ``` - /// #![feature(nonzero_ops)] - /// /// # use std::num::NonZero; /// # /// # fn main() { test().unwrap(); } @@ -1887,12 +1888,17 @@ macro_rules! nonzero_integer_signedness_dependent_methods { /// ``` /// /// [`checked_add`]: Self::checked_add - #[unstable(feature = "nonzero_ops", issue = "84186")] + #[stable(feature = "nonzero_ops", since = "CURRENT_RUSTC_VERSION")] + #[rustc_const_stable(feature = "nonzero_ops", since = "CURRENT_RUSTC_VERSION")] #[must_use = "this returns the result of the operation, \ without modifying the original"] #[inline] pub const unsafe fn unchecked_add(self, other: $Int) -> Self { - // SAFETY: The caller ensures there is no overflow. + // SAFETY: + // - the caller promises that the addition does not overflow; + // - `self` is non-zero; + // - the only way to get zero from an addition without overflow is + // for both addends to be zero. unsafe { Self::new_unchecked(self.get().unchecked_add(other)) } }