Skip to content

Commit f079d4c

Browse files
committed
Use autoharness for num::nonzero::NonZero::<*>::rotate_{left,right}
1 parent a0daec5 commit f079d4c

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

.github/workflows/kani.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ jobs:
149149
--include-pattern "num::nonzero::NonZero::<u64>::count_ones" \
150150
--include-pattern "num::nonzero::NonZero::<u128>::count_ones" \
151151
--include-pattern "num::nonzero::NonZero::<usize>::count_ones" \
152+
--include-pattern "num::nonzero::NonZero::<i8>::rotate_" \
153+
--include-pattern "num::nonzero::NonZero::<i16>::rotate_" \
154+
--include-pattern "num::nonzero::NonZero::<i32>::rotate_" \
155+
--include-pattern "num::nonzero::NonZero::<i64>::rotate_" \
156+
--include-pattern "num::nonzero::NonZero::<i128>::rotate_" \
157+
--include-pattern "num::nonzero::NonZero::<isize>::rotate_" \
158+
--include-pattern "num::nonzero::NonZero::<u8>::rotate_" \
159+
--include-pattern "num::nonzero::NonZero::<u16>::rotate_" \
160+
--include-pattern "num::nonzero::NonZero::<u32>::rotate_" \
161+
--include-pattern "num::nonzero::NonZero::<u64>::rotate_" \
162+
--include-pattern "num::nonzero::NonZero::<u128>::rotate_" \
163+
--include-pattern "num::nonzero::NonZero::<usize>::rotate_" \
152164
--include-pattern ptr::align_offset::mod_inv \
153165
--include-pattern ptr::alignment::Alignment::as_nonzero \
154166
--include-pattern ptr::alignment::Alignment::as_usize \

library/core/src/num/nonzero.rs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,8 @@ macro_rules! nonzero_integer {
755755
#[must_use = "this returns the result of the operation, \
756756
without modifying the original"]
757757
#[inline(always)]
758+
#[ensures(|result| result.get() > 0)]
759+
#[ensures(|result| result.rotate_right(n).get() == old(self).get())]
758760
pub const fn rotate_left(self, n: u32) -> Self {
759761
let result = self.get().rotate_left(n);
760762
// SAFETY: Rotating bits preserves the property int > 0.
@@ -788,6 +790,8 @@ macro_rules! nonzero_integer {
788790
#[must_use = "this returns the result of the operation, \
789791
without modifying the original"]
790792
#[inline(always)]
793+
#[ensures(|result| result.get() > 0)]
794+
#[ensures(|result| result.rotate_left(n).get() == old(self).get())]
791795
pub const fn rotate_right(self, n: u32) -> Self {
792796
let result = self.get().rotate_right(n);
793797
// SAFETY: Rotating bits preserves the property int > 0.
@@ -2572,30 +2576,4 @@ mod verify {
25722576
nonzero_check_clamp_panic!(core::num::NonZeroU64, nonzero_check_clamp_panic_for_u64);
25732577
nonzero_check_clamp_panic!(core::num::NonZeroU128, nonzero_check_clamp_panic_for_u128);
25742578
nonzero_check_clamp_panic!(core::num::NonZeroUsize, nonzero_check_clamp_panic_for_usize);
2575-
2576-
macro_rules! nonzero_check_rotate_left_and_right {
2577-
($nonzero_type:ty, $nonzero_check_rotate_for:ident) => {
2578-
#[kani::proof]
2579-
pub fn $nonzero_check_rotate_for() {
2580-
let x: $nonzero_type = kani::any();
2581-
let n: u32 = kani::any();
2582-
let result = x.rotate_left(n).rotate_right(n);
2583-
assert!(result == x);
2584-
}
2585-
};
2586-
}
2587-
2588-
// Use the macro to generate different versions of the function for multiple types
2589-
nonzero_check_rotate_left_and_right!(core::num::NonZeroI8, nonzero_check_rotate_for_i8);
2590-
nonzero_check_rotate_left_and_right!(core::num::NonZeroI16, nonzero_check_rotate_for_i16);
2591-
nonzero_check_rotate_left_and_right!(core::num::NonZeroI32, nonzero_check_rotate_for_i32);
2592-
nonzero_check_rotate_left_and_right!(core::num::NonZeroI64, nonzero_check_rotate_for_i64);
2593-
nonzero_check_rotate_left_and_right!(core::num::NonZeroI128, nonzero_check_rotate_for_i128);
2594-
nonzero_check_rotate_left_and_right!(core::num::NonZeroIsize, nonzero_check_rotate_for_isize);
2595-
nonzero_check_rotate_left_and_right!(core::num::NonZeroU8, nonzero_check_rotate_for_u8);
2596-
nonzero_check_rotate_left_and_right!(core::num::NonZeroU16, nonzero_check_rotate_for_u16);
2597-
nonzero_check_rotate_left_and_right!(core::num::NonZeroU32, nonzero_check_rotate_for_u32);
2598-
nonzero_check_rotate_left_and_right!(core::num::NonZeroU64, nonzero_check_rotate_for_u64);
2599-
nonzero_check_rotate_left_and_right!(core::num::NonZeroU128, nonzero_check_rotate_for_u128);
2600-
nonzero_check_rotate_left_and_right!(core::num::NonZeroUsize, nonzero_check_rotate_for_usize);
26012579
}

0 commit comments

Comments
 (0)