Skip to content

Ensure that contract closures are FnOnce #4151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jul 4, 2025

Conversation

vonaka
Copy link
Contributor

@vonaka vonaka commented Jun 11, 2025

Rust believes that Kani's contract closures are FnMut. This prevents us from writing contracts for functions that return mutable references to their input arguments (#3764).

To ensure Rust correctly infers these closures as FnOnce, they need to be wrapped in a dummy function that explicitly requires an FnOnce. This wrapping must be done at the point of closure definition, as doing it later, when calling the function, doesn't seem to have any effect.

Resolves #3764

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@vonaka vonaka requested a review from a team as a code owner June 11, 2025 16:22
@github-actions github-actions bot added the Z-EndToEndBenchCI Tag a PR to run benchmark CI label Jun 11, 2025
Copy link
Contributor

@carolynzech carolynzech left a comment

Choose a reason for hiding this comment

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

Thanks! Before we review the code, can you write test(s)? You can put the one from the linked issue in the kani folder (which just checks that Kani exits successfully), and perhaps then add some more complex tests to the expected folder that ensure that the printed output contains successful assertions for the postconditions.

You can take a look at the existing contracts tests for inspiration. To run a test, run cargo build-dev && cargo run -p compiletest -- --suite [SUITE] --mode [MODE] <test name>. You can look at scripts/kani-regression.sh for the relevant suites and modes (or run cargo run -p compiletest -- --help).

@vonaka
Copy link
Contributor Author

vonaka commented Jun 18, 2025

@carolynzech I added some basic tests. Regarding PR itself, defining force_fn_once to later replace it with literarily the same function feels suboptimal. Maybe at the very least, the body of force_fn_once should be unreachable!, just like kani_register_contract. Speaking of which, I wonder if this function is still needed considering this PR

@carolynzech carolynzech force-pushed the force_fn_once branch 2 times, most recently from e726a58 to bc801cf Compare June 23, 2025 16:54
Copy link
Contributor

@carolynzech carolynzech left a comment

Choose a reason for hiding this comment

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

Regarding PR itself, defining force_fn_once to later replace it with literarily the same function feels suboptimal. Maybe at the very least, the body of force_fn_once should be unreachable!, just like kani_register_contract. Speaking of which, I wonder if this function is still needed considering this PR

Yeah, so I can think of a couple of things you can try here:

  1. Make the body of the functions in kani_macros unreachable!, just like kani_register_contract does, or
  2. Remove the compiler & kani_core changes entirely and just do the transformations entirely in the macro expansion logic. I would think you could just change the macro logic to have an extra () that calls these const functions, so that then the closures are just there. You'd have to try it, though.

I don't know if we'll need kani_register_contract anymore. I would try removing it and seeing if we can still write contracts instead const functions, since that's why we needed it in the first place.

@github-actions github-actions bot added the Z-CompilerBenchCI Tag a PR to run benchmark CI label Jun 24, 2025
@vonaka
Copy link
Contributor Author

vonaka commented Jun 26, 2025

Remove the compiler & kani_core changes entirely and just do the transformations entirely in the macro expansion logic. I would think you could just change the macro logic to have an extra () that calls these const functions, so that then the closures are just there. You'd have to try it, though.

Right, so I simply reverted all the changes to the core and the compiler. I assume this is enough? The only problem I see is that if the file Kani tries to verify has its own kani_force_fn_once, Kani will fail to compile. But this is also the case for kani_register_contract, so at least I'm being consistent with the existing code. In general, I'm not sure how to fix this elegantly (and I'm not sure if it's worth fixing).

Copy link
Contributor

@carolynzech carolynzech left a comment

Choose a reason for hiding this comment

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

Thanks! Before merging, can you check if we can indeed get rid of kani_register_contract?

@carolynzech carolynzech assigned vonaka and unassigned carolynzech Jul 3, 2025
@vonaka
Copy link
Contributor Author

vonaka commented Jul 3, 2025

Thanks! Before merging, can you check if we can indeed get rid of kani_register_contract?

We can't, we still need this workaround to call closures from const functions.

@tautschnig tautschnig added this pull request to the merge queue Jul 4, 2025
Merged via the queue into model-checking:main with commit a9e14fe Jul 4, 2025
28 checks passed
github-merge-queue bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Jul 15, 2025
Contract can now be expressed as
model-checking/kani#4151 has been merged.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
nilehmann pushed a commit to flux-rs/verify-rust-std that referenced this pull request Jul 16, 2025
Contract can now be expressed as
model-checking/kani#4151 has been merged.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
AlexanderPortland pushed a commit to AlexanderPortland/kani that referenced this pull request Jul 30, 2025
Rust believes that Kani's contract closures are `FnMut`. This prevents
us from writing contracts for functions that return mutable references
to their input arguments (model-checking#3764).

To ensure Rust correctly infers these closures as `FnOnce`, they need to
be wrapped in a dummy function that explicitly requires an `FnOnce`.
This wrapping must be done at the point of closure definition, as doing
it later, when calling the function, doesn't seem to have any effect.

Resolves model-checking#3764

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

---------

Co-authored-by: Fedor Ryabinin <rfedor@amazon.com>
Co-authored-by: Carolyn Zech <cmzech@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't write contracts for functions that return mutable references to input arguments
3 participants