From b6d9974e5026a65f4d08135b6f12110218237f0c Mon Sep 17 00:00:00 2001 From: Talha-Dmr Date: Wed, 30 Sep 2026 20:26:51 +0300 Subject: [PATCH 1/4] Keep loop attributes when rewriting `while let` and `for` loops with a loop invariant `#[kani::loop_invariant]` rewrites a `while let` loop into `loop { match .. }` and a `for` loop into a `while` loop. Both rewrites rebuilt the loop from its condition (or pattern and iterator) and body only, so the attributes that follow `#[kani::loop_invariant]` on the same loop were dropped. A `#[kani::loop_modifies]` written after the invariant was silently ignored (CBMC then inferred the assigns clause), and so was a `#[kani::loop_decreases]`. Writing those attributes before `#[kani::loop_invariant]` worked, because they expand first. Carry the remaining attributes over to the rewritten loop, as is already the case for plain `while` and `loop` loops, which are modified in place. --- .../src/sysroot/loop_contracts/mod.rs | 13 ++++-- .../for_loop_modifies_fail.expected | 9 ++++ .../loop-contract/for_loop_modifies_fail.rs | 42 +++++++++++++++++++ .../while_let_loop_modifies_fail.expected | 9 ++++ .../while_let_loop_modifies_fail.rs | 42 +++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 tests/expected/loop-contract/for_loop_modifies_fail.expected create mode 100644 tests/expected/loop-contract/for_loop_modifies_fail.rs create mode 100644 tests/expected/loop-contract/while_let_loop_modifies_fail.expected create mode 100644 tests/expected/loop-contract/while_let_loop_modifies_fail.rs diff --git a/library/kani_macros/src/sysroot/loop_contracts/mod.rs b/library/kani_macros/src/sysroot/loop_contracts/mod.rs index f9fde4e4a953..b7388eb6b6d9 100644 --- a/library/kani_macros/src/sysroot/loop_contracts/mod.rs +++ b/library/kani_macros/src/sysroot/loop_contracts/mod.rs @@ -234,14 +234,17 @@ 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, 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 remaining attributes of the loop (e.g., a `#[kani::loop_modifies]` or + // `#[kani::loop_decreases]` written after `#[kani::loop_invariant]`). return parse_quote! { + #(#attrs)* loop { match #scrutinee { #pat => #body, @@ -308,6 +311,7 @@ pub fn transform_for_to_loop( loop_id: &str, ) -> (Stmt, Option) { // Extract components from the for loop + let attrs = for_loop.attrs; let pat = *for_loop.pat; let expr = for_loop.expr; let body = for_loop.body; @@ -371,8 +375,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 remaining attributes of the loop (e.g., a `#[kani::loop_modifies]` or + // `#[kani::loop_decreases]` written after `#[kani::loop_invariant]`). let loop_loop: Stmt = parse_quote! { + #(#attrs)* while (#kani_index < #kani_iter_len) { #(#new_body_stmts)* } diff --git a/tests/expected/loop-contract/for_loop_modifies_fail.expected b/tests/expected/loop-contract/for_loop_modifies_fail.expected new file mode 100644 index 000000000000..25be079af7ba --- /dev/null +++ b/tests/expected/loop-contract/for_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/for_loop_modifies_fail.rs b/tests/expected/loop-contract/for_loop_modifies_fail.rs new file mode 100644 index 000000000000..7407d3f4d6c5 --- /dev/null +++ b/tests/expected/loop-contract/for_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 `for` 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 `for` loop rewrite used to drop the attributes that follow +//! `#[kani::loop_invariant]`, so `invariant_then_modifies` verified successfully with an +//! inferred assigns clause. +//! Other assigns checks (for locals introduced by the `for` loop rewrite) may fail as well; this +//! test only checks the one for `j`. + +#![feature(proc_macro_hygiene)] +#![feature(stmt_expr_attributes)] + +#[kani::proof] +fn invariant_then_modifies() { + let a: [u8; 5] = kani::any(); + let mut i: u8 = 0; + let mut j: u8 = 0; + #[kani::loop_invariant(i == j)] + #[kani::loop_modifies(&i)] + for _x in a { + i = i.wrapping_add(1); + j = j.wrapping_add(1); + } +} + +#[kani::proof] +fn modifies_then_invariant() { + let a: [u8; 5] = kani::any(); + let mut i: u8 = 0; + let mut j: u8 = 0; + #[kani::loop_modifies(&i)] + #[kani::loop_invariant(i == j)] + for _x in a { + i = i.wrapping_add(1); + j = j.wrapping_add(1); + } +} 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); + } +} From eb4ac5e926f00fe38b4bf845a2512a21f30fdf2d Mon Sep 17 00:00:00 2001 From: Talha-Dmr Date: Wed, 30 Sep 2026 20:27:47 +0300 Subject: [PATCH 2/4] Keep the label when rewriting `while let` and `for` loops with a loop invariant The `while let` and `for` rewrites of `#[kani::loop_invariant]` also dropped the loop label, so a labeled loop with a loop invariant whose body uses `break 'label` or `continue 'label` failed to compile with E0426 (use of undeclared label). Carry the label over to the rewritten loop. --- .../src/sysroot/loop_contracts/mod.rs | 16 ++++---- .../loop_contract_labeled_loops.expected | 1 + .../loop_contract_labeled_loops.rs | 38 +++++++++++++++++++ 3 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 tests/expected/loop-contract/loop_contract_labeled_loops.expected create mode 100644 tests/expected/loop-contract/loop_contract_labeled_loops.rs diff --git a/library/kani_macros/src/sysroot/loop_contracts/mod.rs b/library/kani_macros/src/sysroot/loop_contracts/mod.rs index b7388eb6b6d9..6711ec2b171f 100644 --- a/library/kani_macros/src/sysroot/loop_contracts/mod.rs +++ b/library/kani_macros/src/sysroot/loop_contracts/mod.rs @@ -234,18 +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 { attrs, 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. - // Keep the remaining attributes of the loop (e.g., a `#[kani::loop_modifies]` or - // `#[kani::loop_decreases]` written after `#[kani::loop_invariant]`). + // 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! { #(#attrs)* - loop { + #label loop { match #scrutinee { #pat => #body, _ => break, @@ -312,6 +313,7 @@ pub fn transform_for_to_loop( ) -> (Stmt, Option) { // Extract components from the for loop let attrs = for_loop.attrs; + let label = for_loop.label; let pat = *for_loop.pat; let expr = for_loop.expr; let body = for_loop.body; @@ -376,11 +378,11 @@ pub fn transform_for_to_loop( new_body_stmts.extend(body.stmts.iter().cloned()); // Create the final expression with the iterator initialization. - // Keep the remaining attributes of the loop (e.g., a `#[kani::loop_modifies]` or - // `#[kani::loop_decreases]` written after `#[kani::loop_invariant]`). + // 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]`). let loop_loop: Stmt = parse_quote! { #(#attrs)* - 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); +} From 36ea4cf8628fedf49a22ef617fefd90cfe46fbbb Mon Sep 17 00:00:00 2001 From: Talha-Dmr Date: Thu, 1 Oct 2026 23:42:17 +0300 Subject: [PATCH 3/4] Keep only the label when rewriting for loops with a loop invariant Carrying the remaining attributes into the rewritten for loop made a complete loop_modifies fail on variables the rewrite generates, and an indexed target such as &out[0] crash the compiler (not a loop head). Leave the for-loop attribute carry-over to a follow-up and keep the while-let carry-over and the label fixes here. --- .../src/sysroot/loop_contracts/mod.rs | 5 +-- .../for_loop_modifies_fail.expected | 9 ---- .../loop-contract/for_loop_modifies_fail.rs | 42 ------------------- 3 files changed, 1 insertion(+), 55 deletions(-) delete mode 100644 tests/expected/loop-contract/for_loop_modifies_fail.expected delete mode 100644 tests/expected/loop-contract/for_loop_modifies_fail.rs diff --git a/library/kani_macros/src/sysroot/loop_contracts/mod.rs b/library/kani_macros/src/sysroot/loop_contracts/mod.rs index 6711ec2b171f..b124c115fae9 100644 --- a/library/kani_macros/src/sysroot/loop_contracts/mod.rs +++ b/library/kani_macros/src/sysroot/loop_contracts/mod.rs @@ -312,7 +312,6 @@ pub fn transform_for_to_loop( loop_id: &str, ) -> (Stmt, Option) { // Extract components from the for loop - let attrs = for_loop.attrs; let label = for_loop.label; let pat = *for_loop.pat; let expr = for_loop.expr; @@ -378,10 +377,8 @@ pub fn transform_for_to_loop( new_body_stmts.extend(body.stmts.iter().cloned()); // Create the final expression with the iterator initialization. - // 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]`). + // Keep the label of the loop. let loop_loop: Stmt = parse_quote! { - #(#attrs)* #label while (#kani_index < #kani_iter_len) { #(#new_body_stmts)* } diff --git a/tests/expected/loop-contract/for_loop_modifies_fail.expected b/tests/expected/loop-contract/for_loop_modifies_fail.expected deleted file mode 100644 index 25be079af7ba..000000000000 --- a/tests/expected/loop-contract/for_loop_modifies_fail.expected +++ /dev/null @@ -1,9 +0,0 @@ -- 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/for_loop_modifies_fail.rs b/tests/expected/loop-contract/for_loop_modifies_fail.rs deleted file mode 100644 index 7407d3f4d6c5..000000000000 --- a/tests/expected/loop-contract/for_loop_modifies_fail.rs +++ /dev/null @@ -1,42 +0,0 @@ -// 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 `for` 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 `for` loop rewrite used to drop the attributes that follow -//! `#[kani::loop_invariant]`, so `invariant_then_modifies` verified successfully with an -//! inferred assigns clause. -//! Other assigns checks (for locals introduced by the `for` loop rewrite) may fail as well; this -//! test only checks the one for `j`. - -#![feature(proc_macro_hygiene)] -#![feature(stmt_expr_attributes)] - -#[kani::proof] -fn invariant_then_modifies() { - let a: [u8; 5] = kani::any(); - let mut i: u8 = 0; - let mut j: u8 = 0; - #[kani::loop_invariant(i == j)] - #[kani::loop_modifies(&i)] - for _x in a { - i = i.wrapping_add(1); - j = j.wrapping_add(1); - } -} - -#[kani::proof] -fn modifies_then_invariant() { - let a: [u8; 5] = kani::any(); - let mut i: u8 = 0; - let mut j: u8 = 0; - #[kani::loop_modifies(&i)] - #[kani::loop_invariant(i == j)] - for _x in a { - i = i.wrapping_add(1); - j = j.wrapping_add(1); - } -} From d2fadb8c2ca409c7b747d50ae66644380eecf76c Mon Sep 17 00:00:00 2001 From: Talha-Dmr Date: Fri, 2 Oct 2026 22:43:41 +0300 Subject: [PATCH 4/4] Explain why the for loop rewrite does not carry over attributes yet --- library/kani_macros/src/sysroot/loop_contracts/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/kani_macros/src/sysroot/loop_contracts/mod.rs b/library/kani_macros/src/sysroot/loop_contracts/mod.rs index b124c115fae9..77e2ccc4bfad 100644 --- a/library/kani_macros/src/sysroot/loop_contracts/mod.rs +++ b/library/kani_macros/src/sysroot/loop_contracts/mod.rs @@ -377,7 +377,8 @@ pub fn transform_for_to_loop( new_body_stmts.extend(body.stmts.iter().cloned()); // Create the final expression with the iterator initialization. - // Keep the label of the loop. + // 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! { #label while (#kani_index < #kani_iter_len) { #(#new_body_stmts)*