File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ fn main() {
44 read_does_not_invalidate1 ( ) ;
55 read_does_not_invalidate2 ( ) ;
66 ref_raw_int_raw ( ) ;
7+ mut_shr_raw ( ) ;
8+ mut_raw_then_mut_shr ( ) ;
79}
810
911// Deref a raw ptr to access a field of a large struct, where the field
@@ -48,3 +50,29 @@ fn ref_raw_int_raw() {
4850 let xraw = xref as * mut i32 as usize as * mut i32 ;
4951 assert_eq ! ( unsafe { * xraw } , 3 ) ;
5052}
53+
54+ // Creating a raw from a `&mut` through an `&` works, even if we
55+ // write through that raw.
56+ fn mut_shr_raw ( ) {
57+ let mut x = 2 ;
58+ {
59+ let xref = & mut x;
60+ let xraw = & * xref as * const i32 as * mut i32 ;
61+ unsafe { * xraw = 4 ; }
62+ }
63+ assert_eq ! ( x, 4 ) ;
64+ }
65+
66+ // Escape a mut to raw, then share the same mut and use the share, then the raw.
67+ // That should work.
68+ fn mut_raw_then_mut_shr ( ) {
69+ let mut x = 2 ;
70+ {
71+ let xref = & mut x;
72+ let xraw = & mut * xref as * mut _ ;
73+ let xshr = & * xref;
74+ assert_eq ! ( * xshr, 2 ) ;
75+ unsafe { * xraw = 4 ; }
76+ }
77+ assert_eq ! ( x, 4 ) ;
78+ }
You can’t perform that action at this time.
0 commit comments