Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,3 +2265,69 @@ mod verify {
nonzero_check!(u128, core::num::NonZeroU128, nonzero_check_new_unchecked_for_u128);
nonzero_check!(usize, core::num::NonZeroUsize, nonzero_check_new_unchecked_for_usize);
}

#[cfg(kani)]
mod macro_nonzero_check_max {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Last thing -- instead of having a module for each function, can you move all of these harnesses inside of mod verify? We're trying to keep a consistent format for these proofs across challenges, and that's how we've been doing it so far.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have done this in NonZero (rotate_left & rotate_right) Proof #202

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but could you please also do it here?

use super::*;
macro_rules! nonzero_check_max {
($nonzero_type:ty, $nonzero_check_max_for:ident) => {
#[kani::proof]
pub fn $nonzero_check_max_for() {
let x: $nonzero_type = kani::any();
let y: $nonzero_type = kani::any();
let result = x.max(y);
if x > y {
assert!(result == x);
} else {
assert!(result == y);
}
}
};
}

nonzero_check_max!(core::num::NonZeroI8, nonzero_check_max_for_i8);
nonzero_check_max!(core::num::NonZeroI16, nonzero_check_max_for_i16);
nonzero_check_max!(core::num::NonZeroI32, nonzero_check_max_for_i32);
nonzero_check_max!(core::num::NonZeroI64, nonzero_check_max_for_i64);
nonzero_check_max!(core::num::NonZeroI128, nonzero_check_max_for_i128);
nonzero_check_max!(core::num::NonZeroIsize, nonzero_check_max_for_isize);
nonzero_check_max!(core::num::NonZeroU8, nonzero_check_max_for_u8);
nonzero_check_max!(core::num::NonZeroU16, nonzero_check_max_for_u16);
nonzero_check_max!(core::num::NonZeroU32, nonzero_check_max_for_u32);
nonzero_check_max!(core::num::NonZeroU64, nonzero_check_max_for_u64);
nonzero_check_max!(core::num::NonZeroU128, nonzero_check_max_for_u128);
nonzero_check_max!(core::num::NonZeroUsize, nonzero_check_max_for_usize);
}

#[cfg(kani)]
mod macro_nonzero_check_min {
use super::*;
macro_rules! nonzero_check_min {
($nonzero_type:ty, $nonzero_check_min_for:ident) => {
#[kani::proof]
pub fn $nonzero_check_min_for() {
let x: $nonzero_type = kani::any();
let y: $nonzero_type = kani::any();
let result = x.min(y);
if x < y {
assert!(result == x);
} else {
assert!(result == y);
}
}
};
}

nonzero_check_min!(core::num::NonZeroI8, nonzero_check_min_for_i8);
nonzero_check_min!(core::num::NonZeroI16, nonzero_check_min_for_i16);
nonzero_check_min!(core::num::NonZeroI32, nonzero_check_min_for_i32);
nonzero_check_min!(core::num::NonZeroI64, nonzero_check_min_for_i64);
nonzero_check_min!(core::num::NonZeroI128, nonzero_check_min_for_i128);
nonzero_check_min!(core::num::NonZeroIsize, nonzero_check_min_for_isize);
nonzero_check_min!(core::num::NonZeroU8, nonzero_check_min_for_u8);
nonzero_check_min!(core::num::NonZeroU16, nonzero_check_min_for_u16);
nonzero_check_min!(core::num::NonZeroU32, nonzero_check_min_for_u32);
nonzero_check_min!(core::num::NonZeroU64, nonzero_check_min_for_u64);
nonzero_check_min!(core::num::NonZeroU128, nonzero_check_min_for_u128);
nonzero_check_min!(core::num::NonZeroUsize, nonzero_check_min_for_usize);
}