add test to detect dropped temporary
This commit is contained in:
parent
aa3bc06f0f
commit
21968aa53b
2 changed files with 14 additions and 0 deletions
14
tests/compile-fail/dangling_pointers/stack_temporary.rs
Normal file
14
tests/compile-fail/dangling_pointers/stack_temporary.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// This should fail even without validation, but some MIR opts mask the error
|
||||
// compile-flags: -Zmiri-disable-validation -Zmir-opt-level=0
|
||||
|
||||
unsafe fn make_ref<'a>(x: *mut i32) -> &'a mut i32 {
|
||||
&mut *x
|
||||
}
|
||||
|
||||
fn main() {
|
||||
unsafe {
|
||||
let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
|
||||
let val = *x; //~ ERROR dereferenced after this allocation got freed
|
||||
println!("{}", val);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue