Skip to content

Cannot annotate a const fn taking self by value with a contract (E0493) #4984

Closed

Description

@kasimte

I tried this code:

struct S(Box<u32>);

impl S {
    #[kani::requires(*self.0 < 100)]
    #[kani::ensures(|result: &S| *result.0 == old(*self.0))]
    const fn id(self) -> S {
        self
    }
}

using the following command line invocation:

kani repro.rs -Z function-contracts

with Kani version: 0.68.0 (built from main at 1640445, CBMC 6.11.0).

I expected this to compile, the same way the equivalent typed-argument case already does:

// compiles today:
#[kani::requires(*b < 100)]
const fn take(b: Box<u32>) -> Box<u32> { b }

Instead, this happened:

error[E0493]: destructor of `S` cannot be evaluated at compile-time
 --> repro.rs:6:17
  |
4 |     #[kani::requires(*self.0 < 100)]
  |                                    - value is dropped here
5 |     #[kani::ensures(|result: &S| *result.0 == old(*self.0))]
6 |     const fn id(self) -> S {
  |                 ^^^^ the destructor for this type cannot be evaluated in constant functions

The body alone is const-legal: remove the two contract attributes and it compiles. The error appears only with the contract, and only when all three hold together: a const fn, a by-value owned self, and a Self that needs drop. A non-const twin, a Copy receiver, and a typed Box argument all compile.

Use case: proof_for_contract on a const fn with an owned self in std — the case that hit it is Box::<MaybeUninit>::assume_init (const unsafe fn since nightly-2026-09-25); a consumer is model-checking/verify-rust-std#669.

Related (same class, both closed): #3667, #3726.

Activity

  1. added a commit that references this issue on Nov 4, 2026
    47689dc
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions