Skip to content

#[kani::loop_decreases] does not support struct field projections or arithmetic expressions #4585

Description

@feliperodri

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:

  1. 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.

  2. 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]
fn check_arithmetic_decreases() {
    let mut 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:

let mut remaining: u32 = 19;
#[kani::loop_decreases(remaining)]
while remaining > 0 {
    remaining -= 1;
}

Activity

  1. added this to the Contracts milestone on Apr 22, 2026
  2. feliperodri commented on Nov 9, 2026

    @feliperodri
    MemberAuthor

    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.

    Minimal form, Kani main at 47689dc:

    #[kani::proof]
    fn count_up() {
        let mut 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:

    let mut remaining: u16 = 64;
    #[kani::loop_invariant(i <= 64 && remaining as usize + 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.)

  3. added
    [F] Spurious FailureIssues that cause Kani verification to fail despite the code being correct.
    on Nov 17, 2026
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

    Z-ContractsIssue related to code contracts[C] Feature / EnhancementA new feature request or enhancement to an existing feature.[E] Unsupported ConstructAdd support to an unsupported construct[F] Spurious FailureIssues that cause Kani verification to fail despite the code being correct.

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions