Skip to content

Commit c45e9c8

Browse files
committed
Add a test for const unsafe_unreachable that triggers UB
1 parent 2f28d59 commit c45e9c8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(const_fn)]
2+
#![feature(const_unreachable_unchecked)]
3+
4+
const unsafe fn foo(x: bool) -> bool {
5+
match x {
6+
true => true,
7+
false => std::hint::unreachable_unchecked(),
8+
}
9+
}
10+
11+
const BAR: bool = unsafe { foo(false) };
12+
13+
fn main() {
14+
assert_eq!(BAR, true);
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: any use of this value will cause an error
2+
--> $SRC_DIR/libcore/hint.rs:LL:COL
3+
|
4+
LL | unsafe { intrinsics::unreachable() }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| entering unreachable code
8+
| inside `std::hint::unreachable_unchecked` at $SRC_DIR/libcore/hint.rs:LL:COL
9+
| inside `foo` at $DIR/const_unsafe_unreachable_ub.rs:7:18
10+
| inside `BAR` at $DIR/const_unsafe_unreachable_ub.rs:11:28
11+
|
12+
::: $DIR/const_unsafe_unreachable_ub.rs:11:1
13+
|
14+
LL | const BAR: bool = unsafe { foo(false) };
15+
| ----------------------------------------
16+
|
17+
= note: `#[deny(const_err)]` on by default
18+
19+
error: aborting due to previous error
20+

0 commit comments

Comments
 (0)