@@ -135,13 +135,22 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
135
135
136
136
// We don't support inout, isolated, or _const parameters on test functions.
137
137
for parameter in parameterList {
138
- if let specifier = parameter. type. as ( AttributedTypeSyntax . self) ? . specifier {
139
- switch specifier. tokenKind {
140
- case . keyword( . inout) , . keyword( . isolated) , . keyword( . _const) :
138
+ let invalidSpecifierKeywords : [ TokenKind ] = [ . keyword( . inout) , . keyword( . isolated) , . keyword( . _const) , ]
139
+ if let parameterType = parameter. type. as ( AttributedTypeSyntax . self) {
140
+ #if canImport(SwiftSyntax600)
141
+ for specifier in parameterType. specifiers {
142
+ guard case let . simpleTypeSpecifier( specifier) = specifier else {
143
+ continue
144
+ }
145
+ if invalidSpecifierKeywords. contains ( specifier. specifier. tokenKind) {
146
+ diagnostics. append ( . specifierNotSupported( specifier. specifier, on: parameter, whenUsing: testAttribute) )
147
+ }
148
+ }
149
+ #else
150
+ if let specifier = parameterType. specifier, invalidSpecifierKeywords. contains ( specifier. tokenKind) {
141
151
diagnostics. append ( . specifierNotSupported( specifier, on: parameter, whenUsing: testAttribute) )
142
- default :
143
- break
144
152
}
153
+ #endif
145
154
}
146
155
}
147
156
@@ -244,21 +253,41 @@ public struct TestDeclarationMacro: PeerMacro, Sendable {
244
253
private static func _createCaptureListExpr(
245
254
from parametersWithLabels: some Sequence < ( DeclReferenceExprSyntax , FunctionParameterSyntax ) >
246
255
) -> ClosureCaptureClauseSyntax {
247
- ClosureCaptureClauseSyntax {
248
- for (label, parameter) in parametersWithLabels {
249
- if case let . keyword( specifierKeyword) = parameter. type. as ( AttributedTypeSyntax . self) ? . specifier? . tokenKind,
250
- specifierKeyword == . borrowing || specifierKeyword == . consuming {
251
- ClosureCaptureSyntax (
252
- name: label. baseName,
253
- equal: . equalToken( ) ,
254
- expression: CopyExprSyntax (
255
- copyKeyword: . keyword( . copy) . with ( \. trailingTrivia, . space) ,
256
- expression: label
257
- )
258
- )
259
- } else {
260
- ClosureCaptureSyntax ( expression: label)
256
+ let specifierKeywordsNeedingCopy : [ TokenKind ] = [ . keyword( . borrowing) , . keyword( . consuming) , ]
257
+ let closureCaptures = parametersWithLabels. lazy. map { label, parameter in
258
+ var needsCopy = false
259
+ if let parameterType = parameter. type. as ( AttributedTypeSyntax . self) {
260
+ #if canImport(SwiftSyntax600)
261
+ needsCopy = parameterType. specifiers. contains { specifier in
262
+ guard case let . simpleTypeSpecifier( specifier) = specifier else {
263
+ return false
264
+ }
265
+ return specifierKeywordsNeedingCopy. contains ( specifier. specifier. tokenKind)
261
266
}
267
+ #else
268
+ if let specifier = parameterType. specifier {
269
+ needsCopy = specifierKeywordsNeedingCopy. contains ( specifier. tokenKind)
270
+ }
271
+ #endif
272
+ }
273
+
274
+ if needsCopy {
275
+ return ClosureCaptureSyntax (
276
+ name: label. baseName,
277
+ equal: . equalToken( ) ,
278
+ expression: CopyExprSyntax (
279
+ copyKeyword: . keyword( . copy) . with ( \. trailingTrivia, . space) ,
280
+ expression: label
281
+ )
282
+ )
283
+ } else {
284
+ return ClosureCaptureSyntax ( expression: label)
285
+ }
286
+ }
287
+
288
+ return ClosureCaptureClauseSyntax {
289
+ for closureCapture in closureCaptures {
290
+ closureCapture
262
291
}
263
292
}
264
293
}
0 commit comments