Does UnsafeCell make the coexistence of &mut T legal?

โš“ Rust    ๐Ÿ“… 2026-02-21    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

Iโ€™m trying to verify my understanding of Rustโ€™s aliasing rules regarding UnsafeCell<T>.

It seems that if you have multiple &UnsafeCell<T> references and use one to create a temporary &mut T for writing, you inevitably have both types of pointers pointing to the same memory at the same time.

My reasoning is that since UnsafeCell<T> explicitly opts out of the standard &T immutability/noalias guarantees, this "coexistence" might actually be legal within the memory model. Is it correct to say that as long as UnsafeCell is involved, the strict exclusivity of &mut T is relaxed to allow these overlaps?

Iโ€™d appreciate any insights on whether this interpretation is correct.

10 posts - 5 participants

Read full topic

๐Ÿท๏ธ Rust_feed