← Back to Pull requests

Bridge closures with closure parameters in the remaining directions #305

Closed
unknown closed this pull request without merging on Oct 22, 2026 10:23 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 12:50 UTC
Author

#249 made it possible for Swift to call a Kotlin closure that takes closure parameters, through the @_cdecl path (skiptools/skip-bridge#109). Three other cases still sent the nested closure arguments through AnyBridging, which cannot convert between Swift closures and Kotlin functions:

| Bridged type | Closure provided by | Called from | Generated by | Previous result |
|---|---|---|---|---|
| Swift type bridged to Kotlin | Swift | Kotlin, with a Kotlin lambda | convertToCDecl | Unable to bridge Kotlin/Java instance |
| Kotlin type bridged to Swift | Swift (assigned to the property) | Kotlin, with a Kotlin lambda | convertToJava | same |
| Kotlin type bridged to Swift | Kotlin | Swift, with a Swift closure | convertClosureFromJava | Unable to bridge Swift instance (Function) |

For example, Kotlin calling public var onDeposit: @MainActor (@escaping @MainActor () -> Void) -> Void on a bridged Swift class crashed. This is the reverse of skiptools/skip-bridge#101.

Change

  • A new convertClosureToJava wraps a Swift closure whose parameters include closures. Those parameters arrive as JavaObjectPointer? and are converted with convertClosureFromCDecl using their static types. It is used by both convertToCDecl and convertToJava:

  •   SwiftClosure2.javaObject(forMainActor: { @MainActor [_closure = c] (p0: Int, p1: JavaObjectPointer?) in _closure(p0, SwiftClosure0.closure(forJavaObject: p1, options: [])! as @MainActor () -> Void) }, options: [])!
      

    The capture list keeps the closure value from the time it is bridged, as before. Optional closures go through .map. throws and @MainActor are preserved.
  • convertClosureFromJava converts closure arguments with convertClosureToJava before calling JavaBackedClosure.invoke.

  • The #249 @_cdecl conversion has moved into convertClosureFromCDecl, and it now also uses convertClosureToJava for its nested arguments, so deeper nesting goes through the same code. The two existing nested-closure tests therefore now expect ! after the inner javaObject(...) call; it cannot fail for a non-optional closure.
  • As with #249, async closures and closures with more than 5 parameters are still not handled.

    Testing

  • New codegen tests: BridgeToKotlinTests.testNestedClosureVar, testOptionalNestedClosureVar and BridgeToSwiftTests.testNestedClosureVar. All 253 BridgeTo* tests pass.

  • skip-bridge runtime tests that cover all three directions are in a companion skip-bridge PR, which needs a skipstone release containing this change. Against a local build of this branch, its Kotlin-compat (9/9) and BridgeToSwift (71/71) Robolectric suites pass. Against skipstone 1.9.16, the new Kotlin-to-Swift test crashes with Unable to bridge Kotlin/Java instance.
  • 🤖 Generated with Claude Code

    Diff: 356 lines (+267 / -89) across 4 files Reviewer: marcprux Review duration: 1293m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 356 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.352
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #305"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.8% · Safe to merge
    16.8%