Skip to content

Panic clauses: destructors of by-value arguments run after the panic permission is restored #5221

Open

Description

@feliperodri

With the panics_only_if and panics_if contract clauses (#4230), a panic in the destructor of a by-value argument that the function body does not move out is checked against the wrong permission. Kani's contract instrumentation drops such arguments after the check closure has returned, so the drop happens after the function's panic permission is restored and after panics_if has checked the return.

struct Bomb(bool);
impl Drop for Bomb { fn drop(&mut self) { if self.0 { panic!("boom") } } }

#[kani::panics_only_if(true)]
fn unused(_b: Bomb) {}
#[kani::proof_for_contract(unused)]
fn check_unused() { unused(Bomb(kani::any())); }   // FAILED: boom (not permitted by the contract)

#[kani::panics_if(armed)]
fn must(armed: bool, _b: Bomb) { let _ = armed; }
#[kani::proof_for_contract(must)]
fn check_must() { let a: bool = kani::any(); must(a, Bomb(a)); }   // FAILED: returned although `panics_if(armed)` held

The same happens when the body only borrows the argument (fn used(b: Bomb) { let _x = &b; }).

In Rust, these destructors run at the end of the function, so their panics are panics of the function: both harnesses should verify. The current behavior can only cause spurious failures, never hide a panic, and it is documented as a known limitation in the contracts module docs.

A fix needs the check closure to drop the function's by-value arguments before it restores the panic permission and checks the return. That requires a change to how the contract instrumentation passes arguments to the function body.

Activity

  1. added
    [C] Bug
    Z-Contracts
    on Nov 21, 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions