diff --git a/tests/run-pass/stacked-borrows.rs b/tests/run-pass/stacked-borrows.rs index adab2b5a568..00b21c746c2 100644 --- a/tests/run-pass/stacked-borrows.rs +++ b/tests/run-pass/stacked-borrows.rs @@ -4,6 +4,8 @@ fn main() { read_does_not_invalidate1(); read_does_not_invalidate2(); ref_raw_int_raw(); + mut_shr_raw(); + mut_raw_then_mut_shr(); } // Deref a raw ptr to access a field of a large struct, where the field @@ -48,3 +50,29 @@ fn ref_raw_int_raw() { let xraw = xref as *mut i32 as usize as *mut i32; assert_eq!(unsafe { *xraw }, 3); } + +// Creating a raw from a `&mut` through an `&` works, even if we +// write through that raw. +fn mut_shr_raw() { + let mut x = 2; + { + let xref = &mut x; + let xraw = &*xref as *const i32 as *mut i32; + unsafe { *xraw = 4; } + } + assert_eq!(x, 4); +} + +// Escape a mut to raw, then share the same mut and use the share, then the raw. +// That should work. +fn mut_raw_then_mut_shr() { + let mut x = 2; + { + let xref = &mut x; + let xraw = &mut *xref as *mut _; + let xshr = &*xref; + assert_eq!(*xshr, 2); + unsafe { *xraw = 4; } + } + assert_eq!(x, 4); +}