Repository navigation
Conversation
kasimte
force-pushed
the
fix-const-owned-receiver
branch
2 times, most recently
from
October 16, 2026 18:45
46a26fd to
0fbc87f
Compare
…tracts compile The replace closure only borrows `self` (it cannot be re-bound by the kani#3667 dummy reassignments that cover the other arguments -- `self` is a keyword), so the annotated function's own scope had to drop it. For a `const fn` whose receiver type has a destructor that is rejected with E0493. Emit `let self_kani_internal = self;` at the tail of the replace body, after every borrow of `self`, so any drop lands inside the closure body instead.
Contributor
Author
|
Rebased onto current main. CI on the new head is waiting on workflow approval. This fixes the E0493 a contracted const fn throws when self is taken by value and its type has a destructor (#4984). Happy to rebase again. Thanks. |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The tail receiver binding changes destructor order relative to owned arguments, altering existing replacement-mode behavior.
1 open issue
What changed in this PR
This PR addresses #4984 by moving owned receivers into generated contract replacement closures, allowing contracts on const methods whose receiver types have destructors.
Changes:
- Detects by-value receivers and adds a receiver move after replacement-mode contract clauses.
- Preserves the tail move when additional contract attributes expand the generated body.
- Adds successful check/replace regressions and a failing-postcondition test.
| File | Description |
|---|---|
tests/kani/FunctionContracts/const_fn_owned_receiver.rs |
Tests const owned receivers in check and replace modes. |
tests/expected/function-contract/const_fn_owned_receiver_fail.rs |
Tests detection of a violated postcondition. |
tests/expected/function-contract/const_fn_owned_receiver_fail.expected |
Records the expected verification failure. |
library/kani_macros/src/sysroot/contracts/replace.rs |
Generates and repositions the receiver move. |
library/kani_macros/src/sysroot/contracts/mod.rs |
Defines the internal receiver binding name. |
library/kani_macros/src/sysroot/contracts/check.rs |
Adds owned-receiver detection. |
Review effort: Balanced
Contributor
Author
|
Addressed the drop-order comment and merged current main. Awaiting CI workflow approval. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Resolves #4984.
A
const fntakingselfby value can't carry a function contract whenSelfhas a destructor. It fails to compile witherror[E0493]: destructor ... cannot be evaluated at compile-time.This is motivated by model-checking/verify-rust-std#669, which needs this fix to put a contract on
Box::<MaybeUninit<T>>::assume_init(aconst unsafe fn).The fix moves the owned receiver into the generated
replaceclosure. Its drop then happens inside the closure instead of theconst fn's own scope, where E0493 fires. Kani already does this for a contract's other arguments. It couldn't forself, which is a keyword and can't be rebound.Using a move rather than
std::mem::forget(self)is deliberate. Calling forget would also clear E0493, but it would skip the destructor and silently change what replace mode does. The move keeps the destructor, which runs after the contract's clauses. Only replace mode changes; the other modes already consumeselfthrough the body.Other tests cover a
const fnowned-Dropreceiver in both check and replace mode, and a violated-ensures case that still fails, confirming checking isn't weakened. A non-constreceiver throughstub_verifiedasserts that its destructor runs exactly once, with akani::cover!against a vacuous pass. A#[modifies]clause on by-valueselfis contrived, so that replace arm is left untested.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.