Description
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 UnsafeCell
s 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 commentedon Nov 22, 2020
@rustbot modify labels to T-doc, T-lang, C-enhancement.
Also, here’s the link to the relevant URLO discussion.
sfackler commentedon Nov 22, 2020
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" ofUnsafeCell
than just if the type is transparent.the8472 commentedon Nov 22, 2020
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.Cell
#106921Rollup merge of rust-lang#106921 - madsmtm:cell-memory-layout, r=dtolnay
Rollup merge of #106921 - madsmtm:cell-memory-layout, r=dtolnay
Rollup merge of #106921 - madsmtm:cell-memory-layout, r=dtolnay