Skip to content
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
17 changes: 12 additions & 5 deletions library/kani_macros/src/sysroot/loop_contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,19 @@ fn transform_break_continue(block: &mut Block) {

fn while_let_rewrite(loopexpr: Stmt) -> Stmt {
if let Stmt::Expr(ref expr, _) = loopexpr
&& let Expr::While(ExprWhile { cond, body, .. }) = expr
&& let Expr::While(ExprWhile { attrs, label, cond, body, .. }) = expr
&& let Expr::Let(ref let_expr) = **cond
{
let pat = &let_expr.pat;
let scrutinee = &let_expr.expr;

// Transform to loop with match
// Transform to loop with match.
// Keep the label and the remaining attributes of the loop (e.g., a
// `#[kani::loop_modifies]` or `#[kani::loop_decreases]` written after
// `#[kani::loop_invariant]`).
return parse_quote! {
loop {
#(#attrs)*
Comment thread
Talha-Dmr marked this conversation as resolved.
Resolved
#label loop {
match #scrutinee {
#pat => #body,
_ => break,
Expand Down Expand Up @@ -308,6 +312,7 @@ pub fn transform_for_to_loop(
loop_id: &str,
) -> (Stmt, Option<ForLoopExtraStmts>) {
// Extract components from the for loop
let label = for_loop.label;
let pat = *for_loop.pat;
let expr = for_loop.expr;
let body = for_loop.body;
Expand Down Expand Up @@ -371,9 +376,11 @@ pub fn transform_for_to_loop(
// Add the original loop body statements
new_body_stmts.extend(body.stmts.iter().cloned());

// Create the final expression with the iterator initialization
// Create the final expression with the iterator initialization.
// Keep the label of the loop. Attributes after the invariant are intentionally not carried
// over yet: see #4929 (and #4940 for why a loop_modifies clause would then fail).
let loop_loop: Stmt = parse_quote! {
while (#kani_index < #kani_iter_len) {
#label while (#kani_index < #kani_iter_len) {
#(#new_body_stmts)*
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Complete - 2 successfully verified harnesses, 0 failures, 2 total.
38 changes: 38 additions & 0 deletions tests/expected/loop-contract/loop_contract_labeled_loops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// kani-flags: -Z loop-contracts

//! Check that a loop invariant can be attached to labeled `while let` and `for` loops.
//! The rewrites of these loops used to drop the label, so a `break` or `continue` that names
//! the label failed to compile.

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

#[kani::proof]
fn labeled_while_let() {
let mut k: u8 = kani::any();
#[kani::loop_invariant(true)]
'outer: while let Some(n) = k.checked_sub(1) {
k = n;
if k == 3 {
break 'outer;
}
}
assert!(k == 0 || k == 3);
}

#[kani::proof]
fn labeled_for() {
let a: [u8; 5] = kani::any();
let mut sum: u32 = 0;
#[kani::loop_invariant(sum <= (kani::index as u32 * u8::MAX as u32))]
'outer: for x in a {
if x == 0 {
continue 'outer;
}
sum = sum + (x as u32);
}
assert!(sum <= 5 * 255);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- Status: FAILURE\
- Description: "Check that j is assignable"\
in function invariant_then_modifies

- Status: FAILURE\
- Description: "Check that j is assignable"\
in function modifies_then_invariant

VERIFICATION:- FAILED
42 changes: 42 additions & 0 deletions tests/expected/loop-contract/while_let_loop_modifies_fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

// kani-flags: -Z loop-contracts

//! Check that `#[kani::loop_modifies]` is applied to a `while let` loop whether it is written
//! before or after `#[kani::loop_invariant]`.
//! Both loops write `j`, which is not in the modifies clause, so the assigns check for `j` must
//! fail in both harnesses. The `while let` rewrite used to drop the attributes that follow
//! `#[kani::loop_invariant]`, so `invariant_then_modifies` verified successfully with an
//! inferred assigns clause.

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

#[kani::proof]
fn invariant_then_modifies() {
let mut k: u8 = kani::any();
let mut i: u8 = 0;
let mut j: u8 = 0;
#[kani::loop_invariant(i == j)]
#[kani::loop_modifies(&k, &i)]
while let Some(n) = k.checked_sub(1) {
k = n;
i = i.wrapping_add(1);
j = j.wrapping_add(1);
}
}

#[kani::proof]
fn modifies_then_invariant() {
let mut k: u8 = kani::any();
let mut i: u8 = 0;
let mut j: u8 = 0;
#[kani::loop_modifies(&k, &i)]
#[kani::loop_invariant(i == j)]
while let Some(n) = k.checked_sub(1) {
k = n;
i = i.wrapping_add(1);
j = j.wrapping_add(1);
}
}
Loading