When I read The Swift Programming Language: Memory Safety, I was confused by the section Conflicting Access to Properties:
The code below shows that the same error appears for overlapping write accesses to the properties of a structure that’s stored in a global variable.
var holly = Player(name: "Holly", health: 10, energy: 10) balance(&holly.health, &holly.energy) // ErrorIn practice, most access to the properties of a structure can overlap safely. For example, if the variable
hollyin the example above is changed to a local variable instead of a global variable, the compiler can prove that overlapping access to stored properties of the structure is safe:func someFunction() { var oscar = Player(name: "Oscar", health: 10, energy: 10) balance(&oscar.health, &oscar.energy) // OK }In the example above, Oscar’s health and energy are passed as the two in-out parameters to
balance(_:_:). The compiler can prove that memory safety is preserved because the two stored properties don’t interact in any way.
How the compiler can prove that memory safety?