If you have a reference &T , then normally in Rust the compiler performs optimizations based on the knowledge that &T points to immutable data. Mutating that data, for example through an alias or by transmuting an &T into an &mut T , is considered undefined behavior. UnsafeCell<T> opts-out of the immutability guarantee for &T : a shared reference &UnsafeCell<T> may point to data that is being mutated. This is called “interior mutability”.
It is OK indeed. What comes to my mind is: If
struct a {
f: UnsafeCell<String>
}
struct b {
af: &a
}
struct c {
bf: &b
}
struct d {
cf: &c
}
...
struct y {
xf: &x
}
struct z {
yf: &y
}
fn xxx(r: &z) {
....
}
Will the compiler knowns it should not assume r immutable and prevents some optimization because yf.xf. ...cf.bf.af.f is an UnsafeCell?
No. In the example you provided, only the innermost z.yf.xf…cf.bf.af.f field is an UnsafeCell, so that's the only thing that you are allowed to treat as such. All the other fields of all the other structs must still uphold the no-aliased-mutability rule, otherwise the code will have UB. It's not like you use one single UnsafeCell in your code and then every relevant optimization is turned off across the entire code.
Since r.yf.xf…cf.bf.af.f is an UnsafeCell<T>, it might be modified (e.g., on another thread) even while it is borrowed, so that optimization would be incorrect in the general case.
A sufficiently smart compiler might still be able to do it in specific cases, if whole-program analysis could prove that such a modification never happens in practice.
I suppose since the code doesn't use any memory barriers, the compiler could rule out access from other threads (which would be UB), in which case it can optimize out the comparisons as long as there are no calls within the loop to other functions that might modify the f field.