From 3f86e7ea9169c136bd22816356aef74378dc71e9 Mon Sep 17 00:00:00 2001 From: Bardi Harborow Date: Sat, 5 Dec 2026 05:44:34 +1100 Subject: [PATCH] Add contracts and autoharness for the unsigned funnel shifts `unchecked_funnel_shl` and `unchecked_funnel_shr` are public, stable (since 1.101.0) unsafe methods in `core::num` that had no contract and no proof. Add `#[requires(n < <$ActualT>::BITS)]` to both. This is the condition their `assert_unsafe_precondition!` checks and the one their documentation states, and `unchecked_shl` uses the same contract. Verify both with autoharness for `u8`, `u16`, `u32`, `u64`, `u128` and `usize`, over all inputs that satisfy the contract. The signed integer types do not have funnel shifts. --- .github/workflows/kani.yml | 6 ++++++ library/core/src/num/uint_macros.rs | 2 ++ 2 files changed, 8 insertions(+) diff --git a/.github/workflows/kani.yml b/.github/workflows/kani.yml index d93b0ecc26b16..18bcf78e6c630 100644 --- a/.github/workflows/kani.yml +++ b/.github/workflows/kani.yml @@ -151,6 +151,12 @@ jobs: --include-pattern "num::::unchecked_add" \ --include-pattern "num::::unchecked_add" \ --include-pattern "num::::unchecked_add" \ + --include-pattern "num::::unchecked_funnel_sh" \ + --include-pattern "num::::unchecked_funnel_sh" \ + --include-pattern "num::::unchecked_funnel_sh" \ + --include-pattern "num::::unchecked_funnel_sh" \ + --include-pattern "num::::unchecked_funnel_sh" \ + --include-pattern "num::::unchecked_funnel_sh" \ --include-pattern "num::::unchecked_neg" \ --include-pattern "num::::unchecked_neg" \ --include-pattern "num::::unchecked_neg" \ diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index f08a6c020faa6..93c35a5c7da0f 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -729,6 +729,7 @@ macro_rules! uint_impl { #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] #[track_caller] + #[requires(n < <$ActualT>::BITS)] pub const unsafe fn unchecked_funnel_shl(self, right: Self, n: u32) -> Self { assert_unsafe_precondition!( check_language_ub, @@ -756,6 +757,7 @@ macro_rules! uint_impl { #[must_use = "this returns the result of the operation, without modifying the original"] #[inline(always)] #[track_caller] + #[requires(n < <$ActualT>::BITS)] pub const unsafe fn unchecked_funnel_shr(self, right: Self, n: u32) -> Self { assert_unsafe_precondition!( check_language_ub,