Skip to content

Tracking Issue for unsafe_cell_get_mut #76943

Closed
@danielhenrymantilla

Description

@danielhenrymantilla
Contributor

This is a tracking issue for adding the fn get_mut (self: &mut UnsafeCell<T>) -> &mut T method.
The feature gate for the issue is #![feature(unsafe_cell_get_mut)].

History

Unresolved Questions

None

Activity

added
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
on Sep 19, 2020
danielhenrymantilla

danielhenrymantilla commented on Sep 19, 2020

@danielhenrymantilla
ContributorAuthor

This is small enough not to need an RFC, it seems, so it can go directly to the FCP: cc @RalfJung & the rest of T-Lang T-libs

RalfJung

RalfJung commented on Sep 20, 2020

@RalfJung
Member

FCP will happen when we move towards stabilization. We usually wait a few release cycles before doing that. Also this is T-libs territory, I don't think any language rules are being changed here (just clarified).


@rust-lang/libs this is about adding UnsafeCell::get_mut, a method to safely go from &mut UnsafeCell<T> to &mut T. This is a sound operation on its own, and all standard library interior mutable types have an equivalent function in their API surface.

However, there is a theoretical scenario where this can cause unsoundness in existing libraries: if a user-defined library, for some reason, exposes an &mut UnsafeCell<T> to a client and relied on the fact that there was no way for safe clients to do anything with that reference. I do not know why a library would do this, but I seem to recall this concern being brought up before when changes to UnsafeCell are proposed. In this sense, the change is not backwards compatible, but only if a library relied on UnsafeCell guarantees that I do not think we ever made.

added
T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.
on Sep 20, 2020
danielhenrymantilla

danielhenrymantilla commented on Sep 20, 2020

@danielhenrymantilla
ContributorAuthor

if a user-defined library, for some reason, exposes an &mut UnsafeCell to a client and relied on the fact that there was no way for safe clients to do anything with that reference

Good point, although the only way they could rely on "not being to do anything with that reference" is if they (ab)used the fact that there was no other instance of type T around (singleton pattern, or through a highly generic API relying on "type-level generativity").

Otherwise it is possible to:

fn mutate<T> (p: &mut UnsafeCell<T>, value: T)
{
    *p = value.into();
}

and more generally, we can currently write:

fn with_mut<T, R> (
    p: &'_ mut UnsafeCell<T>,
    f: impl FnOnce(&'_ mut T) -> R,
) -> R
where
    T : Default,
{
    let mut prev = mem::take(p).into_inner();
    // defer putting the value back
    let mut prev = ::scopeguard::guard(prev, |it| *p = it.into());
    f(&mut *prev)
}
  • which performs a read-write of the payload when entering, and a write (+ drop in place) when leaving
RalfJung

RalfJung commented on Sep 20, 2020

@RalfJung
Member

Ah right, you can just overwrite what is in an &mut UnsafeCell<T>. Yeah that makes it even less likely that this would be a problem.

added 2 commits that reference this issue on Sep 21, 2020

Rollup merge of rust-lang#76936 - danielhenrymantilla:unsafecell_get_…

4289998

Rollup merge of rust-lang#76936 - danielhenrymantilla:unsafecell_get_…

4e37c22
added 2 commits that reference this issue on Sep 21, 2020

Rollup merge of rust-lang#76936 - danielhenrymantilla:unsafecell_get_…

1f8ee01

Rollup merge of rust-lang#76936 - danielhenrymantilla:unsafecell_get_…

b670b86
added
Libs-TrackedLibs issues that are tracked on the team's project board.
on Nov 6, 2020
m-ou-se

m-ou-se commented on Nov 28, 2020

@m-ou-se
Member

Stabilization PR: #79485

added a commit that references this issue on Dec 18, 2020

Auto merge of rust-lang#79485 - EllenNyan:stabilize_unsafe_cell_get_m…

jonas-schievink

jonas-schievink commented on Dec 18, 2020

@jonas-schievink
Contributor

#79485 was merged 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCLibs-TrackedLibs issues that are tracked on the team's project board.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @RalfJung@m-ou-se@jonas-schievink@KodrAus@danielhenrymantilla

        Issue actions

          Tracking Issue for `unsafe_cell_get_mut` · Issue #76943 · rust-lang/rust