rust/tests/fail/stacked_borrows/aliasing_mut4.rs
2022-06-20 23:50:40 -07:00

17 lines
517 B
Rust

use std::cell::Cell;
use std::mem;
// Make sure &mut UnsafeCell also is exclusive
pub fn safe(_x: &i32, _y: &mut Cell<i32>) {} //~ ERROR protect
fn main() {
let mut x = 0;
let xref = &mut x;
let xraw: *mut i32 = unsafe { mem::transmute_copy(&xref) };
let xshr = &*xref;
// transmute fn ptr around so that we can avoid retagging
let safe_raw: fn(x: *const i32, y: *mut Cell<i32>) =
unsafe { mem::transmute::<fn(&i32, &mut Cell<i32>), _>(safe) };
safe_raw(xshr, xraw as *mut _);
}