Skip to content
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

Fix loop contracts transformation when loops in branching #3640

Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions kani-compiler/src/kani_middle/transform/loop_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl TransformPass for LoopContractPass {
let mut loop_queue: VecDeque<BasicBlockIdx> = VecDeque::new();
queue.push_back(0);

while let Some(bb_idx) = queue.pop_front().or(loop_queue.pop_front()) {
while let Some(bb_idx) = queue.pop_front().or_else(|| loop_queue.pop_front()) {
qinheping marked this conversation as resolved.
Show resolved Hide resolved
visited.insert(bb_idx);

let terminator = new_body.blocks()[bb_idx].terminator.clone();
Expand All @@ -126,9 +126,11 @@ impl TransformPass for LoopContractPass {
// the visiting queue.
for to_visit in terminator.successors() {
if !visited.contains(&to_visit) {
let target_queue =
if is_loop_head { &mut loop_queue } else { &mut queue };
target_queue.push_back(to_visit);
if is_loop_head {
loop_queue.push_back(to_visit);
} else {
queue.push_back(to_visit)
};
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions tests/expected/loop-contract/multiple_loops.rs
qinheping marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
fn multiple_loops() {
let mut x: u8 = kani::any_where(|i| *i >= 10);

#[kani::loop_invariant(x >= 5)]
while x > 5 {
x = x - 1;
if x != 20 {
#[kani::loop_invariant(x >= 5)]
while x > 5 {
x = x - 1;
}
}

assert!(x == 5);
assert!(x == 5 || x == 20);

#[kani::loop_invariant(x >= 2)]
while x > 2 {
Expand Down
Loading