Stabilization report
Summary
This feature allows panicking within constants and reporting a custom message as a compile-time error.
The following code will become valid on stable:
const fn foo(val: u8) {
assert!(val == 1, "val is not 1");
}
// can be also computed by manually creating a `&[u8]` and
// from_utf8_unchecked.
const MSG: &str = "custom message";
const _: () = std::panic!("{}", MSG);
const _: () = panic!("custom message");
The message looks like:
error: any use of this value will cause an error
--> src/main.rs:4:15
|
4 | const _: () = std::panic!("{}", MSG);
| --------------^^^^^^^^^^^^^^^^^^^^^^-
| |
| the evaluated program panicked at 'custom message', src/main.rs:4:15
|
= 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>
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
Motivation for this RFC can be found here: https://rust-lang.github.io/rfcs/2345-const-panic.html#motivation
More examples can be found here: https://github.com/rust-lang/rust/blob/673d0db5e393e9c64897005b470bfeb6d5aec61b/src/test/ui/consts/const-eval/const_panic.rs
Edge cases & tests
Some questions which should be resolved in the future
We have some "unresolved questions" but they aren't an actual blocker.
- Should there be some additional message in the error about this being a panic turned error?
Or do we just produce the exact message the panic would produce?
- This change becomes really useful if
Result::unwrap and Option::unwrap become const fn, doing both in one go might be a good idea.
See the brief summary comment here: #51999 (comment)
Previous attempt: #85194 (This was blocked because it was not possible to use a generated panic message, only literal strings were allowed, which has been resolved in #88954)
Originally posted by @JohnTitor in #51999 (comment)
cc @rust-lang/wg-const-eval
Stabilization report
Summary
This feature allows panicking within constants and reporting a custom message as a compile-time error.
The following code will become valid on stable:
The message looks like:
Motivation for this RFC can be found here: https://rust-lang.github.io/rfcs/2345-const-panic.html#motivation
More examples can be found here: https://github.com/rust-lang/rust/blob/673d0db5e393e9c64897005b470bfeb6d5aec61b/src/test/ui/consts/const-eval/const_panic.rs
Edge cases & tests
Some questions which should be resolved in the future
We have some "unresolved questions" but they aren't an actual blocker.
Or do we just produce the exact message the panic would produce?
Result::unwrapandOption::unwrapbecomeconst fn, doing both in one go might be a good idea.See the brief summary comment here: #51999 (comment)
Previous attempt: #85194 (This was blocked because it was not possible to use a generated panic message, only literal strings were allowed, which has been resolved in #88954)
Originally posted by @JohnTitor in #51999 (comment)
cc @rust-lang/wg-const-eval