Repository navigation
Support ordinary stubs of functions with contracts - #5204
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the changes enabling ordinary stubs to replace contracted functions.
No open issues 🎉
What changed in this PR
Fixes #4591 by allowing ordinary stubs to replace functions with contracts. Contract processing follows the replacement body while preserving explicitly selected contract targets.
Changes:
- Track the resolved replacement through compiler transformation passes.
- Apply the replacement’s contracts without changing explicit
proof_for_contractorstub_verifiedselections. - Add regression coverage for plain and contracted replacements, disabled contract assertions, and expected verification failures.
| File | Description |
|---|---|
tests/kani/FunctionContracts/ordinary_stub.rs |
Tests ordinary stubs across contract forms, generics, recursion, and contracted replacements. |
tests/kani/FunctionContracts/ordinary_stub_no_assert.rs |
Tests replacements with contract assertions disabled. |
tests/expected/function-contract/ordinary_stub_issue_4591.rs |
Adds the issue reproducer with reachable caller overflow. |
tests/expected/function-contract/ordinary_stub_issue_4591.expected |
Expects overflow failure rather than a compilation error. |
tests/expected/function-contract/ordinary_stub_fail.rs |
Tests enforcement of replacement preconditions and postconditions. |
tests/expected/function-contract/ordinary_stub_fail.expected |
Records expected replacement contract failures. |
kani-compiler/src/kani_middle/transform/stubs.rs |
Reports the instance supplying a successfully replaced body. |
kani-compiler/src/kani_middle/transform/mod.rs |
Tracks body provenance and supports pass-specific instance selection. |
kani-compiler/src/kani_middle/transform/contracts.rs |
Selects replacement contract attributes while preserving explicit targets. |
Review effort: Balanced
feliperodri
left a comment
There was a problem hiding this comment.
Thanks, this is a clean fix. Recording the instance that supplied the body and letting the contract pass process that instance's contract is the right model: a plain replacement supplies only its behavior, and a replacement with its own contract keeps its own generated closures. The tests cover the cases that matter, including the #4591 reproducer. Approving.
Follow-up (not blocking, not a regression): #[kani::stub(f, g)] combined with #[kani::proof_for_contract(f)] or #[kani::stub_verified(f)] still fails with the internal "Failed to find contract closure __kani_recursion_check_f". Both combinations are user errors, so it would help to reject them with a diagnostic that names the conflicting attributes. I opened #5206 for it. Feel free to pick it up here or in a follow-up.
Optional: take_body_source is a side channel the loop must remember to drain. Returning the source instance from transform would make it explicit, but with one implementor the current version is fine.
#[kani::stub(f, g)]failed to compile whenfhas a contract: the stubbing pass replacedf's body withg's, and the contract pass then looked forf's generated closures in the replaced body (Failed to find contract closure __kani_recursion_check_f).The contract pass now follows the resolved replacement. A plain replacement simply supplies its behavior, and a replacement that has its own contract keeps its own checks. Harnesses that select
fexplicitly withproof_for_contractorstub_verifiedare unchanged.The new tests stub functions with
requires-only,ensures-only and combined contracts, a recursive and a generic one, and a replacement that has its own contract, used plainly, throughstub_verified, and inside a caller'sproof_for_contract. The reproducer from #4591 now compiles; its harness reports an overflow, because the stub may returni32::MAXand the caller adds one.Resolves #4591.
Testing: CI in my fork: regression on all five platforms, documentation, benchcomp, LLBC, format, Clippy and Cargo Deny passed. The perf job lost its runner twice in that run; it passed on an earlier commit with the same compiler change (the later commits only change tests outside
tests/perf).By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
Disclosure: this change and description were prepared with the help of AI assistants.