Skip to content

Allow assert with custom message to be used in const contexts - #1858

Merged
zhassan-aws merged 2 commits into
model-checking:mainfrom
zhassan-aws:const-assert
Nov 7, 2022
Merged

zhassan-aws merged 2 commits into
model-checking:mainfrom
zhassan-aws:const-assert

Conversation

@zhassan-aws

Copy link
Copy Markdown
Contributor

Description of changes:

Currently, Kani fails to compile programs that use the assert macro with a custom message in a const context, e.g.

const fn my_const_fn(x: i32, y: i32) {
    assert!(x + y == 8, "message");
}

#[cfg_attr(kani, kani::proof)]
fn main() {
    let x = 5;
    let y = 3;
    my_const_fn(x, y);
}

This is due to Kani's overridden definition of the assert macro that uses the format_args macro, which cannot yet be used in a const context.

This PR switches the assert macro definition to use the panic macro instead, which achieves the same effect (parsing of the format argument and emitting any associate warnings/errors).

Resolved issues:

Resolves #1586

Related RFC:

Optional #ISSUE-NUMBER.

Call-outs:

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.

@zhassan-aws
zhassan-aws requested a review from a team as a code owner November 7, 2022 17:20
@zhassan-aws
zhassan-aws merged commit b8c999d into model-checking:main Nov 7, 2022
@zhassan-aws
zhassan-aws deleted the const-assert branch November 7, 2022 21:24
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.

"cannot call non-const fn kani::assert in constant functions"

2 participants