Skip to content

JNI: reuse protocol parameter adapters across overloads - #9

Merged
teamhimeh merged 1 commit into
qoncept-forkfrom
codex/fix-jni-protocol-parameter-overloads
Oct 23, 2026
Merged

teamhimeh merged 1 commit into
qoncept-forkfrom
codex/fix-jni-protocol-parameter-overloads

Conversation

@teamhimeh

Copy link
Copy Markdown
Collaborator

Problem

JNI extraction emits a protocol-parameter adapter for each function declaration. Overloads with the same function name, parameter name, and enclosing type can require the same adapter, so the generated Swift contains duplicate class declarations and fails to compile.

For example, both initializers below generate the same adapter for store:

public protocol Store {
  func value() -> Int64
}

public struct Client {
  public init(store: some Store) {}
  public init(store: some Store, extra: Int64) {}
}

With this change, that adapter is emitted once and both initializers reuse it. The same applies to overloaded instance methods and free functions.

Changes

  • Track emitted protocol-parameter adapters for the generator's output batch.
  • Reuse an existing adapter when its name and protocol conformances match; assert that a repeated name never silently acquires a different conformance list.
  • Keep separate adapters for distinct parameter-wrapper names.
  • Add a generator regression that counts adapter declarations for overloaded initializers and free functions, plus Java runtime coverage for overloaded initializers and instance methods using Java callbacks.

Validation

  • Confirmed the new generator regression finds two declarations per shared adapter before the fix and one afterward.
  • swift test --disable-experimental-prebuilts --disable-sandbox --filter JNIProtocol: 27 tests passed.
  • ./gradlew :Samples:SwiftJavaExtractJNISampleApp:test --no-daemon: all Java tests passed, including the new callback round trips.
  • swift-format and git diff --check passed.

Tested with Apple Swift 6.4 and JetBrains Runtime 25 on macOS.

@teamhimeh
teamhimeh force-pushed the codex/fix-jni-protocol-parameter-overloads branch from 769e5d4 to e133956 Compare October 23, 2026 07:58
@teamhimeh
teamhimeh merged commit f9a510e into qoncept-fork Oct 23, 2026
33 of 42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant