From c45e9c86ca5e1aa20ec8ec9904c4ad9a33a072e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Naz=C4=B1m=20Can=20Alt=C4=B1nova?= Date: Fri, 17 Jul 2020 22:03:33 +0200 Subject: [PATCH] Add a test for const unsafe_unreachable that triggers UB --- .../ui/consts/const_unsafe_unreachable_ub.rs | 15 ++++++++++++++ .../consts/const_unsafe_unreachable_ub.stderr | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/test/ui/consts/const_unsafe_unreachable_ub.rs create mode 100644 src/test/ui/consts/const_unsafe_unreachable_ub.stderr diff --git a/src/test/ui/consts/const_unsafe_unreachable_ub.rs b/src/test/ui/consts/const_unsafe_unreachable_ub.rs new file mode 100644 index 000000000000..2e4dfd1522b9 --- /dev/null +++ b/src/test/ui/consts/const_unsafe_unreachable_ub.rs @@ -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); +} diff --git a/src/test/ui/consts/const_unsafe_unreachable_ub.stderr b/src/test/ui/consts/const_unsafe_unreachable_ub.stderr new file mode 100644 index 000000000000..0a7aa4e00214 --- /dev/null +++ b/src/test/ui/consts/const_unsafe_unreachable_ub.stderr @@ -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 +