Skip to content

#[kani::loop_invariant] on a while loop with a let-chain condition fails to compile #4943

Closed

Description

@kasimte

I tried this code as a Cargo package (let chains are edition-2024 syntax; single-file kani rejects them). Cargo.toml:

[package]
name = "repro"
version = "0.1.0"
edition = "2024"

src/lib.rs:

// loop_invariant is a statement attribute, so both features are required to compile.
#![feature(stmt_expr_attributes)]
#![feature(proc_macro_hygiene)]

#[kani::proof]
fn countdown() {
    let mut remaining: usize = kani::any();
    kani::assume(remaining <= 4);
    #[kani::loop_invariant(remaining <= 4)]
    while remaining > 0 && let Some(next) = remaining.checked_sub(1) {
        remaining = next;
    }
    assert!(remaining <= 4);
}

using the following command line invocation:

cargo kani -Z loop-contracts

with Kani version: 0.68.0 (CBMC 6.11.0)

I expected to see this happen: the harness compiles and the loop contract applies, as it does for similar loops without a let chain (below).

Instead, this happened (cargo's failure trailer elided):

error: expected expression, found `let` statement
  --> src/lib.rs:10:28
   |
10 |     while remaining > 0 && let Some(next) = remaining.checked_sub(1) {
   |                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: only supported directly in conditions of `if` and `while` expressions
note: `let`s wrapped in parentheses are not supported in a context with let chains
  --> src/lib.rs:10:11
   |
10 |     while remaining > 0 && let Some(next) = remaining.checked_sub(1) {
   |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Replacing the countdown harness with the two below (same crate attributes) compiles, and both verify successfully with the same invocation:

// Bare `while let` (no chain): accepted.
#[kani::proof]
fn countdown_bare_while_let() {
    let mut remaining: usize = kani::any();
    kani::assume(remaining <= 4);
    #[kani::loop_invariant(remaining <= 4)]
    while let Some(next) = remaining.checked_sub(1) {
        remaining = next;
    }
    assert!(remaining <= 4);
}

// The chain desugared with let-else (same evaluation order): accepted.
#[kani::proof]
fn countdown_desugared() {
    let mut remaining: usize = kani::any();
    kani::assume(remaining <= 4);
    #[kani::loop_invariant(remaining <= 4)]
    while remaining > 0 {
        let Some(next) = remaining.checked_sub(1) else { break };
        remaining = next;
    }
    assert!(remaining <= 4);
}

So the trigger is a let chain in the while condition, not let-in-condition in general. From reading library/kani_macros/src/sysroot/loop_contracts/mod.rs on main (b44b964b3): while_let_rewrite only matches a condition that is entirely an Expr::Let, so a chain falls through to the plain-while path, which rebuilds the condition as kani_register_loop_contract_<id>(&|| -> bool { <invariant> }, 0) && <original condition>. Per the diagnostic note, the let reaches the parser parenthesized, which is not a valid chain operand; that is consistent with the rebuilt condition embedding the original as a single grouped operand. A possible fix is to generalize while_let_rewrite to chains (check the non-let operands, then match on the scrutinee), or to destructure the chain when rebuilding the condition instead of embedding it whole.

#4930 is related but distinct: it covers attributes and labels being dropped by the while let/for rewrites, and a let chain never reaches those rewrites.

For context, this is the loop shape of Chars::advance_by's chunk-skip loop in library/core/src/str/iter.rs (while remainder > CHUNK_SIZE && let Some(chunk) = chunks.next()), which is in the function table of verify-rust-std challenge 22; annotating that loop hit this error with Kani 0.67.0 (CBMC 6.8.0) against the verify-rust-std toolchain. The let-else desugar above is a workaround for the parse error, but for the standard library that means contracting a rewritten body rather than the shipped code.

Activity

  1. added this to the Contracts milestone on Oct 2, 2026
  2. added a commit that references this issue on Oct 22, 2026
    adffc7d
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