Skip to content

Make panic and assert const functions - #1590

Merged
zhassan-aws merged 2 commits into
model-checking:mainfrom
zhassan-aws:const-panic
Aug 26, 2022
Merged

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

Conversation

@zhassan-aws

@zhassan-aws zhassan-aws commented Aug 26, 2022 •

Copy link
Copy Markdown
Contributor

Description of changes:

Rust allows panic!, assert!, and assert_eq! to be used in const functions. Kani's override of the panic and assert macros is implemented via a panic and an assert function that are not marked as constant, which causes an error when those macros are used in a const function. This PR fixes the issue by adding const to the panic and assert functions.

Resolved issues:

Resolves #1586

Call-outs:

Testing:

  • How is this change tested? Added two tests

  • 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 August 26, 2022 07:04
Comment thread tests/kani/Panic/const.rs
pub fn check_something() {
let x: u8 = if kani::any() { 3 } else { 95 };
if x > 100 {
my_const_fn();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This call isn't reachable (x > 100 is always false). The test shows you can use panic! in a const fn but I think you want to also call it, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The impetus for this was definitely a compile-time error, so maybe not

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Correct, I'm intentionally not calling it so that the test passes. As Ted pointed out, compilation currently fails. The purpose of the kani::any() is to make sure the compiler doesn't optimize the function out (so we still have to codegen it).

@zhassan-aws zhassan-aws changed the title Make panic a const function Make panic and assert const functions Aug 26, 2022
@zhassan-aws

Copy link
Copy Markdown
Contributor Author

I updated the PR to make assert const as well since it's also allowed to be used in a const function: https://rust-lang.github.io/rfcs/2345-const-panic.html

@zhassan-aws
zhassan-aws merged commit 6a8e33f into model-checking:main Aug 26, 2022
@zhassan-aws
zhassan-aws deleted the const-panic branch August 26, 2022 17:45
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"

3 participants