2021-02-16 02:55:44 -06:00
|
|
|
use std::mem::transmute;
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
type TwoPtrs = i64;
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
type TwoPtrs = i128;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
for &my_bool in &[true, false] {
|
|
|
|
let mask = -(my_bool as TwoPtrs); // false -> 0, true -> -1 aka !0
|
|
|
|
// This is branchless code to select one or the other pointer.
|
2022-06-03 07:46:22 -05:00
|
|
|
// However, it drops provenance when transmuting to TwoPtrs, so this is UB.
|
2021-02-16 02:55:44 -06:00
|
|
|
let val = unsafe {
|
2022-06-22 00:56:50 -05:00
|
|
|
transmute::<_, &str>(
|
2022-06-22 01:03:34 -05:00
|
|
|
//~^ ERROR type validation failed: encountered a dangling reference
|
2022-06-22 00:56:50 -05:00
|
|
|
!mask & transmute::<_, TwoPtrs>("false !")
|
|
|
|
| mask & transmute::<_, TwoPtrs>("true !"),
|
2021-02-16 02:55:44 -06:00
|
|
|
)
|
|
|
|
};
|
|
|
|
println!("{}", val);
|
|
|
|
}
|
|
|
|
}
|