Skip to content

Commit

Permalink
Merge pull request #40 from p-x9/feature/fix-key-on-release-build
Browse files Browse the repository at this point in the history
Fixed associated object key issues in release builds
  • Loading branch information
p-x9 committed Apr 3, 2024
2 parents 18f87a7 + d41fe61 commit fd5e39a
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 40 deletions.
6 changes: 4 additions & 2 deletions Sources/AssociatedObjectPlugin/AssociatedObjectMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ extension AssociatedObjectMacro: PeerMacro {
}

let keyAccessor = """
_associated_object_key()
let f: @convention(c) () -> Void = {}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
"""

let keyDecl = VariableDeclSyntax(
Expand Down Expand Up @@ -95,7 +96,8 @@ extension AssociatedObjectMacro: PeerMacro {
// nested peer macro will not expand
// https://github.com/apple/swift/issues/69073
let keyAccessor = """
_associated_object_key()
let f: @convention(c) () -> Void = {}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
"""
let flagKeyDecl = VariableDeclSyntax(
attributes: [
Expand Down
106 changes: 86 additions & 20 deletions Tests/AssociatedObjectTests/AssociatedObjectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -98,7 +100,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_intKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -141,7 +145,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_floatKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -184,7 +190,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_doubleKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -227,7 +235,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -260,7 +270,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -293,7 +305,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -326,7 +340,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -370,13 +386,17 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
@_AssociatedObject(.retain(.nonatomic)) var __associated_stringIsSet: Bool = false
@inline(never) static var __associated___associated_stringIsSetKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -419,7 +439,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_boolKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -462,7 +484,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_intArrayKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -495,7 +519,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_boolKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -538,7 +564,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_dicKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -597,7 +625,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -654,7 +684,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -725,7 +757,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -784,7 +818,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -841,7 +877,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -884,7 +922,9 @@ final class AssociatedObjectTests: XCTestCase {
}
@inline(never) static var __associated_stringKey: UnsafeRawPointer {
_associated_object_key()
let f: @convention(c) () -> Void = {
}
return unsafeBitCast(f, to: UnsafeRawPointer.self)
}
"""
}
Expand Down Expand Up @@ -1049,4 +1089,30 @@ extension AssociatedObjectTests {
XCTAssertEqual(item.definedInProtocol, "modified")
}
}

extension AssociatedObjectTests {
func testKeysUnique() {
let keys = [
ClassType.__associated_intKey,
ClassType.__associated_doubleKey,
ClassType.__associated_stringKey,
ClassType.__associated_boolKey,
ClassType.__associated_optionalIntKey,
ClassType.__associated_optionalDoubleKey,
ClassType.__associated_optionalStringKey,
ClassType.__associated_optionalBoolKey,
ClassType.__associated_implicitlyUnwrappedStringKey,
ClassType.__associated_intArrayKey,
ClassType.__associated_doubleArrayKey,
ClassType.__associated_stringArrayKey,
ClassType.__associated_boolArrayKey,
ClassType.__associated_optionalIntArrayKey,
ClassType.__associated_optionalDoubleArrayKey,
ClassType.__associated_optionalStringArrayKey,
ClassType.__associated_optionalBoolArrayKey,
ClassType.__associated_classTypeKey,
]
XCTAssertEqual(Set(keys).count, keys.count)
}
}
#endif

0 comments on commit fd5e39a

Please sign in to comment.