Skip to content

Commit 3bb48f0

Browse files
thanhnguyen-awstautschnigfeliperodri
authored
Add loop_invariant and harness for array reverse (#430)
This PR adds loop_invariant and harness for array reverse. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. --------- Co-authored-by: Michael Tautschnig <[email protected]> Co-authored-by: Felipe R. Monteiro <[email protected]>
1 parent c0c576a commit 3bb48f0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

library/core/src/slice/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,13 @@ impl<T> [T] {
10111011
let (b, _) = b.split_at_mut(n);
10121012

10131013
let mut i = 0;
1014+
#[safety::loop_invariant(i <= n)]
1015+
#[cfg_attr(kani,
1016+
kani::loop_modifies(
1017+
unsafe {slice::from_raw_parts_mut(a.as_mut_ptr(), n)},
1018+
unsafe {slice::from_raw_parts_mut(b.as_mut_ptr(), n)},
1019+
&i)
1020+
)]
10141021
while i < n {
10151022
mem::swap(&mut a[i], &mut b[n - 1 - i]);
10161023
i += 1;
@@ -5495,4 +5502,10 @@ mod verify {
54955502
gen_align_to_mut_harnesses!(align_to_mut_from_bool, bool);
54965503
gen_align_to_mut_harnesses!(align_to_mut_from_char, char);
54975504
gen_align_to_mut_harnesses!(align_to_mut_from_unit, ());
5505+
5506+
#[kani::proof]
5507+
fn check_reverse() {
5508+
let mut a: [u8; 100] = kani::any();
5509+
a.reverse();
5510+
}
54985511
}

0 commit comments

Comments
 (0)