Strict Memory Safety
One of the features in Swift 6.2 is the ability to turn on strict memory safety checking. It makes it easy to audit unsafe calls by requiring you to add the new
unsafe
keyword to any expression that calls something @unsafe
or uses an unsafe
parameter. This can be done in Xcode 26 by setting “Strict Memory Safety” to “Yes” or, in the case of a Swift package, turning the feature on.
In a Swift package you must first use the latest tools:
// swift-tools-version: 6.2
and in the target specify the .stringMemorySafety(condition)
.target(name: "uuid-exploration"),
swiftSettings: [.strictMemorySafety(), .treatAllWarnings(as: .error)])
You then get warnings (errors if you wish to treat them that way) for all the places that use unsafe constructs:
withUnsafeMutableBytes(of: &random) { raw in
let result = SecRandomCopyBytes(kSecRandomDefault, 10, raw.baseAddress!)
precondition(result == errSecSuccess)
}
Needs to be changed to:
unsafe withUnsafeMutableBytes(of: &random) { raw in
let result = unsafe SecRandomCopyBytes(kSecRandomDefault, 10, raw.baseAddress!)
precondition(result == errSecSuccess)
}
All of the details are in the proposal, of course, which you can read here: