Skip to content

Handle borrowing of unique pointers correctly #2204

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

Closed
nikomatsakis opened this issue Apr 14, 2012 · 5 comments
Closed

Handle borrowing of unique pointers correctly #2204

nikomatsakis opened this issue Apr 14, 2012 · 5 comments
Assignees
Labels
A-lifetimes Area: Lifetimes / regions

Comments

@nikomatsakis
Copy link
Contributor

Borrowing a unique pointer temporarily invalidates its type (the pointer is no longer unique). We need to ensure that the original unique pointer is inaccessible while the borrow alias exists (except, possibly to create other borrows). This will be done in a separate pass (I've actually implemented most of the code as part of #2046) which will check when a ~T is borrowed that either:

  1. the borrowee is an rvalue, in which case the only accessible copy is the borrowed ptr and everything is fine.
  2. the borrowee is an lvalue that is rooted in a local variable. This local variable is then made inaccessible for the scope of the borrow.
@ghost ghost assigned nikomatsakis Apr 14, 2012
@graydon
Copy link
Contributor

graydon commented Apr 20, 2012

Is this correct? I thought we just need to make the unique owner outlive the borrowed region pointer, not prevent access through the owning pointer.

@nikomatsakis
Copy link
Contributor Author

That may be true that preventing any kind of move is sufficient. It makes me a bit... worried to have a type that is untrue (a value with ~T might not, in fact, be the only pointer), but perhaps it does not lead to any problems so long as we prevent the actions that ~T would allow which are now unsafe (namely, moves).

@graydon
Copy link
Contributor

graydon commented Apr 20, 2012

Yeah, this is the origin of the concept of "immutable rooting" in the current safe reference checker.

Unique pointer is unique in the sense of the sole owner; there's no way to ensure that a temporary reference to its content isn't duplicated temporarily in some sub function, anyways.

@nikomatsakis
Copy link
Contributor Author

Naturally there is no way to prevent the borrowed pointer from being duplicated, but that doesn't bother me, as the borrowed pointer has type &T (it does not promise to be unique, unlike the ~T). Still, I think you are correct and we can use a more liberal rule than I had originally envisioned.

@nikomatsakis
Copy link
Contributor Author

this is implemented (the borrow check) though not fully activated. closing nonetheless.

celinval pushed a commit to celinval/rust-dev that referenced this issue Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions
Projects
None yet
Development

No branches or pull requests

2 participants