Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reborrowing mutable pointers is overly permissive #8624

Closed
nikomatsakis opened this issue Aug 19, 2013 · 4 comments
Closed

Reborrowing mutable pointers is overly permissive #8624

nikomatsakis opened this issue Aug 19, 2013 · 4 comments
Assignees
Labels
A-type-system Area: Type system
Milestone

Comments

@nikomatsakis
Copy link
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>
@metajack
Copy link
Contributor

nominating production ready

@nikomatsakis
Copy link
Contributor Author

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

@bluss
Copy link
Member

bluss commented Sep 10, 2013

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

@catamorphism
Copy link
Contributor

Accepted backwards-compatible

bors added a commit that referenced this issue Nov 28, 2013
bors added a commit that referenced this issue Dec 1, 2013
mut_chunks() returns an iterator that produces mutable slices. This is the mutable version of the existing chunks() method on the ImmutableVector trait.

EDIT: This uses only safe code now.

PREVIOUSLY:
I tried to get this working with safe code only, but I couldn't figure out how to make that work. Before #8624, the exact same code worked without the need for the transmute() call. With that fix and without the transmute() call, the compiler complains about the call to mut_slice(). I think the issue is that the mutable slice that is returned will live longer than the self parameter since the self parameter doesn't have an explicit lifetime. However, that is the way that the Iterator trait defines the next() method. I'm sure there is a good reason for that, although I don't quite understand why. Anyway, I think the interface is safe, since the MutChunkIter will only hand out non-overlapping pointers and there is no way to get it to hand out the same pointer twice.
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 21, 2022
New lint `is_digit_ascii_radix`

Closes rust-lang#6399

changelog: Added [`is_digit_ascii_radix`]: recommend `is_ascii_digit()` or `is_ascii_hexdigit()` in place of `is_digit(10)` and `is_digit(16)`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

4 participants