Skip to content

Commit 9e55863

Browse files
committed
iter: add kani proof for next_unchecked (rust-lang#280)
Adds `kani` proof and function contract for `UncheckedIterator::next_unchecked`. Ensures that the safety invariants are held using the model checker.
1 parent 3bb48f0 commit 9e55863

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

library/core/src/iter/traits/unchecked_iterator.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) trait UncheckedIterator: TrustedLen {
3131
/// point you might want to implement this manually instead.
3232
#[unstable(feature = "trusted_len_next_unchecked", issue = "37572")]
3333
#[inline]
34-
#[requires(self.size_hint().0 > 0)]
34+
#[requires(self.size_hint().0 != 0 && self.size_hint().1 != Some(0))]
3535
#[cfg_attr(kani, kani::modifies(self))]
3636
unsafe fn next_unchecked(&mut self) -> Self::Item {
3737
let opt = self.next();
@@ -40,3 +40,17 @@ pub(crate) trait UncheckedIterator: TrustedLen {
4040
unsafe { opt.unwrap_unchecked() }
4141
}
4242
}
43+
44+
#[cfg(kani)]
45+
#[kani::proof]
46+
fn verify_next_unchecked() {
47+
let arr: [u8; 1] = [kani::any(); 1];
48+
49+
let mut it = arr.iter();
50+
let (lo_hint, hi_hint) = it.size_hint();
51+
52+
kani::assume(lo_hint != 0);
53+
kani::assume(hi_hint != Some(0));
54+
55+
let _ = unsafe { it.next_unchecked() };
56+
}

0 commit comments

Comments
 (0)