You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
#[kani::loop_decreases] does not support struct field projections or arithmetic expressions #4585
The #[kani::loop_decreases] attribute only works with simple variable measures. Arithmetic expressions (e.g., 19 - p) and struct field projections (e.g., state.remaining) fail because:
Arithmetic expressions: Rust's checked arithmetic compiles to CheckedBinaryOp which produces a StatementExpression in the GOTO program. By the time the decreases assignment is processed in codegen, the expression has been split across multiple MIR statements — the decreases binding only captures the final field access (_4.0), not the full computation.
Struct field projections: Similar MIR decomposition issue — the field access is split into separate statements.
Steps to reproduce
#![feature(proc_macro_hygiene)]#![feature(stmt_expr_attributes)]#[kani::proof]fncheck_arithmetic_decreases(){letmut p:u32 = 0;#[kani::loop_invariant(p <= 19)]#[kani::loop_decreases(19 - p)]while p < 19{
p += 1;}}
Expected behavior
Verification succeeds — 19 - p strictly decreases each iteration.
Actual behavior
Failed Checks: Check variant decreases after step for loop
Workaround
Use wrapping_sub instead of -:
#[kani::loop_decreases(19u32.wrapping_sub(p))]
Or restructure the loop to count down so a simple variable suffices:
More evidence from real code: this is the most common way loop contracts failed in our runs of the Kani agent plugin (#5059) on the top crates.io crates, and agents give up on the loop contract when it happens.
base64 0.23.1, engine::general_purpose::encode_table: while index < 64 { table[index] = alphabet.symbols[index]; index += 1; } with loop_invariant(index <= 64) and loop_decreases(64 - index) fails Check variant decreases after step.
#[kani::proof]fncount_up(){letmut i:usize = 0;#[kani::loop_invariant(i <= 4)]#[kani::loop_decreases(4 - i)]while i < 4{ i += 1;}}
VERIFICATION:- FAILED (Check variant decreases after step). The separate-counter workaround verifies:
letmut remaining:u16 = 64;#[kani::loop_invariant(i <= 64 && remaining asusize + i == 64)]#[kani::loop_decreases(remaining)]while i < 64{ remaining -= 1; i += 1;}
A measure that reads through a local &mut borrowed from a method may be another instance: let len_ptr = self.len_mut(); #[kani::loop_decreases(*len_ptr)] while len < *len_ptr { *len_ptr -= 1; } fails the same check, while the test count_down(n: &mut u32) with loop_decreases(*n) passes. I have not confirmed that it is the same cause.
Until this is fixed, an error message that suggests the counter workaround when the measure is an arithmetic expression would help agents and users.
(Written with AI assistance; the snippets were run by hand on the build above.)
Activity
Add `#[kani::loop_decreases]` for proving loop termination (model-che…
Keep loop attributes and labels when rewriting `while let` and `for` …
Warn about decreases measures computed before the loop, and hide the …
Check `#[kani::loop_decreases]` clauses whose measure is a variable (m…
Keep loop attributes on rewritten for loops and add loop-only locals …
feliperodri commentedon Nov 9, 2026
More evidence from real code: this is the most common way loop contracts failed in our runs of the Kani agent plugin (#5059) on the top crates.io crates, and agents give up on the loop contract when it happens.
engine::general_purpose::encode_table:while index < 64 { table[index] = alphabet.symbols[index]; index += 1; }withloop_invariant(index <= 64)andloop_decreases(64 - index)failsCheck variant decreases after step.Minimal form, Kani
mainat 47689dc:VERIFICATION:- FAILED(Check variant decreases after step). The separate-counter workaround verifies:A measure that reads through a local
&mutborrowed from a method may be another instance:let len_ptr = self.len_mut(); #[kani::loop_decreases(*len_ptr)] while len < *len_ptr { *len_ptr -= 1; }fails the same check, while the testcount_down(n: &mut u32)withloop_decreases(*n)passes. I have not confirmed that it is the same cause.Until this is fixed, an error message that suggests the counter workaround when the measure is an arithmetic expression would help agents and users.
(Written with AI assistance; the snippets were run by hand on the build above.)