Skip to content

Reborrowing mutable pointers is overly permissive #8624

@nikomatsakis

Description

@nikomatsakis
Contributor

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>

Activity

metajack

metajack commented on Aug 19, 2013

@metajack
Contributor

nominating production ready

nikomatsakis

nikomatsakis commented on Sep 9, 2013

@nikomatsakis
ContributorAuthor

This is actually deserving of backwards compatible, because the fix will likely cause some APIs to be changed.

bluss

bluss commented on Sep 10, 2013

@bluss
Member

I think the mutable extra::dlist iterator is sound, but I have a feeling it is enabled by this hole.

catamorphism

catamorphism commented on Sep 12, 2013

@catamorphism
Contributor

Accepted backwards-compatible

17 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @metajack@nikomatsakis@catamorphism@bluss

      Issue actions

        Reborrowing mutable pointers is overly permissive · Issue #8624 · rust-lang/rust