Skip to content

Commit a1ce686

Browse files
committed
Move SendableExtensionMacro from ExamplePlugin to MacroExamples target
1 parent e732bf5 commit a1ce686

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
import SwiftSyntaxMacros
15+
16+
public enum SendableExtensionMacro: ExtensionMacro {
17+
public static func expansion(
18+
of node: AttributeSyntax,
19+
attachedTo declaration: some DeclGroupSyntax,
20+
providingExtensionsOf type: some TypeSyntaxProtocol,
21+
conformingTo protocols: [TypeSyntax],
22+
in context: some MacroExpansionContext
23+
) throws -> [ExtensionDeclSyntax] {
24+
if protocols.isEmpty {
25+
return []
26+
}
27+
28+
let sendableExtension: DeclSyntax =
29+
"""
30+
extension \(type.trimmed): Sendable {}
31+
"""
32+
33+
guard let extensionDecl = sendableExtension.as(ExtensionDeclSyntax.self) else {
34+
return []
35+
}
36+
37+
return [extensionDecl]
38+
}
39+
}

Examples/Sources/MacroExamples/Implementation/Plugin.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ struct MyPlugin: CompilerPlugin {
4141
PeerValueWithSuffixNameMacro.self,
4242
MemberDeprecatedMacro.self,
4343
EquatableConformanceMacro.self,
44+
SendableExtensionMacro.self,
4445
]
4546
}
4647
#endif

Examples/Sources/MacroExamples/Interface/ExtensionMacros.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414

1515
@attached(extension, conformances: Equatable)
1616
public macro equatable() = #externalMacro(module: "MacroExamplesImplementation", type: "EquatableConformanceMacro")
17+
18+
// MARK: - Sendable Extension
19+
20+
@attached(extension, conformances: Sendable)
21+
public macro sendable() = #externalMacro(module: "MacroExamplesImplementation", type: "SendableExtensionMacro")

Examples/Sources/MacroExamples/Playground/ExtensionMacrosPlayground.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,27 @@ func runEquatableConformanceMacroPlayground() {
2626

2727
print("Is Tom different form Jerry?", cat == mouse ? "No" : "Yes")
2828
}
29+
30+
// MARK: - Sendable Conformance
31+
32+
@sendable
33+
final class Message {
34+
let text: String
35+
let sender: String
36+
37+
init(text: String, sender: String) {
38+
self.text = text
39+
self.sender = sender
40+
}
41+
}
42+
43+
func runSendableConformanceMacroPlayground() async {
44+
let message = Message(text: "Hello, World!", sender: "Alice")
45+
46+
// Example using Task to demonstrate Sendable conformance
47+
let newMessage = await Task {
48+
message
49+
}.value
50+
51+
print("Successfully transferred message from:", newMessage.sender)
52+
}

Examples/Sources/MacroExamples/Playground/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ runExpressionMacrosPlayground()
3333
// MARK: - Extension Macros
3434

3535
runEquatableConformanceMacroPlayground()
36+
await runSendableConformanceMacroPlayground()
3637

3738
// MARK: - Member Attribute Macros
3839

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import MacroExamplesImplementation
14+
import SwiftSyntaxMacros
15+
import SwiftSyntaxMacrosTestSupport
16+
import XCTest
17+
18+
// FIXME: Uncomment when https://github.com/apple/swift-syntax/issues/2031 is fixed
19+
/*
20+
final class SendableExtensionMacroTests: XCTestCase {
21+
private let macros = ["sendable": SendableExtensionMacro.self]
22+
23+
func testExpansionAddsSendableConformance() {
24+
assertMacroExpansion(
25+
"""
26+
@sendable
27+
final class Message {
28+
let text: String
29+
let sender: String
30+
31+
init(text: String, sender: String) {
32+
self.text = text
33+
self.sender = sender
34+
}
35+
}
36+
""",
37+
expandedSource: """
38+
final public class Message {
39+
let text: String
40+
let sender: String
41+
}
42+
43+
extension Message: Sendable {
44+
}
45+
""",
46+
macros: macros,
47+
indentationWidth: .spaces(2)
48+
)
49+
}
50+
}
51+
*/

0 commit comments

Comments
 (0)