Skip to content

Commit

Permalink
Make panic and assert const functions (#1590)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhassan-aws authored Aug 26, 2022
1 parent 22316d1 commit 6a8e33f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/kani/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn assume(_cond: bool) {
/// ```
#[inline(never)]
#[rustc_diagnostic_item = "KaniAssert"]
pub fn assert(_cond: bool, _msg: &'static str) {
pub const fn assert(_cond: bool, _msg: &'static str) {
if cfg!(feature = "concrete_playback") {
assert!(_cond, "{}", _msg);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn expect_fail(_cond: bool, _message: &'static str) {
#[inline(never)]
#[rustc_diagnostic_item = "KaniPanic"]
#[doc(hidden)]
pub fn panic(message: &'static str) -> ! {
pub const fn panic(message: &'static str) -> ! {
panic!("{}", message)
}

Expand Down
15 changes: 15 additions & 0 deletions tests/kani/Assert/in_const_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! This test checks that `assert!` can be used in a const fn
const fn const_add(x: i32, y: i32) {
assert!(x + y == x);
}

#[kani::proof]
fn check() {
let x = kani::any();
let y = 0;
const_add(x, y);
}
16 changes: 16 additions & 0 deletions tests/kani/Panic/const.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! This test checks that `panic!` can be used in a const fn
const fn my_const_fn() {
panic!()
}

#[kani::proof]
pub fn check_something() {
let x: u8 = if kani::any() { 3 } else { 95 };
if x > 100 {
my_const_fn();
}
}

0 comments on commit 6a8e33f

Please sign in to comment.