@@ -49,13 +49,22 @@ extension AssociatedObjectMacro: PeerMacro {
49
49
return [ ]
50
50
}
51
51
52
+ let keyAccessor = """
53
+ _associated_object_key()
54
+ """
55
+
52
56
let keyDecl = VariableDeclSyntax (
57
+ attributes: [
58
+ . attribute( " @inline(never) " )
59
+ ] ,
53
60
bindingSpecifier: . identifier( " static var " ) ,
54
61
bindings: PatternBindingListSyntax {
55
62
PatternBindingSyntax (
56
63
pattern: IdentifierPatternSyntax ( identifier: . identifier( " __associated_ \( identifier. trimmed) Key " ) ) ,
57
- typeAnnotation: . init( type: IdentifierTypeSyntax ( name: . identifier( " UInt8 " ) ) ) ,
58
- initializer: InitializerClauseSyntax ( value: ExprSyntax ( stringLiteral: " 0 " ) )
64
+ typeAnnotation: . init( type: IdentifierTypeSyntax ( name: . identifier( " UnsafeRawPointer " ) ) ) ,
65
+ accessorBlock: . init(
66
+ accessors: . getter( " \( raw: keyAccessor) " )
67
+ )
59
68
)
60
69
}
61
70
)
@@ -65,6 +74,7 @@ extension AssociatedObjectMacro: PeerMacro {
65
74
]
66
75
67
76
if type. isOptional && defaultValue != nil {
77
+ let flagName = " __associated_ \( identifier. trimmed) IsSet "
68
78
let flagDecl = VariableDeclSyntax (
69
79
attributes: [
70
80
. attribute( " @_AssociatedObject(.OBJC_ASSOCIATION_ASSIGN) " )
@@ -73,22 +83,33 @@ extension AssociatedObjectMacro: PeerMacro {
73
83
bindings: PatternBindingListSyntax {
74
84
PatternBindingSyntax (
75
85
pattern: IdentifierPatternSyntax (
76
- identifier: . identifier( " __associated_ \( identifier . trimmed ) IsSet " )
86
+ identifier: . identifier( flagName )
77
87
) ,
78
88
typeAnnotation: . init( type: IdentifierTypeSyntax ( name: . identifier( " Bool " ) ) ) ,
79
89
initializer: InitializerClauseSyntax ( value: BooleanLiteralExprSyntax ( false ) )
80
90
)
81
91
}
82
92
)
93
+
94
+ // nested peer macro will not expand
95
+ // https://github.com/apple/swift/issues/69073
96
+ let keyAccessor = """
97
+ _associated_object_key()
98
+ """
83
99
let flagKeyDecl = VariableDeclSyntax (
100
+ attributes: [
101
+ . attribute( " @inline(never) " )
102
+ ] ,
84
103
bindingSpecifier: . identifier( " static var " ) ,
85
104
bindings: PatternBindingListSyntax {
86
105
PatternBindingSyntax (
87
106
pattern: IdentifierPatternSyntax (
88
107
identifier: . identifier( " __associated___associated_ \( identifier. trimmed) IsSetKey " )
89
108
) ,
90
- typeAnnotation: . init( type: IdentifierTypeSyntax ( name: . identifier( " UInt8 " ) ) ) ,
91
- initializer: InitializerClauseSyntax ( value: ExprSyntax ( stringLiteral: " 0 " ) )
109
+ typeAnnotation: . init( type: IdentifierTypeSyntax ( name: . identifier( " UnsafeRawPointer " ) ) ) ,
110
+ accessorBlock: . init(
111
+ accessors: . getter( " \( raw: keyAccessor) " )
112
+ )
92
113
)
93
114
}
94
115
)
@@ -160,7 +181,7 @@ extension AssociatedObjectMacro: AccessorMacro {
160
181
return [ ]
161
182
}
162
183
163
- var associatedKey : ExprSyntax = " & Self.__associated_\( identifier. trimmed) Key "
184
+ var associatedKey : ExprSyntax = " Self.__associated_ \( identifier. trimmed) Key "
164
185
if case let . argumentList( arguments) = node. arguments,
165
186
let element = arguments. first ( where: { $0. label? . text == " key " } ) ,
166
187
let customKey = element. expression. as ( ExprSyntax . self) {
@@ -236,7 +257,7 @@ extension AssociatedObjectMacro {
236
257
"""
237
258
if let value = objc_getAssociatedObject(
238
259
self,
239
- &Self.__associated_ \( identifier . trimmed ) Key
260
+ \( associatedKey )
240
261
) as? \( varTypeWithoutOptional. trimmed) {
241
262
return value
242
263
} else {
0 commit comments