Skip to content

Conversation

@zhassan-aws
Copy link
Contributor

This PR adds a new unstable option, --no-codegen, that can be used for running Kani, just to check if the code compiles, i.e. similar to running cargo check. This is useful as a lightweight check for compilation errors including code under #[cfg(kani)].

This is implemented through passing rustc's -Zno-codegen option, which is what cargo check passes to rustc.

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

@carolynzech
Copy link
Contributor

If I run kani src/lib.rs -Z function-contracts on the following:

// Error: not a boolean expression
#[kani::requires(x - 2)]
fn main(x: u32) {
    let x: i32 = 5;
    // Error: different types
    let y: u32 = x;
    assert_eq!(y, 5);
}

I get these error messages:

error[E0308]: mismatched types
  --> src/fail.rs:2:18
   |
2  | #[kani::requires(x - 2)]
   | -----------------^^^^^--
   | |                |
   | |                expected `bool`, found `u32`
   | arguments to this function are incorrect
   |
note: function defined here
  --> /Users/cmzech/kani/library/kani/src/lib.rs:54:1
   |
54 | kani_core::kani_lib!(kani);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `kani_core::kani_intrinsics` which comes from the expansion of the macro `kani_core::kani_lib` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
 --> src/fail.rs:6:18
  |
6 |     let y: u32 = x;
  |            ---   ^ expected `u32`, found `i32`
  |            |
  |            expected due to this
  |
help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit
  |
6 |     let y: u32 = x.try_into().unwrap();
  |                   ++++++++++++++++++++

So my question is: how is this option better than just running Kani and seeing what happens? These type errors will cause Kani to abort before codegen already.

@zhassan-aws
Copy link
Contributor Author

Right, but the point is the behavior in the case where there are no errors. This option makes it terminate immediately without going through codegen (similar to cargo check).

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.

Two documentation nits, take them or leave them. The changes in phrasing are meant to make clearer that this is more useful than the default in the success case.

@zhassan-aws zhassan-aws requested a review from a team as a code owner April 10, 2025 16:48
@zhassan-aws zhassan-aws enabled auto-merge April 10, 2025 17:03
@zhassan-aws zhassan-aws added this pull request to the merge queue Apr 10, 2025
Merged via the queue into model-checking:main with commit 1537c29 Apr 10, 2025
26 checks passed
@zhassan-aws zhassan-aws deleted the no-codegen-opt branch April 10, 2025 18:54
zhassan-aws added a commit that referenced this pull request Apr 23, 2025
This PR adds a new unstable option, `--no-codegen`, that can be used for
running Kani, just to check if the code compiles, i.e. similar to
running `cargo check`. This is useful as a lightweight check for
compilation errors including code under `#[cfg(kani)]`.

This is implemented through passing rustc's `-Zno-codegen` option, which
is what `cargo check` passes to `rustc`.

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: Carolyn Zech <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants