From b63999420d7898fcb1455d725f6704cc04672cf3 Mon Sep 17 00:00:00 2001 From: Vrtgs Date: Tue, 14 Oct 2025 18:47:50 +0300 Subject: [PATCH 1/3] update isolate_highest_one for NonZero --- library/core/src/num/nonzero.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index fcdb65bd45c95..a97b12c571172 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -660,12 +660,15 @@ macro_rules! nonzero_integer { without modifying the original"] #[inline(always)] pub const fn isolate_highest_one(self) -> Self { - let n = self.get() & (((1 as $Int) << (<$Int>::BITS - 1)).wrapping_shr(self.leading_zeros())); - // SAFETY: // `self` is non-zero, so masking to preserve only the most // significant set bit will result in a non-zero `n`. - unsafe { NonZero::new_unchecked(n) } + // and self.leading_zeros() is always < $INT::BITS since + // at least one of the bits in the number is not zero + unsafe { + let bit = (((1 as $Int) << (<$Int>::BITS - 1)).unchecked_shr(self.leading_zeros())); + NonZero::new_unchecked(bit) + } } /// Returns `self` with only the least significant bit set. From a04e2f3a415c168ce709a76b309aa76f9fc39638 Mon Sep 17 00:00:00 2001 From: Vrtgs Date: Tue, 14 Oct 2025 22:16:18 +0300 Subject: [PATCH 2/3] fix isolate_highest_one for signed NonZero --- library/core/src/num/nonzero.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index a97b12c571172..93a180954439f 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -666,8 +666,8 @@ macro_rules! nonzero_integer { // and self.leading_zeros() is always < $INT::BITS since // at least one of the bits in the number is not zero unsafe { - let bit = (((1 as $Int) << (<$Int>::BITS - 1)).unchecked_shr(self.leading_zeros())); - NonZero::new_unchecked(bit) + let bit = (((1 as $UInt) << (<$UInt>::BITS - 1)).unchecked_shr(self.leading_zeros())); + NonZero::new_unchecked(bit as $Int) } } From 0ad0cae9a7130f7f4caffe29cab92190c970c69d Mon Sep 17 00:00:00 2001 From: Vrtgs Date: Tue, 14 Oct 2025 22:34:02 +0300 Subject: [PATCH 3/3] fix compiler error where $UInt was used in place of $Uint --- library/core/src/num/nonzero.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/num/nonzero.rs b/library/core/src/num/nonzero.rs index 93a180954439f..cb8838f1eece6 100644 --- a/library/core/src/num/nonzero.rs +++ b/library/core/src/num/nonzero.rs @@ -666,7 +666,7 @@ macro_rules! nonzero_integer { // and self.leading_zeros() is always < $INT::BITS since // at least one of the bits in the number is not zero unsafe { - let bit = (((1 as $UInt) << (<$UInt>::BITS - 1)).unchecked_shr(self.leading_zeros())); + let bit = (((1 as $Uint) << (<$Uint>::BITS - 1)).unchecked_shr(self.leading_zeros())); NonZero::new_unchecked(bit as $Int) } }