-
Notifications
You must be signed in to change notification settings - Fork 13.3k
UnsafePinned: also include the effects of UnsafeCell #140638
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
base: master
Are you sure you want to change the base?
Conversation
The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
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.
getting rid of those Copy/Clone impls is nice
b08491e
to
66ca017
Compare
This comment has been minimized.
This comment has been minimized.
66ca017
to
bc8609c
Compare
bc8609c
to
d87cf5a
Compare
This comment has been minimized.
This comment has been minimized.
d87cf5a
to
405c954
Compare
use std::pin::UnsafePinned; | ||
|
||
fn mutate(x: &UnsafePinned<i32>) { | ||
let ptr = x as *const _ as *mut i32; |
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.
shouldn't this go through UnsafeCell/UnsafePinned::get
? https://doc.rust-lang.org/nightly/std/cell/struct.UnsafeCell.html#memory-layout
also maybe UnsafePinned::get
should return a *mut T
now...
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.
We're testing the aliasing model here so we want to see everything that happens in the code. This is not a doctest. :)
This tackles #137750 by including an
UnsafeCell
inUnsafePinned
, thus imbuing it with all the usual properties of interior mutability (nonoalias
nordereferenceable
on shared refs, special treatment by Miri's aliasing model). The soundness issue is not fixed yet because coroutine lowering does not useUnsafePinned
.The RFC said that
UnsafePinned
would not permit mutability on shared references, but since then, #137750 has demonstrated that this is not tenable. In the face of those examples, I propose that we do the "obvious" thing and permit shared mutable state insideUnsafePinned
. This seems loosely consistent with the fact that we allow going fromPin<&mut T>
to&T
(where the former can be aliased with other pointers that perform mutation, and hence the same goes for the latter) -- but theas_ref
example shows that we in fact would need to add thisUnsafeCell
even if we didn't have a safe conversion to&T
, since for the compiler and Miri,&T
andPin<&T>
are basically the same type.To make this possible, I had to remove the
Copy
andClone
impls forUnsafePinned
.Tracking issue: #125735
Cc @rust-lang/lang @rust-lang/opsem @Sky9x
I don't think this needs FCP since the type is still unstable -- we'll finally decide whether we like this approach when
UnsafePinned
is moved towards stabilization (IOW, this PR is reversible). However, I'd still like to make sure that the lang team is okay with the direction I am proposing here.