rust/tests/compile-fail/transmute-pair-undef.rs

23 lines
751 B
Rust
Raw Normal View History

2017-07-03 15:57:18 -05:00
#![feature(core_intrinsics)]
2017-07-03 18:16:05 -05:00
use std::mem;
2017-07-03 15:57:18 -05:00
fn main() {
let x: Option<Box<[u8]>> = unsafe {
let z = std::intrinsics::add_with_overflow(0usize, 0usize);
std::mem::transmute::<(usize, bool), Option<Box<[u8]>>>(z)
};
let y = &x;
2019-02-15 19:43:56 -06:00
// Now read this bytewise. There should be (`ptr_size + 1`) def bytes followed by
// (`ptr_size - 1`) undef bytes (the padding after the bool) in there.
2017-07-03 15:57:18 -05:00
let z : *const u8 = y as *const _ as *const _;
2017-07-03 18:16:05 -05:00
let first_undef = mem::size_of::<usize>() as isize + 1;
for i in 0..first_undef {
2017-07-03 15:57:18 -05:00
let byte = unsafe { *z.offset(i) };
assert_eq!(byte, 0);
}
2017-07-03 18:16:05 -05:00
let v = unsafe { *z.offset(first_undef) };
2019-02-15 19:43:56 -06:00
if v == 0 {}
//~^ ERROR attempted to read undefined bytes
2017-07-03 15:57:18 -05:00
}