@@ -57,6 +57,26 @@ public struct LocalFuncAndVarMacro: DeclarationMacro {
5757 }
5858}
5959
60+ public struct FuncFromClosureMacro : DeclarationMacro {
61+ public static func expansion(
62+ of node: some FreestandingMacroExpansionSyntax ,
63+ in context: some MacroExpansionContext
64+ ) throws -> [ DeclSyntax ] {
65+ guard
66+ let closure = node. trailingClosure,
67+ let arg1 = node. argumentList. first? . expression else {
68+ return [ ]
69+ }
70+
71+ return [ """
72+ func fromClosure() {
73+ print( \( arg1) )
74+ \( closure. statements)
75+ }
76+ """ ]
77+ }
78+ }
79+
6080//--- test.swift
6181
6282@freestanding ( declaration, names: named ( globalFunc) , named ( globalVar) ) macro globalDecls( ) = #externalMacro( module: " MacroDefinition " , type: " GlobalFuncAndVarMacro " )
@@ -97,3 +117,44 @@ func testLocal() {
97117 }
98118#endif
99119}
120+
121+ @freestanding ( declaration, names: named ( fromClosure) ) macro func FromClosureMacro( _: String , _: ( ) -> Void ) = #externalMacro( module: " MacroDefinition" , type: " FuncFromClosureMacro" )
122+
123+ @available ( macOS 99 , * )
124+ func APIFrom99( ) -> String { " " }
125+ @available ( macOS 999 , * )
126+ func APIFrom999( ) -> String { " " }
127+
128+ @available ( macOS 99 , * )
129+ #funcFromClosureMacro( APIFrom99 ( ) ) {
130+ _ = APIFrom99 ( )
131+ if #available( macOS 999 , * ) {
132+ _ = APIFrom99 ( )
133+ _ = APIFrom999 ( )
134+ }
135+ }
136+
137+ struct S1 {
138+ @available ( macOS 99 , * )
139+ #funcFromClosureMacro( APIFrom99 ( ) ) {
140+ _ = APIFrom99 ( )
141+ if #available( macOS 999 , * ) {
142+ _ = APIFrom99 ( )
143+ _ = APIFrom999 ( )
144+ }
145+ }
146+ }
147+
148+ // FIXME: Diagnostics could be better.
149+ struct S2 { // expected-note 4 {{add @available attribute to enclosing struct}}
150+ // expected-note@+3 6 {{in expansion of macro 'funcFromClosureMacro' here}}
151+ // expected-error@+2 {{'APIFrom99()' is only available in macOS 99 or newer}}
152+ // expected-error@+2 {{'APIFrom99()' is only available in macOS 99 or newer}} expected-note@+2 {{add 'if #available' version check}}
153+ #funcFromClosureMacro( APIFrom99 ( ) ) {
154+ _ = APIFrom99 ( )
155+ if #available( macOS 999 , * ) {
156+ _ = APIFrom99 ( )
157+ _ = APIFrom999 ( )
158+ }
159+ }
160+ }
0 commit comments