]> git.lizzy.rs Git - rust.git/blob - src/tools/miri/tests/fail/branchless-select-i128-pointer.rs
Rollup merge of #100462 - zohnannor:master, r=thomcc
[rust.git] / src / tools / miri / tests / fail / branchless-select-i128-pointer.rs
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         // However, it drops provenance when transmuting to TwoPtrs, so this is UB.
13         let val = unsafe {
14             transmute::<_, &str>(
15                 //~^ ERROR: constructing invalid value: encountered a dangling reference
16                 !mask & transmute::<_, TwoPtrs>("false !")
17                     | mask & transmute::<_, TwoPtrs>("true !"),
18             )
19         };
20         println!("{}", val);
21     }
22 }