Auto merge of #1036 - RalfJung:stacked-borrows-test, r=RalfJung

add an interesting run-pass stacked borrows example
This commit is contained in:
bors 2019-11-05 10:05:59 +00:00
commit 9f0b99bae3
2 changed files with 36 additions and 0 deletions

View File

@ -11,6 +11,7 @@ fn main() {
direct_mut_to_const_raw();
two_raw();
shr_and_raw();
disjoint_mutable_subborrows();
}
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
@ -138,3 +139,31 @@ fn shr_and_raw() { /* unsafe {
*y2 += 1;
// TODO: Once this works, add compile-fail test that tries to read from y1 again.
} */ }
fn disjoint_mutable_subborrows() {
struct Foo {
a: String,
b: Vec<u32>,
}
unsafe fn borrow_field_a<'a>(this:*mut Foo) -> &'a mut String {
&mut (*this).a
}
unsafe fn borrow_field_b<'a>(this:*mut Foo) -> &'a mut Vec<u32> {
&mut (*this).b
}
let mut foo = Foo {
a: "hello".into(),
b: vec![0,1,2],
};
let ptr = &mut foo as *mut Foo;
let a = unsafe{ borrow_field_a(ptr) };
let b = unsafe{ borrow_field_b(ptr) };
b.push(4);
a.push_str(" world");
dbg!(a,b);
}

View File

@ -0,0 +1,7 @@
[$DIR/stacked-borrows.rs:168] a = "hello world"
[$DIR/stacked-borrows.rs:168] b = [
0,
1,
2,
4,
]