diff --git a/library/kani_macros/src/sysroot/loop_contracts/mod.rs b/library/kani_macros/src/sysroot/loop_contracts/mod.rs index f9fde4e4a953..77e2ccc4bfad 100644 --- a/library/kani_macros/src/sysroot/loop_contracts/mod.rs +++ b/library/kani_macros/src/sysroot/loop_contracts/mod.rs @@ -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)* + #label loop { match #scrutinee { #pat => #body, _ => break, @@ -308,6 +312,7 @@ pub fn transform_for_to_loop( loop_id: &str, ) -> (Stmt, Option) { // 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; @@ -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)* } }; diff --git a/tests/expected/loop-contract/loop_contract_labeled_loops.expected b/tests/expected/loop-contract/loop_contract_labeled_loops.expected new file mode 100644 index 000000000000..4426ff6c02cd --- /dev/null +++ b/tests/expected/loop-contract/loop_contract_labeled_loops.expected @@ -0,0 +1 @@ +Complete - 2 successfully verified harnesses, 0 failures, 2 total. diff --git a/tests/expected/loop-contract/loop_contract_labeled_loops.rs b/tests/expected/loop-contract/loop_contract_labeled_loops.rs new file mode 100644 index 000000000000..cc5b82a610c2 --- /dev/null +++ b/tests/expected/loop-contract/loop_contract_labeled_loops.rs @@ -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); +} diff --git a/tests/expected/loop-contract/while_let_loop_modifies_fail.expected b/tests/expected/loop-contract/while_let_loop_modifies_fail.expected new file mode 100644 index 000000000000..25be079af7ba --- /dev/null +++ b/tests/expected/loop-contract/while_let_loop_modifies_fail.expected @@ -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 diff --git a/tests/expected/loop-contract/while_let_loop_modifies_fail.rs b/tests/expected/loop-contract/while_let_loop_modifies_fail.rs new file mode 100644 index 000000000000..897445daee31 --- /dev/null +++ b/tests/expected/loop-contract/while_let_loop_modifies_fail.rs @@ -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); + } +}