-
Notifications
You must be signed in to change notification settings - Fork 95
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
"cannot call non-const fn kani::assert
in constant functions"
#1586
Comments
Do you have an example? |
Good point. I believe this was the |
Hello, I'm trying kani and ran into the same issue. The codebase I want to analyze uses
|
Hmm. This is possibly not yet completely solved. It did fix most of the issues I saw but I ran into this:
Which slightly confuses me. I think it might be triggerable just by building with the |
Reproduction steps:
|
The non-const |
Currently, the const fn const_fn() {
assert!(1 + 1 == 2, "A message");
}
fn main() {
const_fn();
} $ rustc assert.rs && ./assert
$ This one isn't: const fn const_fn() {
format_args!("A message");
}
fn main() {
const_fn();
} $ rustc assert.rs && ./assert
error: `Arguments::<'a>::new_v1` is not yet stable as a const fn
--> assert.rs:2:5
|
2 | format_args!("A message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
error: erroneous constant used
--> assert.rs:2:18
|
2 | format_args!("A message");
| ^^^^^^^^^^^ referenced constant has errors
|
= note: `#[deny(const_err)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
error: erroneous constant used
--> assert.rs:2:5
|
2 | format_args!("A message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
= note: this error originates in the macro `format_args` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors A workaround is to add:
to the root of the crate in which the This issue should be resolved if/when |
This got hit again: time-rs/time#521 |
I think we need to redefine the assert macro macro_rules! assert {
($cond:expr $(,)?) => {
kani::assert($cond, concat!("assertion failed: ", stringify!($cond)));
};
($cond:expr, $fmt:literal $(,)?) => {{
kani::assert($cond, concat!(stringify!($fmt)));
if false {
let _ = $fmt;
}
}};
($cond:expr, $fmt:literal, $($arg:tt)+) => {{
kani::assert($cond, concat!(stringify!($fmt, $($arg)+)));
if false {
let _ = format_args!($fmt, $($arg)+);
}
}};
} |
Thanks @camshaft for the great suggestion! The only downside is that Kani would fail to report errors/warnings in the format literal, e.g. #[cfg(kani)]
macro_rules! assert {
($cond:expr $(,)?) => {
kani::assert($cond, concat!("assertion failed: ", stringify!($cond)));
};
($cond:expr, $fmt:literal $(,)?) => {{
kani::assert($cond, concat!(stringify!($fmt)));
if false {
let _ = $fmt;
}
}};
($cond:expr, $fmt:literal, $($arg:tt)+) => {{
kani::assert($cond, concat!(stringify!($fmt, $($arg)+)));
if false {
let _ = format_args!($fmt, $($arg)+);
}
}};
}
#[cfg_attr(kani, kani::proof)]
fn main() {
assert!(1 + 1 == 2, "Hello world {}");
} rustc:
rustc 2021:
Kani
but perhaps this is OK until |
Yeah that's a good point. Could you forward the string on to panic instead? if false {
::std::panic!($($arg)+);
} |
That's a great idea! |
D'oh, this still doesn't work with cf017e8 for const panics, e.g.: const fn my_const_fn(msg: &str) -> ! {
panic!("{}", msg)
}
#[cfg_attr(kani, kani::proof)]
fn main() {
my_const_fn("failed");
}
Re-opening. |
Having a poke at building top-100 crates with Kani. Found this one frequently.
Cause is typically an
assert!
orpanic!
etc in a constant function. Probably we just need to makekani::assert
and friends const?The text was updated successfully, but these errors were encountered: