Skip to content

Commit 4c867fe

Browse files
committed
add test by @eddyb
1 parent 0b3dba8 commit 4c867fe

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::mem::transmute;
2+
3+
#[cfg(target_pointer_width = "32")]
4+
type TwoPtrs = i64;
5+
#[cfg(target_pointer_width = "64")]
6+
type TwoPtrs = i128;
7+
8+
fn main() {
9+
for &my_bool in &[true, false] {
10+
let mask = -(my_bool as TwoPtrs); // false -> 0, true -> -1 aka !0
11+
// This is branchless code to select one or the other pointer.
12+
// For now, Miri brafs on it, but if this code ever passes we better make sure it behaves correctly.
13+
let val = unsafe {
14+
transmute::<_, &str>(
15+
!mask & transmute::<_, TwoPtrs>("false !") | mask & transmute::<_, TwoPtrs>("true !"), //~ERROR encountered (potentially part of) a pointer, but expected plain (non-pointer) bytes
16+
)
17+
};
18+
println!("{}", val);
19+
}
20+
}

0 commit comments

Comments
 (0)