Skip to content

Reborrowing mutable pointers is overly permissive #8624

Closed

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>

Activity

  1. metajack commented on Aug 19, 2013

    @metajack
    Contributor

    nominating production ready

  2. 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.

  3. bluss commented on Sep 10, 2013

    @bluss
    Contributor

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

  4. catamorphism commented on Sep 12, 2013

    @catamorphism
    Contributor

    Accepted backwards-compatible

  5. added a commit that references this issue on Nov 28, 2013
    09e12fa
  6. added a commit that references this issue on Nov 28, 2013
  7. added a commit that references this issue on Dec 1, 2013
  8. added a commit that references this issue on Apr 21, 2022
  9. added a commit that references this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-type-system

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions