Skip to content

Commit 7c66eaf

Browse files
committed
Auto merge of #1766 - RalfJung:dropped-temporary, r=RalfJung
add test to detect dropped temporary Let's make sure we catch this kind of error.
2 parents d0a4b5d + 21968aa commit 7c66eaf

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This should fail even without validation, but some MIR opts mask the error
2+
// compile-flags: -Zmiri-disable-validation -Zmir-opt-level=0
3+
4+
unsafe fn make_ref<'a>(x: *mut i32) -> &'a mut i32 {
5+
&mut *x
6+
}
7+
8+
fn main() {
9+
unsafe {
10+
let x = make_ref(&mut 0); // The temporary storing "0" is deallocated at the ";"!
11+
let val = *x; //~ ERROR dereferenced after this allocation got freed
12+
println!("{}", val);
13+
}
14+
}

0 commit comments

Comments
 (0)