Skip to content

Support ordinary stubs of functions with contracts - #5204

Merged
feliperodri merged 3 commits into
model-checking:mainfrom
Tiramisu-Cake:fix/stub-contracted-function
Nov 19, 2026
Merged

feliperodri merged 3 commits into
model-checking:mainfrom
Tiramisu-Cake:fix/stub-contracted-function

Conversation

@Tiramisu-Cake

Copy link
Copy Markdown
Contributor

#[kani::stub(f, g)] failed to compile when f has a contract: the stubbing pass replaced f's body with g's, and the contract pass then looked for f'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 f explicitly with proof_for_contract or stub_verified are 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, through stub_verified, and inside a caller's proof_for_contract. The reproducer from #4591 now compiles; its harness reports an overflow, because the stub may return i32::MAX and 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.

@Tiramisu-Cake
Tiramisu-Cake requested review from a team as code owners November 19, 2026 16:14
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Nov 19, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 19, 2026 16:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_contract or stub_verified selections.
  • 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 feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-Contracts Issue related to code contracts labels Nov 19, 2026
@feliperodri feliperodri added this to the Contracts milestone Nov 19, 2026
@feliperodri feliperodri removed their assignment Nov 19, 2026
@feliperodri
feliperodri added this pull request to the merge queue Nov 19, 2026
Merged via the queue into model-checking:main with commit 39c0853 Nov 19, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-CompilerBenchCI Tag a PR to run benchmark CI Z-Contracts Issue related to code contracts Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[kani::stub] on contracted functions

3 participants