rust/tests/compile-fail/storage_dead_dangling.rs
2020-09-20 12:18:02 +02:00

23 lines
465 B
Rust

// This should fail even without validation, but some MIR opts mask the error
// compile-flags: -Zmiri-disable-validation -Zmir-opt-level=0
static mut LEAK: usize = 0;
fn fill(v: &mut i32) {
unsafe { LEAK = v as *mut _ as usize; }
}
fn evil() {
unsafe { &mut *(LEAK as *mut i32) }; //~ ERROR dereferenced after this allocation got freed
}
fn main() {
let _y;
{
let mut x = 0i32;
fill(&mut x);
_y = x;
}
evil();
}