about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNazım Can Altınova <canaltinova@gmail.com>2020-07-17 22:03:33 +0200
committerNazım Can Altınova <canaltinova@gmail.com>2020-07-17 22:03:33 +0200
commitc45e9c86ca5e1aa20ec8ec9904c4ad9a33a072e5 (patch)
tree67cbf84c100d88483c93ac57d996c4eb20bb88ec
parent2f28d5945dab495a77c67c6051eea116ce9ceee2 (diff)
downloadrust-c45e9c86ca5e1aa20ec8ec9904c4ad9a33a072e5.tar.gz
rust-c45e9c86ca5e1aa20ec8ec9904c4ad9a33a072e5.zip
Add a test for const unsafe_unreachable that triggers UB
-rw-r--r--src/test/ui/consts/const_unsafe_unreachable_ub.rs15
-rw-r--r--src/test/ui/consts/const_unsafe_unreachable_ub.stderr20
2 files changed, 35 insertions, 0 deletions
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 00000000000..2e4dfd1522b
--- /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 00000000000..0a7aa4e0021
--- /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
+