Skip to content

Commit 350cdd3

Browse files
authored
Merge pull request #71246 from DougGregor/member-macros-subscripts-deinit
2 parents cb0fb1e + 292df02 commit 350cdd3

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,3 +2284,35 @@ public struct WrapperMacro: PeerMacro {
22842284
[]
22852285
}
22862286
}
2287+
2288+
public struct AddDeinit: MemberMacro {
2289+
public static func expansion(
2290+
of node: AttributeSyntax,
2291+
providingMembersOf decl: some DeclGroupSyntax,
2292+
in context: some MacroExpansionContext
2293+
) throws -> [DeclSyntax] {
2294+
return [
2295+
"""
2296+
deinit {
2297+
print("deinit was called")
2298+
}
2299+
"""
2300+
]
2301+
}
2302+
}
2303+
2304+
public struct AddSubscript: MemberMacro {
2305+
public static func expansion(
2306+
of node: AttributeSyntax,
2307+
providingMembersOf decl: some DeclGroupSyntax,
2308+
in context: some MacroExpansionContext
2309+
) throws -> [DeclSyntax] {
2310+
return [
2311+
"""
2312+
subscript(unchecked index: Int) -> String {
2313+
return "\\(index)"
2314+
}
2315+
"""
2316+
]
2317+
}
2318+
}

test/Macros/macro_expand_synthesized_members.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,32 @@ struct S2 {
112112
}
113113
}
114114
#endif
115+
116+
@attached(
117+
member,
118+
names: named(deinit)
119+
)
120+
macro addDeinit() = #externalMacro(module: "MacroDefinition", type: "AddDeinit")
121+
122+
@attached(
123+
member,
124+
names: named(subscript(unchecked:))
125+
)
126+
macro addSubscript() = #externalMacro(module: "MacroDefinition", type: "AddSubscript")
127+
128+
@addDeinit
129+
@addSubscript
130+
class C2 {
131+
init() {
132+
print("Created a C2")
133+
}
134+
}
135+
136+
func testC2() {
137+
// CHECK: Created a C2
138+
let c2 = C2()
139+
// CHECK: 17
140+
print(c2[unchecked: 17])
141+
// CHECK: deinit was called
142+
}
143+
testC2()

0 commit comments

Comments
 (0)