Skip to content

#[kani::loop_decreases] with a plain variable measure generates no decreases check #4942

Closed

Description

@Talha-Dmr

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.rs and loop_loop_decreases.rs verify, but without a decreases check.
  • decreases_fail_wrong_measure.rs fails only on attempt to add with overflow (line 21).
  • decreases_fail_stale_measure.rs fails only on Check 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

  1. Talha-Dmr commented on Oct 2, 2026

    @Talha-Dmr
    ContributorAuthor

    I found the cause. CBMC reads the #spec_decreases annotation of a loop latch as a list of measures: dfcc_cfg_info.cpp takes get_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 u32 and m + 0 are 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, as 4 - i does (#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:

    • When the measure is a variable that is assigned only once (like y in decreases_fail_stale_measure.rs), copy propagation merges the kani_loop_decreases binding into that variable. Codegen then takes the definition of the variable (y = x) for the binding, drops that assignment and measures x instead of y.
    • The clause is attached to the next loop latch that codegen generates, so with nested loops the clause of the outer loop goes to the inner loop.

    The fix for all three is in #4946.

  2. added this to the Contracts milestone on Oct 2, 2026
  3. added a commit that references this issue on Oct 6, 2026
    ac3eab3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions