Skip to content

Commit 1286637

Browse files
committed
[Macros] Bump the PluginMessage protocol version number for extension macros
and add a macro plugin test for extension macros.
1 parent d11f4c7 commit 1286637

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

Examples/Sources/ExamplePlugin/ExamplePlugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct ThePlugin: CompilerPlugin {
1010
PeerValueWithSuffixNameMacro.self,
1111
MemberDeprecatedMacro.self,
1212
EquatableConformanceMacro.self,
13+
SendableExtensionMacro.self,
1314
DidSetPrintMacro.self,
1415
PrintAnyMacro.self,
1516
]

Examples/Sources/ExamplePlugin/Macros.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,26 @@ struct EquatableConformanceMacro: ConformanceMacro {
8686
}
8787
}
8888

89+
public struct SendableExtensionMacro: ExtensionMacro {
90+
public static func expansion(
91+
of node: AttributeSyntax,
92+
attachedTo: some DeclGroupSyntax,
93+
providingExtensionsOf type: some TypeSyntaxProtocol,
94+
in context: some MacroExpansionContext
95+
) throws -> [ExtensionDeclSyntax] {
96+
let sendableExtension: DeclSyntax =
97+
"""
98+
extension \(type.trimmed): Sendable {}
99+
"""
100+
101+
guard let extensionDecl = sendableExtension.as(ExtensionDeclSyntax.self) else {
102+
return []
103+
}
104+
105+
return [extensionDecl]
106+
}
107+
}
108+
89109
/// Add 'didSet' printing the new value.
90110
struct DidSetPrintMacro: AccessorMacro {
91111
static func expansion(

Sources/SwiftCompilerPluginMessageHandling/PluginMessages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ internal enum PluginToHostMessage: Codable {
7777
}
7878

7979
/*namespace*/ internal enum PluginMessage {
80-
static var PROTOCOL_VERSION_NUMBER: Int { 5 } // Added 'expandMacroResult'.
80+
static var PROTOCOL_VERSION_NUMBER: Int { 6 } // Added 'expandMacroResult'.
8181

8282
struct HostCapability: Codable {
8383
var protocolVersion: Int

lit_tests/compiler_plugin_basic.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ macro MemberDeprecated() = #externalMacro(module: "ExamplePlugin", type: "Member
3333
@attached(conformance)
3434
macro Equatable() = #externalMacro(module: "ExamplePlugin", type: "EquatableConformanceMacro")
3535

36+
@attached(extension, conformances: Sendable)
37+
macro AddSendbale() = #externalMacro(module: "ExamplePlugin", type: "SendableExtensionMacro")
38+
3639
@attached(accessor)
3740
macro DidSetPrint() = #externalMacro(module: "ExamplePlugin", type: "DidSetPrintMacro")
3841

3942
@Metadata
4043
@MemberDeprecated
4144
@Equatable
45+
@AddSendable
4246
@PeerWithSuffix
4347
struct MyStruct {
4448
@DidSetPrint
@@ -81,6 +85,11 @@ struct MyStruct {
8185
// CHECK-NEXT: extension MyStruct : Equatable {}
8286
// CHECK-NEXT: ------------------------------
8387

88+
// CHECK: @__swiftmacro_7TestApp8MyStruct9EquatablefMe_.swift
89+
// CHECK-NEXT: ------------------------------
90+
// CHECK-NEXT: extension MyStruct: Sendable {}
91+
// CHECK-NEXT: ------------------------------
92+
8493
// CHECK: @__swiftmacro_7TestApp8MyStructV5value11DidSetPrintfMa_.swift
8594
// CHECK-NEXT: ------------------------------
8695
// CHECK-NEXT: {

0 commit comments

Comments
 (0)