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

Allow &bool in asserts #2117

Merged
merged 2 commits into from
Jan 13, 2023
Merged

Allow &bool in asserts #2117

merged 2 commits into from
Jan 13, 2023

Conversation

zhassan-aws
Copy link
Contributor

Description of changes:

From investigating #2108, it turns out that due to how the standard library expands assert!(x) as

    if !x {
        ::core::panicking::panic("assertion failed: x")
    };

the following is valid Rust code:

fn main() {
    let b: bool = true;
    assert!(&b);
}

because it gets expanded to:

fn main() {
    let b: bool = true;
    if !&b {
        ::core::panicking::panic("assertion failed: &b")
    }
}

which, however, is rejected by Kani because &b is not a boolean (only !&b is!).

This PR adds a hacky fix, which is to inject !! (i.e. double negation) before the condition.

Resolved issues:

Resolves #2108

Related RFC:

Optional #ISSUE-NUMBER.

Call-outs:

TBH, I'm not sure about whether this should be merged as it is quite hacky. We should look into where this occurs, and why it is valid.

Testing:

  • How is this change tested? Added a new test

  • Is this a refactor change? No

Checklist

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

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

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

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

Did you try defining kani::assert() using AsRef<bool> instead?

@celinval
Copy link
Contributor

Did you try defining kani::assert() using AsRef<bool> instead?

Actually, I don't think that will work. :(

@tedinski
Copy link
Contributor

Seems fine, can we change the PR name to something slightly more customer-facing in advance of merging? "Accept &bool in asserts, like Rust's assert macro does" perhaps

@zhassan-aws zhassan-aws changed the title Add double negation in assert macro definition Allow &bool in asserts Jan 13, 2023
@zhassan-aws
Copy link
Contributor Author

can we change the PR name to something slightly more customer-facing in advance of merging?

Done.

@zhassan-aws zhassan-aws merged commit ce697e6 into model-checking:main Jan 13, 2023
@zhassan-aws zhassan-aws deleted the iss2108 branch January 13, 2023 17:56
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.

Kani assert override is more restrictive than standard assert
3 participants