Repository navigation
Description
While working on the for loop follow-up to #4930, I found that on current main a #[kani::loop_decreases] clause whose measure is a plain variable does not generate a decreases check. A measure that increases on every iteration verifies successfully.
Reproducer
#![feature(stmt_expr_attributes)]
#![feature(proc_macro_hygiene)]
#[kani::proof]
fn while_wrong_decreases() {
let mut i: usize = 0;
let mut m: u16 = 0;
#[kani::loop_invariant(i <= 4 && m as usize == i)]
#[kani::loop_decreases(m)]
while i < 4 {
m = m + 1; // the measure increases
i += 1;
}
}kani decreases.rs -Z loop-contracts
Expected: a failure of Check variant decreases after step for loop while_wrong_decreases.0, as described under "Wrong decreases clause" in the loop contracts reference (docs/src/reference/experimental/loop-contracts.md).
Actual: VERIFICATION:- SUCCESSFUL. The loop checks in the output are Check invariant before entry, Check invariant after step, Check step was unwound (twice), Check assigns clause inclusion, Check that i is assignable and Check that m is assignable. There is no decreases check. The same happens for a for loop with the decreases clause written before the invariant.
With a measure that is not a plain variable, the check is generated. With #[kani::loop_decreases(m as u32)] or #[kani::loop_decreases(m + 0)], the same harness fails on Check variant decreases after step for loop while_wrong_decreases.0, as expected. (A correct measure such as 4 - i also fails that check, but that is #4585.)
Kani's own tests
I ran the decreases tests in tests/expected/loop-contract (except the fixme_ ones) on main. Only decreases_binary_search.rs, whose measure is hi - lo, produces a Check variant decreases property. All the others use a plain variable (x, y, i or remaining) and produce none:
simple_while_loop_decreases.rs,decreases_expr.rs,decreases_loop_max.rs,decreases_with_function_contract.rs,decreases_with_old.rsandloop_loop_decreases.rsverify, but without a decreases check.decreases_fail_wrong_measure.rsfails only onattempt to add with overflow(line 21).decreases_fail_stale_measure.rsfails only onCheck invariant after step for loop stale_measure_harness.0(line 18).
The .expected files of the two failing tests only match Status: FAILURE and VERIFICATION:- FAILED, so they pass without a decreases check. They have not changed since #4564, so I could not tell from them whether the check was generated when the feature was added. #4585 says that plain variable measures work and suggests them as a workaround, so this may be a regression; I have not bisected it.
#[kani::loop_decreases(e)] expands to let kani_loop_decreases = (e); before the loop, and codegen_statement (statement.rs) takes the measure from the right-hand side of the assignment to kani_loop_decreases. I have not checked yet what happens to that assignment when e is a plain variable.
Environment
Kani main at 4783025, built from source; CBMC 6.11.0; x86_64 Linux.
Activity
Talha-Dmr commentedon Oct 2, 2026
I found the cause. CBMC reads the
#spec_decreasesannotation of a loop latch as a list of measures:dfcc_cfg_info.cpptakesget_loop_decreases(loop.latch, ..).operands(), because the C front end stores__CPROVER_decreases(e1, ..., en)as an irep whose operands are the measures. Kani attaches the measure expression itself (to_irep.rs). A variable has no operands, so the list is empty and there is no check.For the other measures, CBMC used their operands as the measure.
m as u32andm + 0are computed through MIR temporaries before the loop (_3 = copy _2; _k = move _3 as u32), so CBMC compared a temporary that the loop never changes. That is why those variants fail, but they would also fail with a correct measure, as4 - idoes (#4585). This has not changed since #4564 (CBMC 6.8.0 also reads the operands), so it is not a regression.Fixing the encoding is not enough on its own:
yindecreases_fail_stale_measure.rs), copy propagation merges thekani_loop_decreasesbinding into that variable. Codegen then takes the definition of the variable (y = x) for the binding, drops that assignment and measuresxinstead ofy.The fix for all three is in #4946.
Check `#[kani::loop_decreases]` clauses whose measure is a variable
Check `#[kani::loop_decreases]` clauses whose measure is a variable (m…
Keep loop attributes on rewritten for loops and add loop-only locals …