Skip to content

Specify layout of Cell and UnsafeCell #79303

Closed
@Kestrer

Description

@Kestrer
Contributor

Currently, Cell<T> and UnsafeCell<T> are both #[repr(transparent)]. However, it is unclear whether this is stable or just an implementation detail. This line in the implementation of UnsafeCell notes that "there is no guarantee for user code that this [casting a *const UnsafeCell<T> to *mut T] will work in future versions of the compiler"; but it is unclear whether this applies to the casting of *const UnsafeCell<T> to *const T or the casting of *const T to *mut T. as_slice_of_cells also relies on the layout of UnsafeCell, but makes no mention of it being exclusive to std.

Even if they do stably have a transparent layout, there are also some questions to be resolved around whether creating UnsafeCells from references is UB; conversions like *const T to *const UnsafeCell<T>, &T to &UnsafeCell<T>, &mut T to &UnsafeCell<T>, &[UnsafeCell<T>] to &UnsafeCell<[T]> should all probably be specified.

Activity

steffahn

steffahn commented on Nov 22, 2020

@steffahn
Member

@rustbot modify labels to T-doc, T-lang, C-enhancement.

Also, here’s the link to the relevant URLO discussion.

added
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
T-langRelevant to the language team
on Nov 22, 2020
sfackler

sfackler commented on Nov 22, 2020

@sfackler
Member

From a libs perspective, a #[repr(transparent)] annotation on a public type is a stable guarantee. I think that warning has more to due with the unsafe code guidelines decisions around what exactly is UB or not with pointer casts in relation to the "specialness" of UnsafeCell than just if the type is transparent.

the8472

the8472 commented on Nov 22, 2020

@the8472
Member

From a libs perspective, a #[repr(transparent)] annotation on a public type is a stable guarantee.

What does that tell a user though? Its field is not part of the public API thus one can't know in relation to what it is transparent. In other words there could in theory be yet another wrapper inside and the repr(transparent) only gives us the wrapper's ABI and not T's.

added a commit that references this issue on Mar 10, 2023

Rollup merge of rust-lang#106921 - madsmtm:cell-memory-layout, r=dtolnay

b90277e
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

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @the8472@sfackler@steffahn@Kestrer@rustbot

      Issue actions

        Specify layout of `Cell` and `UnsafeCell` · Issue #79303 · rust-lang/rust