The optimizer assumes that a non-atomic non-volatile value written to memory stays the same, until some code that could modify it is executed. This allows a lot of obvious optimizations, like hoisting needlessly repeated computation out of loops, removal of redundant loads, and optimizations of common subexpressions and arithmetic.
If a value in memory could suddenly revert to something else, then
if obj.x == 1 {
print(obj.x)
}
could print "2", and such paradoxes can lead to unsafety:
if obj.x < array.len {
array[obj.x]
}
Defining that values in memory can't be trusted would mean giving up on a lot of optimizations, and require implementations to emit a lot of mostly-useless copying of values to protect them from being unexpectedly modified.
If a value in memory could suddenly revert to something else, then
could print "2", and such paradoxes can lead to unsafety: Defining that values in memory can't be trusted would mean giving up on a lot of optimizations, and require implementations to emit a lot of mostly-useless copying of values to protect them from being unexpectedly modified.