Description
See https://users.rust-lang.org/t/can-you-break-the-lift/58858 for the original discussion (it actually starts from the 12th post)
The original question takes a &UnsafeCell<T>
and, supposing that nobody is borrowing the inner value, gets a mutable reference (&mut T
) to the inner value, then trasmute it to a &mut UnsafeCell<T>
, creating a &mut UnsafeCell<T>
that alias with another &UnsafeCell<T>
. Is this sound? Does this also mean you can transmute a &UnsafeCell<T>
to a &mut UnsafeCell<T>
(assuming no other reference to the inner value exist)?
Related: jasoncarr0 also showed you can safely do something like this with GhostCell
.
Also related: rust-lang/rust#63787 since that's also caused by a &UnsafeCell<T>
aliasing with a reference to itself, even though in that case it's a reference to the inner value. While that may be solved by storing a raw pointer inside Ref
, it's not an actual possibility here.
I feel like that either this is unsound or the rust aliasing model is fundamentally incompatible with LLVM's noalias.