Skip to content

Commit 77de51d

Browse files
authored
Work around a compiler regression affecting exit test value capturing. (#1171)
1 parent e63d542 commit 77de51d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

Sources/Testing/Expectations/ExpectationChecking+Macro.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ public func __checkClosureCall(
11751175
///
11761176
/// - Warning: This function is used to implement the `#expect()` and
11771177
/// `#require()` macros. Do not call it directly.
1178+
#if SWT_FIXED_154221449
11781179
@_spi(Experimental)
11791180
public func __checkClosureCall<each T>(
11801181
identifiedBy exitTestID: (UInt64, UInt64, UInt64, UInt64),
@@ -1199,6 +1200,32 @@ public func __checkClosureCall<each T>(
11991200
sourceLocation: sourceLocation
12001201
)
12011202
}
1203+
#else
1204+
@_spi(Experimental)
1205+
public func __checkClosureCall<each T>(
1206+
identifiedBy exitTestID: (UInt64, UInt64, UInt64, UInt64),
1207+
encodingCapturedValues capturedValues: repeat each T,
1208+
processExitsWith expectedExitCondition: ExitTest.Condition,
1209+
observing observedValues: [any PartialKeyPath<ExitTest.Result> & Sendable] = [],
1210+
performing _: @convention(c) () -> Void,
1211+
expression: __Expression,
1212+
comments: @autoclosure () -> [Comment],
1213+
isRequired: Bool,
1214+
isolation: isolated (any Actor)? = #isolation,
1215+
sourceLocation: SourceLocation
1216+
) async -> Result<ExitTest.Result?, any Error> where repeat each T: Codable & Sendable {
1217+
await callExitTest(
1218+
identifiedBy: exitTestID,
1219+
encodingCapturedValues: Array(repeat each capturedValues),
1220+
processExitsWith: expectedExitCondition,
1221+
observing: observedValues,
1222+
expression: expression,
1223+
comments: comments(),
1224+
isRequired: isRequired,
1225+
sourceLocation: sourceLocation
1226+
)
1227+
}
1228+
#endif
12021229
#endif
12031230

12041231
// MARK: -

Sources/TestingMacros/ConditionMacro.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,20 @@ extension ConditionMacro {
9898
if let trailingClosureIndex {
9999
// Assume that the comment, if present is the last argument in the
100100
// argument list prior to the trailing closure that has no label.
101+
#if SWT_FIXED_154221449
101102
commentIndex = macroArguments[..<trailingClosureIndex].lastIndex { $0.label == nil }
103+
#else
104+
commentIndex = macroArguments[..<trailingClosureIndex].lastIndex { argument in
105+
guard argument.label == nil else {
106+
return false
107+
}
108+
if let expr = argument.expression.as(MacroExpansionExprSyntax.self),
109+
expr.macroName.tokenKind == .identifier("__capturedValue") {
110+
return false
111+
}
112+
return true
113+
}
114+
#endif
102115
} else if macroArguments.count > 1 {
103116
// If there is no trailing closure argument and there is more than one
104117
// argument, then the comment is the last argument with no label (and also
@@ -547,6 +560,7 @@ extension ExitTestConditionMacro {
547560
var leadingArguments = [
548561
Argument(label: "identifiedBy", expression: idExpr),
549562
]
563+
#if SWT_FIXED_154221449
550564
if !capturedValues.isEmpty {
551565
leadingArguments.append(
552566
Argument(
@@ -559,6 +573,19 @@ extension ExitTestConditionMacro {
559573
)
560574
)
561575
}
576+
#else
577+
if let firstCapturedValue = capturedValues.first {
578+
leadingArguments.append(
579+
Argument(
580+
label: "encodingCapturedValues",
581+
expression: firstCapturedValue.typeCheckedExpression
582+
)
583+
)
584+
leadingArguments += capturedValues.dropFirst()
585+
.map(\.typeCheckedExpression)
586+
.map { Argument(expression: $0) }
587+
}
588+
#endif
562589
arguments = leadingArguments + arguments
563590

564591
// Replace the exit test body (as an argument to the macro) with a stub

Sources/TestingMacros/Support/ClosureCaptureListParsing.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct CapturedValueInfo {
3636

3737
/// The expression to assign to the captured value with type-checking applied.
3838
var typeCheckedExpression: ExprSyntax {
39-
#"#__capturedValue(\#(expression.trimmed), \#(literal: name.trimmedDescription), (\#(type.trimmed)).self)"#
39+
#"#__capturedValue(\#(expression.trimmed), \#(literal: name.trimmedDescription), \#(type.trimmed).self)"#
4040
}
4141

4242
init(_ capture: ClosureCaptureSyntax, in context: some MacroExpansionContext) {

0 commit comments

Comments
 (0)