Skip to content

Reborrowing mutable pointers is overly permissive #8624

Closed
@nikomatsakis

Description

@nikomatsakis

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>; or
  • fn copy_borrowed_ptr<'a, 'b>(&'a mut S<'b>) -> S<'a>

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions