Skip to content

Commit a0daec5

Browse files
committed
Use autoharness for num::nonzero::NonZero::<*>::count_ones
1 parent bc70538 commit a0daec5

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

.github/workflows/kani.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ jobs:
137137
--include-pattern "num::<impl u64>::wrapping_sh" \
138138
--include-pattern "num::<impl u128>::wrapping_sh" \
139139
--include-pattern "num::<impl usize>::wrapping_sh" \
140+
--include-pattern "num::nonzero::NonZero::<i8>::count_ones" \
141+
--include-pattern "num::nonzero::NonZero::<i16>::count_ones" \
142+
--include-pattern "num::nonzero::NonZero::<i32>::count_ones" \
143+
--include-pattern "num::nonzero::NonZero::<i64>::count_ones" \
144+
--include-pattern "num::nonzero::NonZero::<i128>::count_ones" \
145+
--include-pattern "num::nonzero::NonZero::<isize>::count_ones" \
146+
--include-pattern "num::nonzero::NonZero::<u8>::count_ones" \
147+
--include-pattern "num::nonzero::NonZero::<u16>::count_ones" \
148+
--include-pattern "num::nonzero::NonZero::<u32>::count_ones" \
149+
--include-pattern "num::nonzero::NonZero::<u64>::count_ones" \
150+
--include-pattern "num::nonzero::NonZero::<u128>::count_ones" \
151+
--include-pattern "num::nonzero::NonZero::<usize>::count_ones" \
140152
--include-pattern ptr::align_offset::mod_inv \
141153
--include-pattern ptr::alignment::Alignment::as_nonzero \
142154
--include-pattern ptr::alignment::Alignment::as_usize \

library/core/src/num/nonzero.rs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ macro_rules! nonzero_integer {
721721
#[must_use = "this returns the result of the operation, \
722722
without modifying the original"]
723723
#[inline(always)]
724+
#[ensures(|result| result.get() > 0)]
724725
pub const fn count_ones(self) -> NonZero<u32> {
725726
// SAFETY:
726727
// `self` is non-zero, which means it has at least one bit set, which means
@@ -2572,32 +2573,6 @@ mod verify {
25722573
nonzero_check_clamp_panic!(core::num::NonZeroU128, nonzero_check_clamp_panic_for_u128);
25732574
nonzero_check_clamp_panic!(core::num::NonZeroUsize, nonzero_check_clamp_panic_for_usize);
25742575

2575-
macro_rules! nonzero_check_count_ones {
2576-
($nonzero_type:ty, $nonzero_check_count_ones_for:ident) => {
2577-
#[kani::proof]
2578-
pub fn $nonzero_check_count_ones_for() {
2579-
let x: $nonzero_type = kani::any();
2580-
let result = x.count_ones();
2581-
// Since x is non-zero, count_ones should never return 0
2582-
assert!(result.get() > 0);
2583-
}
2584-
};
2585-
}
2586-
2587-
// Use the macro to generate different versions of the function for multiple types
2588-
nonzero_check_count_ones!(core::num::NonZeroI8, nonzero_check_count_ones_for_i8);
2589-
nonzero_check_count_ones!(core::num::NonZeroI16, nonzero_check_count_ones_for_i16);
2590-
nonzero_check_count_ones!(core::num::NonZeroI32, nonzero_check_count_ones_for_i32);
2591-
nonzero_check_count_ones!(core::num::NonZeroI64, nonzero_check_count_ones_for_i64);
2592-
nonzero_check_count_ones!(core::num::NonZeroI128, nonzero_check_count_ones_for_i128);
2593-
nonzero_check_count_ones!(core::num::NonZeroIsize, nonzero_check_count_ones_for_isize);
2594-
nonzero_check_count_ones!(core::num::NonZeroU8, nonzero_check_count_ones_for_u8);
2595-
nonzero_check_count_ones!(core::num::NonZeroU16, nonzero_check_count_ones_for_u16);
2596-
nonzero_check_count_ones!(core::num::NonZeroU32, nonzero_check_count_ones_for_u32);
2597-
nonzero_check_count_ones!(core::num::NonZeroU64, nonzero_check_count_ones_for_u64);
2598-
nonzero_check_count_ones!(core::num::NonZeroU128, nonzero_check_count_ones_for_u128);
2599-
nonzero_check_count_ones!(core::num::NonZeroUsize, nonzero_check_count_ones_for_usize);
2600-
26012576
macro_rules! nonzero_check_rotate_left_and_right {
26022577
($nonzero_type:ty, $nonzero_check_rotate_for:ident) => {
26032578
#[kani::proof]

0 commit comments

Comments
 (0)