2018-10-17 09:55:59 -05:00
|
|
|
static mut PTR: *mut u8 = 0 as *mut _;
|
|
|
|
|
|
|
|
fn fun1(x: &mut u8) {
|
|
|
|
unsafe {
|
|
|
|
PTR = x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fun2() {
|
|
|
|
// Now we use a pointer we are not allowed to use
|
2018-10-22 11:01:32 -05:00
|
|
|
let _x = unsafe { *PTR }; //~ ERROR does not exist on the stack
|
2018-10-17 09:55:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2018-12-02 04:14:24 -06:00
|
|
|
let mut val = 0;
|
|
|
|
let val = &mut val;
|
2018-10-17 09:55:59 -05:00
|
|
|
fun1(val);
|
|
|
|
*val = 2; // this invalidates any raw ptrs `fun1` might have created.
|
|
|
|
fun2(); // if they now use a raw ptr they break our reference
|
|
|
|
}
|