Skip to content

#[kani::loop_decreases] combined with #[kani::loop_modifies] causes verification failure #4584

Closed

Description

@feliperodri

When #[kani::loop_decreases] and #[kani::loop_modifies] are both applied to the same loop, CBMC's goto-instrument produces incorrect instrumentation, causing spurious verification failures. Each attribute works correctly in isolation.

Steps to reproduce

#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

#[kani::proof]
fn check_decreases_with_modifies() {
    let mut x: u32 = 10;
    #[kani::loop_invariant(x <= 10)]
    #[kani::loop_modifies(&x)]
    #[kani::loop_decreases(x)]
    while x > 0 {
        x -= 1;
    }
    assert!(x == 0);
}

Expected behavior

Verification succeeds — the loop terminates (decreases x) and only modifies x.

Actual behavior

Verification fails with assigns clause or decreases check failures.

Root cause

The with_loop_decreases and with_loop_modifies builders in stmt.rs each clone the other's field when constructing the annotated Goto statement. However, the order in which goto-instrument processes these annotations may cause conflicts in the instrumented code.

Workaround

Use loop_decreases without loop_modifies, or vice versa. For termination proofs, loop_decreases + loop_invariant (without loop_modifies) is sufficient.

Activity

  1. added this to the Contracts milestone on Apr 22, 2026
  2. added
    [C] Bug
    Z-Contracts
    on Apr 22, 2026
  3. added theissue type on Apr 22, 2026
  4. feliperodri commented on Sep 25, 2026

    @feliperodri
    MemberAuthor

    Re-tested with Kani 0.68.0 (CBMC 6.11.0): kani test.rs -Z loop-contracts on the reproducer in this issue now reports ** 0 of 50 failed / VERIFICATION:- SUCCESSFUL. With both attributes on the loop, a non-decreasing measure (loop_decreases(10 - x)) still fails with "Check variant decreases after step", and writing a variable outside loop_modifies still fails with "Check that y is assignable". So combining the two attributes now works and both are enforced. Closing; please reopen if you have a case that still fails.

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-Contracts[C] Bug

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions