rust/tests/compile-fail/validity/transmute_through_ptr.rs

17 lines
454 B
Rust
Raw Normal View History

#[repr(u32)]
#[derive(Debug)]
enum Bool { True }
fn evil(x: &mut Bool) {
let x = x as *mut _ as *mut u32;
2020-06-20 04:48:42 -05:00
unsafe { *x = 44; } // out-of-bounds enum tag
}
fn main() {
let mut x = Bool::True;
evil(&mut x);
let y = x; // reading this ought to be enough to trigger validation
2020-07-05 06:43:20 -05:00
//~^ ERROR encountered 0x0000002c at .<enum-tag>, but expected a valid enum tag
println!("{:?}", y); // make sure it is used (and not optimized away)
}