Issue #2724: Kani mode: loop invariants from the prover

Open and answer this issue on GitHub.

blocked, safety · opened 2026-11-22 · updated 2026-11-29 · 1 comment

Records ADR-proof-kani-v1 (Kani checks the prover’s proofs on the emitted Rust, in a mode of its own); the third of its six issues. Needs #2721

Goal

In the Kani mode the emitter writes, on each unchanged for loop the prover proved, the invariant the prover used as #[kani::loop_invariant(…)], so Kani checks the induction the prover claims. Loops Kani cannot take a contract on are unwound and reported.

Governing decision

ADR-proof-kani-v1 D2 (invariants on the unchanged for), D4 (the invariant is an obligation, never an assumption), D5 (loops outside Kani’s coverage are unwound and reported). ADR-361 D4, D5, D10 (what a loop’s invariant is). ADR-306 D19 (a dropped check).

Today (read in the code)

To build

  1. Hand the invariant over. The walk records, per loop it proved, the invariant it used, keyed so the emitter finds the loop (the builder names the key and the record; one function all reads go through). With the Kani mode off nothing is recorded. Test: tests/compiler/tests/kani_mode_writes_the_loop_invariant.nika, a counted loop over xs.len() with a dropped read; its .stdout is its output, and the lowered loop with the switch is shown by the issue-1 probe run (quoted in the report).
  2. Write it in Rust. The invariant’s facts are spelled as a Rust boolean over the loop’s variables (0 <= k && (k as usize) <= xs.len()); a fact with no Rust spelling (a ledger fact over a field, ADR-306 D21) drops the whole invariant for that loop and the loop goes to item 3. Test: the same program with a loop over xs.iter() and one with a while over a counter.
  3. Unwound and reported. A loop without an invariant (none proved, no Rust spelling, or a form Kani 0.68 takes no contract on — the forms the first issue recorded) gets nothing on the loop; its harness gets #[kani::unwind(9)] (the vector bound of the harness plus one); the report line is unwound: f: loop at FILE:LINE: <why>. Test: a loop over a map’s values_mut().
  4. A wrong invariant fails. A test hook writes a deliberately weakened invariant (one fact dropped) for one named loop, so the run shows Kani refusing it. The builder decides where the hook lives (a setting read only in the Kani mode). Test: the run’s refusal line as .stderr.

Done when, with the Kani mode on, cargo kani -Z loop-contracts -Z function-contracts proves every loop of the first issue’s probe program by its invariant (no unwinding), refuses the weakened one of item 4, and reports each unwound loop by item 3’s line; without the switch the lowering is byte-identical (lower-std diff empty, quoted). Item 4’s commit carries Fixes #N. Each surprise is a Found: line in the report, never a new issue number.

Files and limits

Ruled out

Comments

keywan-ghadami, 2026-11-29

Related decision: ADR-342 D6 (owner, 2026-11-29, 56f8c93f). A chain over a sequence (xs.iter().map(f).sum(), io::lines().filter(...)) is lowered as a loop the compiler writes instead of the language below’s adapters. Such a chain then has a for or while in the emitted code that this issue’s invariant can stand on, instead of a loop inside std’s iterator that Kani has to unwind. The first build is #2899 (chains over io::lines()).


Generated by Claude Code

Back to Open Issues