Skip to content

Loop contracts: ICE "Kani can only insert instructions after terminators that have a target field" when a loop-local is initialized through branching control flow #4959

Description

@kasimte

I tried this code:

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

fn pick(x: u32) -> u32 {
    x + 1
}

#[kani::proof]
fn loop_local_var_branching_init() {
    let n: u32 = kani::any_where(|x| *x <= 8);
    let mut i: u32 = 0;
    #[kani::loop_invariant(i <= n)]
    while i < n {
        let step = if i % 2 == 0 { pick(0) } else { pick(0) };
        i += step;
    }
    assert!(i == n);
}

using the following command line invocation:

kani loop_local_var_branching_init.rs -Z loop-contracts

with Kani version: 0.68.0 (CBMC 6.11.0).

I expected to see this happen: Kani verifies the contracted loop.

Instead, this happened: the compiler panicked.

thread 'rustc' panicked at kani-compiler/src/kani_middle/transform/body.rs:680:14:
not implemented: Kani can only insert instructions after terminators that have a `target` field.
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: <kani_compiler::kani_middle::transform::body::MutableBody>::insert_bb
   3: <kani_compiler::kani_middle::transform::loop_contracts::LoopContractPass>::transform_body_with_loop
   4: <kani_compiler::kani_middle::transform::loop_contracts::LoopContractPass as kani_compiler::kani_middle::transform::TransformPass>::transform
   ...

Kani unexpectedly panicked during compilation.

The declaration-moving step (move_storagelive_call_to_loophead) copies a
loop-local's init blocks to the loop head; the splice re-targets each block's
terminator, which only works for terminators with a single target. A branching
initializer puts a SwitchInt in the group, so the splice hits the
unimplemented! in get_mut_target_ref (via insert_bb).

A straight-line initializer (let step = pick(0);, no branch) puts no SwitchInt
in the group and is unaffected. The crash also does not depend on the branch
condition reading loop state: it fires just as well when the condition reads a
variable declared before the loop.

Activity

  1. added
    [F] Crash
    Z-Contracts
    on Nov 6, 2026
  2. added this to the Contracts milestone on Nov 6, 2026
  3. added theissue type on Nov 6, 2026
  4. added
    [C] Bug
    on Nov 6, 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-Contracts[C] Bug[F] Crash

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions