File tree Expand file tree Collapse file tree 6 files changed +121
-0
lines changed
Tests/MacroExamples/Implementation/Extension Expand file tree Collapse file tree 6 files changed +121
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ struct MyPlugin: CompilerPlugin {
41
41
PeerValueWithSuffixNameMacro . self,
42
42
MemberDeprecatedMacro . self,
43
43
EquatableConformanceMacro . self,
44
+ SendableExtensionMacro . self,
44
45
]
45
46
}
46
47
#endif
Original file line number Diff line number Diff line change 14
14
15
15
@attached ( extension, conformances: Equatable)
16
16
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 " )
Original file line number Diff line number Diff line change @@ -26,3 +26,27 @@ func runEquatableConformanceMacroPlayground() {
26
26
27
27
print ( " Is Tom different form Jerry? " , cat == mouse ? " No " : " Yes " )
28
28
}
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
+ }
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ runExpressionMacrosPlayground()
33
33
// MARK: - Extension Macros
34
34
35
35
runEquatableConformanceMacroPlayground ( )
36
+ await runSendableConformanceMacroPlayground ( )
36
37
37
38
// MARK: - Member Attribute Macros
38
39
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments