Skip to content

should_panic and --prove-safety-only treat postconditions and kani::assert as panics #5212

Description

@feliperodri

Kani gives Rust panics and its own checks the same property class, assertion:

  • panics, from codegen_panic and from the panicking MIR Assert terminators (bounds, overflow, division);
  • kani::assert, which Kani's assert!, assert_eq! and assert_ne! overrides in library/std also call, and which contract postconditions expand to.

So neither the compiler nor the driver can tell a panic from a failed postcondition or user assertion, and two features give wrong results.

Reproduce

#[kani::ensures(|r: &u8| *r == x)]
fn wrong(x: u8) -> u8 { x.wrapping_add(1) }

#[kani::proof_for_contract(wrong)]
fn check_wrong_post() { wrong(kani::any()); }

#[kani::proof_for_contract(wrong)]
#[kani::should_panic]
fn check_wrong_post_should_panic() { wrong(kani::any()); }

#[kani::proof]
#[kani::should_panic]
fn user_assert_should_panic() { let x: u8 = kani::any(); kani::assert(x != 3, "user property"); }
  1. should_panic accepts non-panics. kani p.rs -Z function-contracts reports both should_panic harnesses as VERIFICATION:- SUCCESSFUL (encountered one or more panics as expected), although neither panics: one fails its postcondition, the other a kani::assert.
  2. --prove-safety-only drops postconditions and kani::assert. With -Z unstable-options --prove-safety-only, check_wrong_post verifies, because the flag assumes away every assertion-class check (codegen_assert_assume in assert.rs), including the postcondition.

Expected

  • should_panic counts only panics.
  • --prove-safety-only assumes only panics away and still checks postconditions and kani::assert.

Suggested fix

Give panics their own property class, panic:

  • codegen_panic and the panicking MIR assertions emit panic;
  • the assert!/assert_eq!/assert_ne! overrides call a Kani-internal function hooked to emit panic, instead of kani::assert;
  • kani::assert and contract conditions keep assertion;
  • the driver's should_panic classification and --prove-safety-only key on panic.

This changes some property names in the output. It is also the prerequisite for RFC #3893 (panics_only_if).

Activity

  1. added
    [C] Bug
    [F] Soundness
    on Nov 20, 2026
  2. added 2 commits that reference this issue on Nov 20, 2026
    cd69a36
    a0b10e1
  3. added 2 commits that reference this issue on Nov 23, 2026
    90fc784
    1ec4892
  4. added
    Z-Kani Compiler
    on Nov 25, 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-Kani Compiler[C] Bug[F] Soundness

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions