Add a test for const unsafe_unreachable that triggers UB

This commit is contained in:
Nazım Can Altınova 2020-07-17 22:03:33 +02:00
parent 2f28d5945d
commit c45e9c86ca
2 changed files with 35 additions and 0 deletions

View file

@ -0,0 +1,15 @@
#![feature(const_fn)]
#![feature(const_unreachable_unchecked)]
const unsafe fn foo(x: bool) -> bool {
match x {
true => true,
false => std::hint::unreachable_unchecked(),
}
}
const BAR: bool = unsafe { foo(false) };
fn main() {
assert_eq!(BAR, true);
}

View file

@ -0,0 +1,20 @@
error: any use of this value will cause an error
--> $SRC_DIR/libcore/hint.rs:LL:COL
|
LL | unsafe { intrinsics::unreachable() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| entering unreachable code
| inside `std::hint::unreachable_unchecked` at $SRC_DIR/libcore/hint.rs:LL:COL
| inside `foo` at $DIR/const_unsafe_unreachable_ub.rs:7:18
| inside `BAR` at $DIR/const_unsafe_unreachable_ub.rs:11:28
|
::: $DIR/const_unsafe_unreachable_ub.rs:11:1
|
LL | const BAR: bool = unsafe { foo(false) };
| ----------------------------------------
|
= note: `#[deny(const_err)]` on by default
error: aborting due to previous error