Apple has introduced full exclusivity enforcement enabled at run-time in version 5 of its Swift programming language. The feature improves memory safety by preventing a variable from being accessed by a different name during a modification of its value, explained Andrew Trick, software engineer at Apple, in a developer blog.

The feature was previously available in debug builds in Swift 4, but applications that weren’t fully tested in debug might be affected by the update, Trick said.

Trick explained that in situations where a programmer’s intention might be ambiguous, the compiler can’t guarantee some application behavior. For example, a variable that is read and called within the same scope that it’s modified would cause an exclusivity violation at run-time with the new feature, Trick explained.

“Compile-time (static) diagnostics catch many common exclusivity violations, but run-time (dynamic) diagnostics are also required to catch violations involving escaping closures, properties of class types, static properties, and global variables,” Trick said. While the types of exclusivity violations that the compiler could catch were increased over successive versions of Swift 4, Swift 4.2 made them more visible with the exclusive access warning error.

“Swift 5 fixes the remaining holes in the language model and fully enforces that model,” Trick said.

For those apps not fully tested in debug, Trick says that memory overhead for access checks might cause a small performance hit, but if it’s measurable, developers should a bug report. “As a general guideline, avoid performing class property access within the most performance critical loops, particularly on different objects in each loop iteration,” Trick said. “If that isn’t possible, making the class properties private or internal can help the compiler prove that no other code accesses the same property inside the loop.”

More examples and information can be found here.