Evaluating constants in MIR optimizations introduces const eval errors #122828
Open
Description
opened on Mar 21, 2024
#![feature(inline_const)]
#![crate_type = "lib"]
pub fn f<T>() -> usize {
g::<()>()
}
pub fn g<T>() -> usize {
const { 0 / std::mem::size_of::<T>() }
}
This compiles fine in debug builds, but fails in release builds:
$ rustc b.rs
$ rustc b.rs -O
error[E0080]: evaluation of `g::<()>::{constant#0}` failed
--> b.rs:7:13
|
7 | const { 0 / std::mem::size_of::<T>() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to divide `0_usize` by zero
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0080`.
Based on #107503 (comment) cc @RalfJung.
But also see this example based on type sizes.
Activity