Open
Description
Based on an example by @theemathas:
enum Never {}
static X: &Never = weird(&X);
const fn weird(a: &&Never) -> &'static Never {
// SAFETY: our argument type has an unsatisfiable
// library invariant; therefore, this code is unreachable.
unsafe { std::hint::unreachable_unchecked() };
}
error[E0080]: could not evaluate static initializer
--> src/lib.rs:3:20
|
3 | static X: &Never = weird(&X);
| ^^^^^^^^^ entering unreachable code
|
note: inside `weird`
--> src/lib.rs:8:14
|
8 | unsafe { std::hint::unreachable_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `unreachable_unchecked`
--> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/hint.rs:109:14
|
109 | unsafe { intrinsics::unreachable() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the failure occurred here
I can't see anything wrong about weird
. Therefore, this is UB from entirely sound code.
Cc @rust-lang/opsem @rust-lang/wg-const-eval