Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented May 4, 2025

This tackles #137750 by including an UnsafeCell in UnsafePinned, thus imbuing it with all the usual properties of interior mutability (no noalias nor dereferenceable on shared refs, special treatment by Miri's aliasing model). The soundness issue is not fixed yet because coroutine lowering does not use UnsafePinned.

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 inside UnsafePinned. This seems loosely consistent with the fact that we allow going from Pin<&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 the as_ref example shows that we in fact would need to add this UnsafeCell even if we didn't have a safe conversion to &T, since for the compiler and Miri, &T and Pin<&T> are basically the same type.

To make this possible, I had to remove the Copy and Clone impls for UnsafePinned.

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.

@rustbot
Copy link
Collaborator

rustbot commented May 4, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 4, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@Sky9x Sky9x left a 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

@RalfJung RalfJung added the I-lang-nominated Nominated for discussion during a lang team meeting. label May 4, 2025
@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from b08491e to 66ca017 Compare May 4, 2025 16:17
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from 66ca017 to bc8609c Compare May 4, 2025 21:16
@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from bc8609c to d87cf5a Compare May 5, 2025 06:45
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from d87cf5a to 405c954 Compare May 5, 2025 11:30
use std::pin::UnsafePinned;

fn mutate(x: &UnsafePinned<i32>) {
let ptr = x as *const _ as *mut i32;
Copy link
Contributor

@Sky9x Sky9x May 5, 2025

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

Copy link
Member Author

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants