Skip to content

Commit aa8f523

Browse files
committed
test for special things that are now possible
1 parent 94e7512 commit aa8f523

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/run-pass/stacked-borrows.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

0 commit comments

Comments
 (0)