Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed associated object key issues in release builds #40

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +1094 to +1116
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if generated keys is uniqued.

}
#endif
Loading
Loading