Skip to content

Specify layout of Cell and UnsafeCell #79303

Closed
@Kestrer

Description

@Kestrer

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.

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

    No branches or pull requests

    Issue actions