Closed
Description
This example program should not type check:
struct S<'self> {
pointer: &'self mut int
}
fn copy_borrowed_ptr<'a, 'b>(p: &'a mut S<'b>) -> S<'b> {
S { pointer: &mut *p.pointer }
}
fn main() {
let mut x = 1;
let mut y = S { pointer: &mut x };
let z = copy_borrowed_ptr(&mut y);
*y.pointer += 1;
*z.pointer += 1;
}
The problem is that the rules which limit reborrowing of mut data to unique paths do not consider the lifetime of those unique paths.
The legal signatures for copy_borrowed_ptr
would be either:
fn copy_borrowed_ptr<'a>(&'a mut S<'a>) -> S<'a>
; orfn copy_borrowed_ptr<'a, 'b>(&'a mut S<'b>) -> S<'a>