Skip to content

Commit 2a004bf

Browse files
piercifaniclaude
andcommitted
Enum case closure payloads are escaping
An associated value is stored in the enum, but a closure payload without `@escaping` (which Swift doesn't allow, or need, there) was lowered as non-escaping: the case constructor's thunk captured the call's JNI environment and local reference in the closure the enum keeps, which dangle once the call returns, and which Swift 6 rejects for `@MainActor` closures. Case constructors now take closures as escaping, like a property setter's `newValue`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8d08945 commit 2a004bf

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

‎Sources/SwiftExtract/SwiftTypes/SwiftFunctionSignature.swift‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,14 @@ extension SwiftFunctionSignature {
171171
enclosingType: SwiftType,
172172
lookupContext: SwiftTypeLookupContext
173173
) throws {
174+
// An associated value is stored in the enum, so a closure payload is always escaping.
174175
let parameters = try node.parameterClause?.parameters.map { param in
175-
try SwiftParameter(param, lookupContext: lookupContext)
176+
var parameter = try SwiftParameter(param, lookupContext: lookupContext)
177+
if case .function(var functionType) = parameter.type {
178+
functionType.isEscaping = true
179+
parameter.type = .function(functionType)
180+
}
181+
return parameter
176182
}
177183

178184
self.init(

‎Tests/JExtractSwiftTests/JNI/JNIEnumTests.swift‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ extension JNIEnumTests {
416416
return java.util.Optional.of(new Case.Custom());
417417
}
418418
""",
419-
"public static Policy custom(java.util.function.LongPredicate arg0, SwiftArena swiftArena) {",
419+
"public static Policy custom(com.example.swift.Policy.custom.arg0 arg0, SwiftArena swiftArena) {",
420420
"public boolean allows(long value) {",
421421
]
422422
)
@@ -430,6 +430,8 @@ extension JNIEnumTests {
430430
.swift,
431431
expectedChunks: [
432432
"Java_com_example_swift_Policy__00024custom__",
433+
// The enum stores the closure: it holds the Java object, not the call's local reference.
434+
"let javaInterface_arg0$ = JavaPolicy_custom_arg0(javaThis: arg0, environment: environment)",
433435
"Java_com_example_swift_Policy__00024allows__JJ",
434436
],
435437
notExpectedChunks: [

0 commit comments

Comments
 (0)