Skip to content

Commit 30f302d

Browse files
hborlaDougGregor
authored andcommitted
[Macros] Replace all uses of the ConformanceMacro protocol with ExtensionMacro.
(cherry picked from commit d03ef5a)
1 parent ba7628c commit 30f302d

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
@@ -1344,14 +1344,16 @@ public struct EmptyPeerMacro: PeerMacro {
13441344
}
13451345
}
13461346

1347-
public struct EquatableMacro: ConformanceMacro {
1347+
public struct EquatableMacro: ExtensionMacro {
13481348
public static func expansion(
13491349
of node: AttributeSyntax,
1350-
providingConformancesOf decl: some DeclGroupSyntax,
1350+
attachedTo: some DeclGroupSyntax,
1351+
providingExtensionsOf type: some TypeSyntaxProtocol,
1352+
conformingTo protocols: [TypeSyntax],
13511353
in context: some MacroExpansionContext
1352-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1353-
let protocolName: TypeSyntax = "Equatable"
1354-
return [(protocolName, nil)]
1354+
) throws -> [ExtensionDeclSyntax] {
1355+
let ext: DeclSyntax = "extension \(type.trimmed): Equatable {}"
1356+
return [ext.cast(ExtensionDeclSyntax.self)]
13551357
}
13561358
}
13571359

@@ -1412,34 +1414,37 @@ public struct ConformanceViaExtensionMacro: ExtensionMacro {
14121414
}
14131415
}
14141416

1415-
public struct HashableMacro: ConformanceMacro {
1417+
public struct HashableMacro: ExtensionMacro {
14161418
public static func expansion(
14171419
of node: AttributeSyntax,
1418-
providingConformancesOf decl: some DeclGroupSyntax,
1420+
attachedTo: some DeclGroupSyntax,
1421+
providingExtensionsOf type: some TypeSyntaxProtocol,
1422+
conformingTo protocols: [TypeSyntax],
14191423
in context: some MacroExpansionContext
1420-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1421-
let protocolName: TypeSyntax = "Hashable"
1422-
return [(protocolName, nil)]
1424+
) throws -> [ExtensionDeclSyntax] {
1425+
let ext: DeclSyntax = "extension \(type.trimmed): Hashable {}"
1426+
return [ext.cast(ExtensionDeclSyntax.self)]
14231427
}
14241428
}
14251429

1426-
public struct DelegatedConformanceMacro: ConformanceMacro, MemberMacro {
1430+
public struct DelegatedConformanceMacro: ExtensionMacro, MemberMacro {
14271431
public static func expansion(
14281432
of node: AttributeSyntax,
1429-
providingConformancesOf decl: some DeclGroupSyntax,
1433+
attachedTo: some DeclGroupSyntax,
1434+
providingExtensionsOf type: some TypeSyntaxProtocol,
1435+
conformingTo protocols: [TypeSyntax],
14301436
in context: some MacroExpansionContext
1431-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1432-
let protocolName: TypeSyntax = "P"
1437+
) throws -> [ExtensionDeclSyntax] {
14331438
let conformance: DeclSyntax =
14341439
"""
1435-
extension Placeholder where Element: P {}
1440+
extension \(type.trimmed): P where Element: P {}
14361441
"""
14371442

14381443
guard let extensionDecl = conformance.as(ExtensionDeclSyntax.self) else {
14391444
return []
14401445
}
14411446

1442-
return [(protocolName, extensionDecl.genericWhereClause)]
1447+
return [extensionDecl]
14431448
}
14441449

14451450
public static func expansion(
@@ -1894,26 +1899,21 @@ public struct AddPeerStoredPropertyMacro: PeerMacro, Sendable {
18941899
}
18951900
}
18961901

1897-
public struct InitializableMacro: ConformanceMacro, MemberMacro {
1898-
public static func expansion(
1899-
of node: AttributeSyntax,
1900-
providingConformancesOf decl: some DeclGroupSyntax,
1901-
in context: some MacroExpansionContext
1902-
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
1903-
return [("Initializable", nil)]
1904-
}
1905-
1902+
public struct InitializableMacro: ExtensionMacro {
19061903
public static func expansion(
19071904
of node: AttributeSyntax,
1908-
providingMembersOf decl: some DeclGroupSyntax,
1905+
attachedTo: some DeclGroupSyntax,
1906+
providingExtensionsOf type: some TypeSyntaxProtocol,
1907+
conformingTo protocols: [TypeSyntax],
19091908
in context: some MacroExpansionContext
1910-
) throws -> [DeclSyntax] {
1911-
let requirement: DeclSyntax =
1909+
) throws -> [ExtensionDeclSyntax] {
1910+
let ext: DeclSyntax =
19121911
"""
1913-
init(value: Int) {}
1912+
extension \(type.trimmed): Initializable {
1913+
init(value: Int) {}
1914+
}
19141915
"""
1915-
1916-
return [requirement]
1916+
return [ext.cast(ExtensionDeclSyntax.self)]
19171917
}
19181918
}
19191919

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)