Skip to content

Commit afff5aa

Browse files
committed
[CompilerPlugin] Update for conformance macro
1 parent 1561e76 commit afff5aa

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

Sources/SwiftCompilerPlugin/Macros.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,40 @@ extension CompilerPlugin {
206206
expanded =
207207
peers
208208
.map { $0.trimmedDescription }
209+
210+
case (let attachedMacro as ConformanceMacro.Type, .conformance):
211+
guard
212+
let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self),
213+
let identified = declarationNode.asProtocol(IdentifiedDeclSyntax.self)
214+
else {
215+
fatalError("type mismatch")
216+
}
217+
218+
// Local function to expand a conformance macro once we've opened up
219+
// the existential.
220+
func expandConformanceMacro<Node: DeclGroupSyntax>(
221+
_ node: Node
222+
) throws -> [(TypeSyntax, GenericWhereClauseSyntax?)] {
223+
return try attachedMacro.expansion(
224+
of: customAttributeNode,
225+
providingConformancesOf: node,
226+
in: context
227+
)
228+
}
229+
230+
let conformances = try _openExistential(
231+
declGroup,
232+
do: expandConformanceMacro
233+
)
234+
235+
// Form a buffer of extension declarations to return to the caller.
236+
expanded = conformances.map { typeSyntax, whereClause in
237+
let typeName = identified.identifier.trimmedDescription
238+
let protocolName = typeSyntax.trimmedDescription
239+
let whereClause = whereClause?.trimmedDescription ?? ""
240+
return "extension \(typeName) : \(protocolName) \(whereClause) {}"
241+
}
242+
209243
default:
210244
fatalError("\(macroDefinition) does not conform to any known attached macro protocol")
211245
}

Sources/SwiftCompilerPlugin/PluginMessages.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ internal enum PluginToHostMessage: Codable {
4747
}
4848

4949
/*namespace*/ internal enum PluginMessage {
50-
static var PROTOCOL_VERSION_NUMBER: Int { 1 }
50+
static var PROTOCOL_VERSION_NUMBER: Int { 2 } // Added 'MacroRole.conformance'
5151

5252
struct PluginCapability: Codable {
5353
var protocolVersion: Int
@@ -72,6 +72,7 @@ internal enum PluginToHostMessage: Codable {
7272
case memberAttribute
7373
case member
7474
case peer
75+
case conformance
7576
}
7677

7778
struct SourceLocation: Codable {

lit_tests/compiler_plugin_basic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// RUN: -dump-macro-expansions \
88
// RUN: -load-plugin-executable %examples_bin_path/ExamplePlugin#ExamplePlugin \
99
// RUN -module-name MyApp \
10-
// RUN: %s > %t/expansions-dump.txt 2>&1
10+
// RUN: %s 2>&1 | tee %t/expansions-dump.txt
1111
//
1212
// RUN: %FileCheck %s < %t/expansions-dump.txt
1313

0 commit comments

Comments
 (0)