Skip to content
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

Span of type error in contracts could be narrowed #3009

Closed
pnkfelix opened this issue Feb 8, 2024 · 3 comments · Fixed by #3526
Closed

Span of type error in contracts could be narrowed #3009

pnkfelix opened this issue Feb 8, 2024 · 3 comments · Fixed by #3526
Assignees
Labels
[C] Feature / Enhancement A new feature request or enhancement to an existing feature. T-User Tag user issues / requests Z-Contracts Issue related to code contracts

Comments

@pnkfelix
Copy link
Contributor

pnkfelix commented Feb 8, 2024

I was having a go at transcribing the example from the recent blog post, and I encountered a sub-optimal diagnostic issue.

Test case:

#[kani::ensures(a % result && b % result == 0 && result != 0)]
fn gcd(a: u64, b: u64) -> u64 {
    0
}

The above currently generates the following from Kani:

error[E0308]: mismatched types
 --> src/main.rs:1:1
  |
1 | #[kani::ensures(a % result && b % result == 0 && result != 0)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `u64`
  |
  = note: this error originates in the attribute macro `kani::ensures` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0308]: mismatched types
 --> src/main.rs:1:1
  |
1 | #[kani::ensures(a % result && b % result == 0 && result != 0)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `u64`
  |
  = note: this error originates in the attribute macro `kani::ensures` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

(I'm not sure why it prints two copies of the error message.)

I had a hard time seeing what was wrong, since the highlighted span was overly broad.

If you were to encode the contract as an assertion at the end of the method body and then invoke rustc, it produces a more precise diagnostic message:

error[E0308]: mismatched types
  --> src/main.rs:13:13
   |
13 | ...t!(a % result &&...
   |       ^^^^^^^^^^ expected `bool`, found `u64`

error: aborting due to 1 previous error

You can see how this more immediately reveals the source of the problem.

@pnkfelix pnkfelix added the [C] Feature / Enhancement A new feature request or enhancement to an existing feature. label Feb 8, 2024
@pnkfelix
Copy link
Contributor Author

pnkfelix commented Feb 8, 2024

(Oh, I should try with -Z macro-backtrace and see if that's any better... but, no, Kani does not support passing that flag along...)

@zhassan-aws
Copy link
Contributor

Indeed, the error message is pretty vague.

Oh, I should try with -Z macro-backtrace and see if that's any better... but, no, Kani does not support passing that flag along...)

It supports it, but the extra diags are not that helpful either:

$ RUSTFLAGS="-Zmacro-backtrace" kani iss3009.rs 
Kani Rust Verifier 0.45.0 (standalone)
error[E0308]: mismatched types
   --> iss3009.rs:1:1
    |
1   | #[kani::ensures(a % result && b % result == 0 && result != 0)]
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    | |
    | expected `bool`, found `u64`
    | in this procedural macro expansion
    |
   ::: /home/ubuntu/git/kani/library/kani_macros/src/lib.rs:135:1
    |
135 | pub fn ensures(attr: TokenStream, item: TokenStream) -> TokenStream {
    | ------------------------------------------------------------------- in this expansion of `#[kani::ensures]`

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
error: /home/ubuntu/git/kani/target/kani/bin/kani-compiler exited with status exit status: 1

@zhassan-aws zhassan-aws added the T-User Tag user issues / requests label Feb 8, 2024
@JustusAdam
Copy link
Contributor

I can answer why it come up twice. Kani code-generates two functions from your contract, one that checks it and one that can be used to replace it. Both use the code in your contract so by the time that the rust type checker gets to it your code exists in two places ;)

@feliperodri feliperodri self-assigned this Feb 28, 2024
@feliperodri feliperodri added this to the Function Contracts MVP milestone Jun 6, 2024
@feliperodri feliperodri changed the title Span of type error in contacts could be narrowed Span of type error in contracts could be narrowed Jun 27, 2024
@tautschnig tautschnig added the Z-Contracts Issue related to code contracts label Aug 1, 2024
github-merge-queue bot pushed a commit that referenced this issue Sep 18, 2024
Kani v0.55 no longer has the overly broad span issue reported in #3009.
I suspect that our shift (#3363) from functions to closures for
contracts allows rustc to produce better error messages. Add tests to
prevent regression in the future.

Resolves #3009 

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[C] Feature / Enhancement A new feature request or enhancement to an existing feature. T-User Tag user issues / requests Z-Contracts Issue related to code contracts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants