Skip to content

Commit fdcd5f3

Browse files
committed
For Swift 5.10, mark Storage defaultInstance nonisolated(unsafe)
- Update AnyMessageStorage.swift - Update the generator.
1 parent fe38d0a commit fdcd5f3

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,15 @@ internal class AnyMessageStorage {
146146
}
147147
var state: InternalState = .binary(Data())
148148

149-
static let defaultInstance = AnyMessageStorage()
149+
#if swift(>=5.10)
150+
// This property is used as the initial default value for new instances of the type.
151+
// The type itself is protecting the reference to its storage via CoW semantics.
152+
// This will force a copy to be made of this reference when the first mutation occurs;
153+
// hence, it is safe to mark this as `nonisolated(unsafe)`.
154+
static nonisolated(unsafe) let defaultInstance = AnyMessageStorage()
155+
#else
156+
static let defaultInstance = AnyMessageStorage()
157+
#endif
150158

151159
private init() {}
152160

Sources/protoc-gen-swift/MessageStorageClassGenerator.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ class MessageStorageClassGenerator {
5151
// readonly usage never causes the allocation.
5252
p.print(
5353
"\n",
54-
"static let defaultInstance = _StorageClass()\n",
54+
"#if swift(>=5.10)\n",
55+
" // This property is used as the initial default value for new instances of the type.\n",
56+
" // The type itself is protecting the reference to its storage via CoW semantics.\n",
57+
" // This will force a copy to be made of this reference when the first mutation occurs;\n",
58+
" // hence, it is safe to mark this as `nonisolated(unsafe)`.\n",
59+
" static nonisolated(unsafe) let defaultInstance = _StorageClass()\n",
60+
"#else\n",
61+
" static let defaultInstance = _StorageClass()\n",
62+
"#endif\n",
5563
"\n",
5664
"private init() {}\n",
5765
"\n")

0 commit comments

Comments
 (0)