-
Notifications
You must be signed in to change notification settings - Fork 66
Challenge 24: Verify safety of Vec IntoIter functions #570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -541,3 +541,165 @@ unsafe impl<T> AsVecIntoIter for IntoIter<T> { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[cfg(kani)] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| #[unstable(feature = "kani", issue = "none")] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mod verify { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use super::*; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::vec::Vec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use crate::vec::Vec; | |
| use crate::vec::Vec; | |
| use core::kani; |
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These Kani harnesses use only fixed, concrete inputs (e.g., Vec::from(&[1,2,3])), which proves behavior only for that single case and won’t exercise important edge cases (empty vec, partial consumption, ZSTs, OOM paths, etc.). Other Kani proofs in this repository use nondeterministic values (e.g., kani::Arbitrary::any_array() + kani::any_where(...); see library/alloc/src/vec/mod.rs’s verify module) to get meaningful safety coverage. Consider switching these harnesses to use kani::Arbitrary/kani::any_* inputs with appropriate assumptions to better match the PR’s stated goal of verifying safety of these APIs.
| let v = Vec::from(&[1i32, 2, 3]); | |
| let it = v.into_iter(); | |
| assert!(it.as_slice().len() == N); | |
| } | |
| #[kani::proof] | |
| fn verify_as_mut_slice() { | |
| let v = Vec::from(&[1i32, 2, 3]); | |
| let mut it = v.into_iter(); | |
| it.as_mut_slice()[0] = 42; | |
| } | |
| #[kani::proof] | |
| fn verify_next() { | |
| let v = Vec::from(&[1i32, 2, 3]); | |
| let mut it = v.into_iter(); | |
| assert!(it.next() == Some(1)); | |
| } | |
| #[kani::proof] | |
| fn verify_size_hint() { | |
| let v = Vec::from(&[1i32, 2, 3]); | |
| let it = v.into_iter(); | |
| assert!(it.size_hint() == (N, Some(N))); | |
| // Use arbitrary elements and an arbitrary length up to N | |
| let data: [i32; N] = kani::Arbitrary::any_array(); | |
| let len: usize = kani::any(); | |
| kani::assume(len <= N); | |
| let mut v = Vec::with_capacity(N); | |
| for i in 0..len { | |
| v.push(data[i]); | |
| } | |
| let it = v.into_iter(); | |
| let slice = it.as_slice(); | |
| assert!(slice.len() == len); | |
| } | |
| #[kani::proof] | |
| fn verify_as_mut_slice() { | |
| // Use arbitrary elements and an arbitrary length up to N | |
| let data: [i32; N] = kani::Arbitrary::any_array(); | |
| let len: usize = kani::any(); | |
| kani::assume(len <= N); | |
| let mut v = Vec::with_capacity(N); | |
| for i in 0..len { | |
| v.push(data[i]); | |
| } | |
| let mut it = v.into_iter(); | |
| if len > 0 { | |
| it.as_mut_slice()[0] = 42; | |
| } | |
| } | |
| #[kani::proof] | |
| fn verify_next() { | |
| // Require at least one element so that next() returns Some(_) | |
| let data: [i32; N] = kani::Arbitrary::any_array(); | |
| let len: usize = kani::any(); | |
| kani::assume(len <= N && len > 0); | |
| let first = data[0]; | |
| let mut v = Vec::with_capacity(N); | |
| for i in 0..len { | |
| v.push(data[i]); | |
| } | |
| let mut it = v.into_iter(); | |
| assert!(it.next() == Some(first)); | |
| } | |
| #[kani::proof] | |
| fn verify_size_hint() { | |
| // For a fresh IntoIter, size_hint should match the remaining length | |
| let data: [i32; N] = kani::Arbitrary::any_array(); | |
| let len: usize = kani::any(); | |
| kani::assume(len <= N); | |
| let mut v = Vec::with_capacity(N); | |
| for i in 0..len { | |
| v.push(data[i]); | |
| } | |
| let it = v.into_iter(); | |
| assert!(it.size_hint() == (len, Some(len))); |
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
advance_by returns a Result indicating whether the requested number of steps was fully advanced. Currently the result is ignored, so the harness won’t fail if advance_by(2) unexpectedly returns Err(_). Consider asserting it.advance_by(2).is_ok() (or unwrap()), then checking the postcondition on len().
| let _ = it.advance_by(2); | |
| assert!(it.advance_by(2).is_ok()); |
Copilot
AI
Mar 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
advance_back_by returns a Result indicating whether the requested number of steps was fully advanced. The harness ignores it, so it wouldn’t catch an unexpected Err(_). Consider asserting the return value is Ok(()) (or unwrapping) before checking len().
| let _ = it.advance_back_by(2); | |
| assert!(it.advance_back_by(2).is_ok()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use super::*;appears unused within thisverifymodule (all uses are via inherent methods andVecfromcrate::vec::Vec). In the standard library build, unused imports are typically treated as warnings/errors, so consider removing the glob import or importing only the specific items you actually reference by name.