Skip to content

Commit 6d74182

Browse files
committed
[Macros] Replace all uses of the ConformanceMacro protocol with ExtensionMacro.
1 parent 6a4c789 commit 6d74182

File tree

4 files changed

+53
-54
lines changed

4 files changed

+53
-54
lines changed

lib/Macros/Sources/SwiftMacros/OptionSetMacro.swift

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,27 +123,25 @@ public struct OptionSetMacro {
123123
}
124124
}
125125

126-
extension OptionSetMacro: ConformanceMacro {
127-
public static func expansion<
128-
Decl: DeclGroupSyntax,
129-
Context: MacroExpansionContext
130-
>(
126+
extension OptionSetMacro: ExtensionMacro {
127+
public static func expansion(
131128
of attribute: AttributeSyntax,
132-
providingConformancesOf decl: Decl,
133-
in context: Context
134-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
135-
// Decode the expansion arguments.
136-
guard let (structDecl, _, _) = decodeExpansion(of: attribute, attachedTo: decl, in: context) else {
137-
return []
138-
}
139-
129+
attachedTo decl: some DeclGroupSyntax,
130+
providingExtensionsOf type: some TypeSyntaxProtocol,
131+
conformingTo protocols: [TypeSyntax],
132+
in context: some MacroExpansionContext
133+
) throws -> [ExtensionDeclSyntax] {
140134
// If there is an explicit conformance to OptionSet already, don't add one.
141-
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypeCollection,
142-
inheritedTypes.contains(where: { inherited in inherited.typeName.trimmedDescription == "OptionSet" }) {
135+
if protocols.isEmpty {
143136
return []
144137
}
145138

146-
return [("OptionSet", nil)]
139+
let ext: DeclSyntax =
140+
"""
141+
extension \(type.trimmed): OptionSet {}
142+
"""
143+
144+
return [ext.cast(ExtensionDeclSyntax.self)]
147145
}
148146
}
149147

test/Index/index_macros.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,16 @@ public struct SomeAccessorMacro: AccessorMacro {
207207
}
208208
}
209209

210-
public struct SomeConformanceMacro: ConformanceMacro {
210+
public struct SomeConformanceMacro: ExtensionMacro {
211211
public static func expansion(
212212
of node: AttributeSyntax,
213-
providingConformancesOf decl: some DeclGroupSyntax,
213+
attachedTo: some DeclGroupSyntax,
214+
providingExtensionsOf type: some TypeSyntaxProtocol,
215+
conformingTo protocols: [TypeSyntax],
214216
in context: some MacroExpansionContext
215-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
216-
let protocolName: TypeSyntax = "TestProto"
217-
return [(protocolName, nil)]
217+
) throws -> [ExtensionDeclSyntax] {
218+
let ext: DeclSyntax = "extension \(type.trimmed): TestProto {}"
219+
return [ext.cast(ExtensionDeclSyntax.self)]
218220
}
219221
}
220222

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,14 +1324,16 @@ public struct EmptyPeerMacro: PeerMacro {
13241324
}
13251325
}
13261326

1327-
public struct EquatableMacro: ConformanceMacro {
1327+
public struct EquatableMacro: ExtensionMacro {
13281328
public static func expansion(
13291329
of node: AttributeSyntax,
1330-
providingConformancesOf decl: some DeclGroupSyntax,
1330+
attachedTo: some DeclGroupSyntax,
1331+
providingExtensionsOf type: some TypeSyntaxProtocol,
1332+
conformingTo protocols: [TypeSyntax],
13311333
in context: some MacroExpansionContext
1332-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1333-
let protocolName: TypeSyntax = "Equatable"
1334-
return [(protocolName, nil)]
1334+
) throws -> [ExtensionDeclSyntax] {
1335+
let ext: DeclSyntax = "extension \(type.trimmed): Equatable {}"
1336+
return [ext.cast(ExtensionDeclSyntax.self)]
13351337
}
13361338
}
13371339

@@ -1359,34 +1361,37 @@ public struct ConformanceViaExtensionMacro: ExtensionMacro {
13591361
}
13601362
}
13611363

1362-
public struct HashableMacro: ConformanceMacro {
1364+
public struct HashableMacro: ExtensionMacro {
13631365
public static func expansion(
13641366
of node: AttributeSyntax,
1365-
providingConformancesOf decl: some DeclGroupSyntax,
1367+
attachedTo: some DeclGroupSyntax,
1368+
providingExtensionsOf type: some TypeSyntaxProtocol,
1369+
conformingTo protocols: [TypeSyntax],
13661370
in context: some MacroExpansionContext
1367-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1368-
let protocolName: TypeSyntax = "Hashable"
1369-
return [(protocolName, nil)]
1371+
) throws -> [ExtensionDeclSyntax] {
1372+
let ext: DeclSyntax = "extension \(type.trimmed): Hashable {}"
1373+
return [ext.cast(ExtensionDeclSyntax.self)]
13701374
}
13711375
}
13721376

1373-
public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
1377+
public struct DelegatedConformanceMacro: ExtensionMacro, MemberMacro {
13741378
public static func expansion(
13751379
of node: AttributeSyntax,
1376-
providingConformancesOf decl: some DeclGroupSyntax,
1380+
attachedTo: some DeclGroupSyntax,
1381+
providingExtensionsOf type: some TypeSyntaxProtocol,
1382+
conformingTo protocols: [TypeSyntax],
13771383
in context: some MacroExpansionContext
1378-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1379-
let protocolName: TypeSyntax = "P"
1384+
) throws -> [ExtensionDeclSyntax] {
13801385
let conformance: DeclSyntax =
13811386
"""
1382-
extension Placeholder where Element: P {}
1387+
extension \(type.trimmed): P where Element: P {}
13831388
"""
13841389

13851390
guard let extensionDecl = conformance.as(ExtensionDeclSyntax.self) else {
13861391
return []
13871392
}
13881393

1389-
return [(protocolName, extensionDecl.genericWhereClause)]
1394+
return [extensionDecl]
13901395
}
13911396

13921397
public static func expansion(
@@ -1803,26 +1808,21 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
18031808
}
18041809
}
18051810

1806-
public struct InitializableMacro: ConformanceMacro, MemberMacro {
1811+
public struct InitializableMacro: ExtensionMacro {
18071812
public static func expansion(
18081813
of node: AttributeSyntax,
1809-
providingConformancesOf decl: some DeclGroupSyntax,
1810-
in context: some MacroExpansionContext
1811-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1812-
return [("Initializable", nil)]
1813-
}
1814-
1815-
public static func expansion(
1816-
of node: AttributeSyntax,
1817-
providingMembersOf decl: some DeclGroupSyntax,
1814+
attachedTo: some DeclGroupSyntax,
1815+
providingExtensionsOf type: some TypeSyntaxProtocol,
1816+
conformingTo protocols: [TypeSyntax],
18181817
in context: some MacroExpansionContext
1819-
) throws -> [DeclSyntax] {
1820-
let requirement: DeclSyntax =
1818+
) throws -> [ExtensionDeclSyntax] {
1819+
let ext: DeclSyntax =
18211820
"""
1822-
init(value: Int) {}
1821+
extension \(type.trimmed): Initializable {
1822+
init(value: Int) {}
1823+
}
18231824
"""
1824-
1825-
return [requirement]
1825+
return [ext.cast(ExtensionDeclSyntax.self)]
18261826
}
18271827
}
18281828

test/Macros/top_level_freestanding.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ macro Empty<T>(_ closure: () -> T) = #externalMacro(module: "MacroDefinition", t
117117
S(a: 10, b: 10)
118118
}
119119

120-
@attached(extension, conformances: Initializable)
121-
@attached(member, names: named(init))
120+
@attached(extension, conformances: Initializable, names: named(init))
122121
macro Initializable() = #externalMacro(module: "MacroDefinition", type: "InitializableMacro")
123122

124123
protocol Initializable {

0 commit comments

Comments
 (0)