Skip to content

Commit 5a6aa7b

Browse files
authored
Merge pull request #67762 from hborla/remove-conformance-macro-protocol
[Macros] Replace all uses of the `ConformanceMacro` protocol with `ExtensionMacro`.
2 parents 6214336 + d03ef5a commit 5a6aa7b

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?.inheritedTypes,
142-
inheritedTypes.contains(where: { inherited in inherited.type.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
@@ -1281,14 +1281,16 @@ public struct EmptyPeerMacro: PeerMacro {
12811281
}
12821282
}
12831283

1284-
public struct EquatableMacro: ConformanceMacro {
1284+
public struct EquatableMacro: ExtensionMacro {
12851285
public static func expansion(
12861286
of node: AttributeSyntax,
1287-
providingConformancesOf decl: some DeclGroupSyntax,
1287+
attachedTo: some DeclGroupSyntax,
1288+
providingExtensionsOf type: some TypeSyntaxProtocol,
1289+
conformingTo protocols: [TypeSyntax],
12881290
in context: some MacroExpansionContext
1289-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1290-
let protocolName: TypeSyntax = "Equatable"
1291-
return [(protocolName, nil)]
1291+
) throws -> [ExtensionDeclSyntax] {
1292+
let ext: DeclSyntax = "extension \(type.trimmed): Equatable {}"
1293+
return [ext.cast(ExtensionDeclSyntax.self)]
12921294
}
12931295
}
12941296

@@ -1316,34 +1318,37 @@ public struct ConformanceViaExtensionMacro: ExtensionMacro {
13161318
}
13171319
}
13181320

1319-
public struct HashableMacro: ConformanceMacro {
1321+
public struct HashableMacro: ExtensionMacro {
13201322
public static func expansion(
13211323
of node: AttributeSyntax,
1322-
providingConformancesOf decl: some DeclGroupSyntax,
1324+
attachedTo: some DeclGroupSyntax,
1325+
providingExtensionsOf type: some TypeSyntaxProtocol,
1326+
conformingTo protocols: [TypeSyntax],
13231327
in context: some MacroExpansionContext
1324-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1325-
let protocolName: TypeSyntax = "Hashable"
1326-
return [(protocolName, nil)]
1328+
) throws -> [ExtensionDeclSyntax] {
1329+
let ext: DeclSyntax = "extension \(type.trimmed): Hashable {}"
1330+
return [ext.cast(ExtensionDeclSyntax.self)]
13271331
}
13281332
}
13291333

1330-
public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
1334+
public struct DelegatedConformanceMacro: ExtensionMacro, MemberMacro {
13311335
public static func expansion(
13321336
of node: AttributeSyntax,
1333-
providingConformancesOf decl: some DeclGroupSyntax,
1337+
attachedTo: some DeclGroupSyntax,
1338+
providingExtensionsOf type: some TypeSyntaxProtocol,
1339+
conformingTo protocols: [TypeSyntax],
13341340
in context: some MacroExpansionContext
1335-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1336-
let protocolName: TypeSyntax = "P"
1341+
) throws -> [ExtensionDeclSyntax] {
13371342
let conformance: DeclSyntax =
13381343
"""
1339-
extension Placeholder where Element: P {}
1344+
extension \(type.trimmed): P where Element: P {}
13401345
"""
13411346

13421347
guard let extensionDecl = conformance.as(ExtensionDeclSyntax.self) else {
13431348
return []
13441349
}
13451350

1346-
return [(protocolName, extensionDecl.genericWhereClause)]
1351+
return [extensionDecl]
13471352
}
13481353

13491354
public static func expansion(
@@ -1760,26 +1765,21 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
17601765
}
17611766
}
17621767

1763-
public struct InitializableMacro: ConformanceMacro, MemberMacro {
1768+
public struct InitializableMacro: ExtensionMacro {
17641769
public static func expansion(
17651770
of node: AttributeSyntax,
1766-
providingConformancesOf decl: some DeclGroupSyntax,
1767-
in context: some MacroExpansionContext
1768-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1769-
return [("Initializable", nil)]
1770-
}
1771-
1772-
public static func expansion(
1773-
of node: AttributeSyntax,
1774-
providingMembersOf decl: some DeclGroupSyntax,
1771+
attachedTo: some DeclGroupSyntax,
1772+
providingExtensionsOf type: some TypeSyntaxProtocol,
1773+
conformingTo protocols: [TypeSyntax],
17751774
in context: some MacroExpansionContext
1776-
) throws -> [DeclSyntax] {
1777-
let requirement: DeclSyntax =
1775+
) throws -> [ExtensionDeclSyntax] {
1776+
let ext: DeclSyntax =
17781777
"""
1779-
init(value: Int) {}
1778+
extension \(type.trimmed): Initializable {
1779+
init(value: Int) {}
1780+
}
17801781
"""
1781-
1782-
return [requirement]
1782+
return [ext.cast(ExtensionDeclSyntax.self)]
17831783
}
17841784
}
17851785

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)