UnsafeCell and Exclusivity
⚓ Rust 📅 2026-01-14 👤 surdeus 👁️ 2Regarding UnsafeCell, I have a question about how it interacts with a mutable borrow.
I know that we can mutate an object through immutable borrows by using UnsafeCell without violating the aliasing rules. For example, when we want to mutate some object that is immutably borrowed through raw pointers, we need to wrap it with UnsafeCell in order to avoid UB as Rc does. Then I wondered if it is legal to mutate an UnsafeCell object through a raw pointer that is mutably borrowed. Could it be illegal because UnsafeCell is an escape hatch to invalidate immutability?
I once wrote a demo code that implemented the above mutation and ran Miri to check if is considered violation of the aliasing rule, but Miri didn't complain, so I posted this question.
I appreciate your support.
3 posts - 2 participants
🏷️ Rust_feed