2018-10-29 15:04:56 -05:00
|
|
|
// Test various stacked-borrows-related things.
|
|
|
|
fn main() {
|
|
|
|
deref_partially_dangling_raw();
|
2018-11-05 09:05:17 -06:00
|
|
|
read_does_not_invalidate1();
|
|
|
|
read_does_not_invalidate2();
|
2018-10-29 15:04:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deref a raw ptr to access a field of a large struct, where the field
|
|
|
|
// is allocated but not the entire struct is.
|
|
|
|
// For now, we want to allow this.
|
|
|
|
fn deref_partially_dangling_raw() {
|
|
|
|
let x = (1, 1);
|
|
|
|
let xptr = &x as *const _ as *const (i32, i32, i32);
|
|
|
|
let _val = unsafe { (*xptr).1 };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that reading from an `&mut` does, like reborrowing to `&`,
|
|
|
|
// NOT invalidate other reborrows.
|
2018-11-05 09:05:17 -06:00
|
|
|
fn read_does_not_invalidate1() {
|
2018-10-29 15:04:56 -05:00
|
|
|
fn foo(x: &mut (i32, i32)) -> &i32 {
|
|
|
|
let xraw = x as *mut (i32, i32);
|
|
|
|
let ret = unsafe { &(*xraw).1 };
|
|
|
|
let _val = x.1; // we just read, this does NOT invalidate the reborrows.
|
|
|
|
ret
|
|
|
|
}
|
2018-11-05 09:05:17 -06:00
|
|
|
foo(&mut (1, 2));
|
|
|
|
}
|
|
|
|
// Same as above, but this time we first create a raw, then read from `&mut`
|
|
|
|
// and then freeze from the raw.
|
|
|
|
fn read_does_not_invalidate2() {
|
|
|
|
fn foo(x: &mut (i32, i32)) -> &i32 {
|
|
|
|
let xraw = x as *mut (i32, i32);
|
|
|
|
let _val = x.1; // we just read, this does NOT invalidate the raw reborrow.
|
|
|
|
let ret = unsafe { &(*xraw).1 };
|
|
|
|
ret
|
|
|
|
}
|
2018-10-29 15:04:56 -05:00
|
|
|
foo(&mut (1, 2));
|
|
|
|
}
|