You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CBMC aborts with l2_rename_rvalues case struct' not handled` when a zero-sized closure is a loop_modifies target #4786
Naming a zero-sized closure (a capture-free impl FnMut) as a #[kani::loop_modifies] target makes CBMC abort with l2_rename_rvalues case struct' not handled` (exit status 6) while applying the loop contract. The same message is tracked in #1257 / diffblue/cbmc#6911 for a different trigger (boxed fn items); this one comes from havocking a zero-sized struct-typed assigns target.
I tried this code (kani zst_closure.rs -Z loop-contracts):
#![feature(proc_macro_hygiene)]#![feature(stmt_expr_attributes)]fncount_kept(a:&mut[u8],mutkeep:implFnMut(&mutu8) -> bool) -> usize{let n = a.len();letmut i = 0;letmut kept = 0;#[kani::loop_invariant(i <= n && kept <= i)]#[kani::loop_modifies(&i,&kept,&keep,&raw mut*a)]while i < n {ifkeep(&mut a[i]){
kept += 1;}
i += 1;}
kept
}#[kani::proof]fncheck(){letmut a:[u8;8] = kani::any();let k = count_kept(&mut a, |x| {let keep:bool = kani::any();if keep {*x = 0;} keep });assert!(k <= 8);}
I expected to see this happen: verification succeeds (or Kani reports that a zero-sized target is not a valid loop_modifies target).
Instead, this happened:
Checking harness check...
l2_rename_rvalues case `struct' not handled
CBMC failed with status 6
VERIFICATION:- FAILED
Removing &keep from the loop_modifies list makes the harness verify. Listing the closure is natural when the loop calls a generic FnMut parameter (that is how VecDeque::retain_mut's loops look in model-checking/verify-rust-std#681, where I hit this), because a stateful closure would otherwise fail the assigns check.
Reproduced with Kani d4df833c (0.67.0-dev, 2026-01-18) and with current main (b07abe8, 2026-09-01), both with CBMC 6.8.0 on macOS/aarch64 (I could not test with the CBMC 6.11.0 that main pins).
Root cause: the loop-contract assigns lowering never filtered zero-sized targets. When a capture-free closure (a zero-sized impl FnMut) is named in #[kani::loop_modifies], rvalue_to_assign_targets (kani-compiler/.../codegen/statement.rs) emits a dereferenced size-0 struct as a __CPROVER_assigns target, and CBMC aborts while havocking it (l2_rename_rvalues case 'struct' not handled, exit 6).
The function-contract path already handles this — codegen_modifies_target filters ZST pointers out of the assigns clause (added in #3417) — but the loop-contract path was missing the equivalent guard, which is why removing &keep from the list was a working workaround.
Fix in #4792: skip a loop_modifies target whose pointee is a ZST. This is sound — a ZST has a single inhabitant and occupies zero bytes, so havocking it is a no-op and dropping it cannot mask any state change. Stateful closures capture state and are not ZSTs, so they remain in the assigns clause and are still havocked.
Activity
feliperodri commentedon Sep 14, 2026
Root cause: the loop-contract assigns lowering never filtered zero-sized targets. When a capture-free closure (a zero-sized
impl FnMut) is named in#[kani::loop_modifies],rvalue_to_assign_targets(kani-compiler/.../codegen/statement.rs) emits a dereferenced size-0 struct as a__CPROVER_assignstarget, and CBMC aborts while havocking it (l2_rename_rvalues case 'struct' not handled, exit 6).The function-contract path already handles this —
codegen_modifies_targetfilters ZST pointers out of the assigns clause (added in #3417) — but the loop-contract path was missing the equivalent guard, which is why removing&keepfrom the list was a working workaround.Fix in #4792: skip a
loop_modifiestarget whose pointee is a ZST. This is sound — a ZST has a single inhabitant and occupies zero bytes, so havocking it is a no-op and dropping it cannot mask any state change. Stateful closures capture state and are not ZSTs, so they remain in the assigns clause and are still havocked.Skip ZST targets in loop_modifies assigns clause (model-checking#4792)