Repository navigation
JNI: non-escaping closures taking or returning jextracted types - #20
Open
piercifani wants to merge 2 commits into
Open
piercifani wants to merge 2 commits into
piercifani wants to merge 2 commits into
Conversation
The non-escaping closure lowering converts primitive arguments and results only, so a function taking `(Item) -> Item` was skipped. When that lowering can't translate a non-escaping closure, it now goes through the escaping one, whose `@JavaInterface` wrapper already converts jextracted types, optionals of them and strings. Its Swift closure is passed where a non-escaping one is expected. The immediately-called literal that builds it spells out its closure type in that case: inferred from the non-escaping parameter, it couldn't return the closure. Java callers see the same functional interface as before, `<Type>.<method>.<parameter>`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack 16/21 · base:
jni/15-nullable-optionals· previous: #19 · next: #21 (jni/17-closure-results)Problem
A non-escaping closure whose arguments or result are jextracted types was skipped:
func transform(_ item: Item, _ f: (Item) -> Item) -> Itemdidn't reach Java. The non-escaping closure lowering converts primitives only.Changes
When the non-escaping lowering can't translate a closure, it now goes through the escaping one. Its
@JavaInterfacewrapper already converts jextracted types, optionals of them and strings, and the Swift closure it builds can be passed where a non-escaping one is expected.{ () -> (Item) -> Item in … }(), for a non-escaping parameter. Inferred from that parameter, the returned closure would be non-escaping, and returning it wouldn't compile.<Type>.<method>.<parameter>, with jextracted types in itsapply.The escaping path costs a global reference per call to hold the Java object for as long as the Swift closure lives, here for the duration of the call.
Kotlin in this PR
ClosuresTest.ktwithClosures.swift:Why Skip needs it
Skip passes Kotlin lambdas for Swift closure parameters whatever their types, and non-escaping is Swift's default, so closures over the app's own structs are common.
Testing
./gradlew :Samples:SwiftJavaExtractJNIKotlinSampleApp:testpasses (43 tests).JNIEscapingClosureJExtractedTypesTests: two new tests check the@JavaInterfacewrapper and the spelled-out closure type for a non-escaping(Item) -> Item, and the Java interface.ClosuresTest.nonEscapingStructClosurepasses a struct-returning lambda totransformStruct.swift test --disable-experimental-prebuilts --disable-sandbox(675 tests) and./gradlew :Samples:SwiftJavaExtractJNISampleApp:test(294 tests) pass.swift format lint --stricton changed files;git diff --check.Not in this PR
CompletableFuturethat Swift awaits.🤖 Generated with Claude Code