Auto merge of #1036 - RalfJung:stacked-borrows-test, r=RalfJung
add an interesting run-pass stacked borrows example
This commit is contained in:
commit
9f0b99bae3
@ -11,6 +11,7 @@ fn main() {
|
|||||||
direct_mut_to_const_raw();
|
direct_mut_to_const_raw();
|
||||||
two_raw();
|
two_raw();
|
||||||
shr_and_raw();
|
shr_and_raw();
|
||||||
|
disjoint_mutable_subborrows();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
|
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
|
||||||
@ -138,3 +139,31 @@ fn shr_and_raw() { /* unsafe {
|
|||||||
*y2 += 1;
|
*y2 += 1;
|
||||||
// TODO: Once this works, add compile-fail test that tries to read from y1 again.
|
// 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);
|
||||||
|
}
|
||||||
|
7
tests/run-pass/stacked-borrows/stacked-borrows.stderr
Normal file
7
tests/run-pass/stacked-borrows/stacked-borrows.stderr
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
[$DIR/stacked-borrows.rs:168] a = "hello world"
|
||||||
|
[$DIR/stacked-borrows.rs:168] b = [
|
||||||
|
0,
|
||||||
|
1,
|
||||||
|
2,
|
||||||
|
4,
|
||||||
|
]
|
Loading…
x
Reference in New Issue
Block a user