-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Remove ptr::Unique
#71530
Remove ptr::Unique
#71530
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
@rust-lang/libs What do you think? |
rust-lang#71507 (comment) discusses whether `Unique` not actually being unique is UB. `Unique` has very limited use over `NonNull`, so it doesn’t seem worth spending a lot of effort defining an abstraction and coming up with rules about what uses of it are or aren’t sound.
I've found that |
// Box is kind-of a library type, but recognized as a "unique pointer" by | ||
// Stacked Borrows. This function here corresponds to "reborrowing to | ||
// a raw pointer", but there is no actual reborrow here -- so | ||
// without some care, the pointer we are returning here still carries | ||
// the tag of `b`, with `Unique` permission. | ||
// We round-trip through a mutable reference to avoid that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RalfJung Could you say some more about how and why Box
is special in Stacked Borrows? Could it be "just" another library type? Are Vec
or Rc
for example also special?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The how: Box
is basically like &mut
(except that unlike &mut
we never add "protectors" for Box
, but that's a detail).
The why: As far as I know, rustc emits noalias
annotations for Box
. This demonstrates that it is not "just another library type". Those annotations are incorrect unless our aliasing model (i.e., Stacked Borrows) actually treats Box
special. Additionally, back when I started with Stacked Borrows, we also added dereferencable
annotations. Right now we do not do that any more, but the plan is to start doing that again once LLVM weakened dereferencable
semantics to mean "dereferencable on entry to the function" (as opposed to "dereferencable throughout the entire execution of the function", which is what it means now). That would be another way in which Box
is not just a library type.
Long-term, the plan is to not make Box
special but to make Unique
special instead. At least, @Gankra told me that that was the plan. ;) But that is blocked on figuring out better how exactly we want it to be special.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are Vec or Rc for example also special?
No, right now only Box
is special.
Eventually, when Unique
is special, that would be inherited by Vec
. Vec
is, in fact, the main motivation for making Unique
special. See e.g. #71354.
Cc @rust-lang/lang -- not sure what the lang team's long-term plans around |
pub fn into_unique(b: Box<T>) -> Unique<T> { | ||
let b = mem::ManuallyDrop::new(b); | ||
let mut unique = b.0; | ||
let mut b = mem::ManuallyDrop::new(b); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this PR conflicts badly with #71168.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll deal with it after one of them lands.
☔ The latest upstream changes (presumably #71556) made this pull request unmergeable. Please resolve the merge conflicts. |
i agree the motivation for Unique is weak, but i also agree with Ralf that removing accompishes ~nothing as long as Box has these special semantics that Unique is supposed to just be a generalization of. |
Instead of removing it, what about removing the
Could you give an example or two? |
Hmm never mind, I seem to be misremembering. |
What are the aliasing rules for |
The intention is that it matches the semantics of C EDIT: Actually disregard that, I need to review the rules for restrict in C. |
Actually I'll just point to the docs for C's restrict: https://en.cppreference.com/w/c/language/restrict |
I don't think the details of C's But in any case, I expect that whenever we do settle on some aliasing rules for That kind of experiment would actually be easier if we left |
In terms of Stacked Borrows, roughly speaking I'd say: like |
Alright, thanks for the feedback everyone. Given no enthusiasm for removing and some against, I’ll close this. |
#71507 (comment) discusses whether
Unique
not actually being unique is UB.Unique
has very limited use overNonNull
, so it doesn’t seem worth spending a lot of effort defining an abstraction and coming up with rules about what uses of it are or aren’t sound.