Skip to content

Commit de4ac0c

Browse files
committed
add const eval bool-to-int cast test
1 parent db44cae commit de4ac0c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const fn bool_cast(ptr: *const bool) { unsafe {
2+
let _val = *ptr as u32; //~ERROR: evaluation of constant value failed
3+
//~^ interpreting an invalid 8-bit value as a bool
4+
}}
5+
6+
const _: () = {
7+
let v = 3_u8;
8+
bool_cast(&v as *const u8 as *const bool);
9+
};
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/ub-invalid-values.rs:2:16
3+
|
4+
LL | let _val = *ptr as u32;
5+
| ^^^^^^^^^^^ interpreting an invalid 8-bit value as a bool: 0x03
6+
|
7+
note: inside `bool_cast`
8+
--> $DIR/ub-invalid-values.rs:2:16
9+
|
10+
LL | let _val = *ptr as u32;
11+
| ^^^^^^^^^^^
12+
note: inside `_`
13+
--> $DIR/ub-invalid-values.rs:8:5
14+
|
15+
LL | bool_cast(&v as *const u8 as *const bool);
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17+
18+
error: aborting due to 1 previous error
19+
20+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)