In Unsafe Code Guidelines Reference, it says
All interior mutation in Rust has to happen inside an
UnsafeCell, so all data structures that have interior mutability must (directly or indirectly) useUnsafeCellfor this purpose.
Also, in a discussion about UnsafeCell, it says
UnsafeCellis basically an optimization barrier to the compiler.
It is true that UnsafeCell acts as a compiler optimization barrier in Rust? If yes, which line in the standard library source code emits a barrier and how does it work?
[UPDATE]
The answer of a related question gives a very nice explanation. The TL;DR version is: UnsafeCell<T> is marked with #[lang = "unsafe_cell"] which forces it to be invariant over T.
Now I think this is not very much connected to optimization, but interacts more closely with lifetime analysis.
For the notion of variance in Rust, The Rustonomicon Book gives a detailed explanation.