Skip to content

jextract/jni: Fix label-based overloading for JNI#742

Merged
ktoso merged 2 commits into
swiftlang:mainfrom
sidepelican:jni_labeled_overload
May 13, 2026
Merged

jextract/jni: Fix label-based overloading for JNI#742
ktoso merged 2 commits into
swiftlang:mainfrom
sidepelican:jni_labeled_overload

Conversation

@sidepelican
Copy link
Copy Markdown
Contributor

@sidepelican sidepelican commented May 13, 2026

This PR fixes an issue where label-based function overloading was not working correctly for JNI.
While this feature is already supported on the FFM side, the JNI implementation was incomplete.

Interestingly, standalone Java code generation (as seen in MethodImportTests) seemed to work fine. However, when both Swift and Java code were generated simultaneously, the resulting source code became corrupted.

The root cause was that the Swift-side code generator was caching state-dependent values in JNISwift2JavaGenerator incorrectly.
This created a strange behavior where the logic appeared to work in isolated unit tests but failed during actual full-scale code generation.

  • Example compile error
.../MySwiftLibrary.java:260: error: method globalOverloaded(long) is already defined in class MySwiftLibrary
  public static long globalOverloaded(long b) throws SwiftIntegerOverflowException {
                     ^
.../MySwiftLibrary.java:263: error: method $globalOverloaded(long) is already defined in class MySwiftLibrary
  private static native long $globalOverloaded(long b);
                             ^
.../MySwiftLibrary.java:275: error: method globalOverloaded(long) is already defined in class MySwiftLibrary
  public static long globalOverloaded(long c) throws SwiftIntegerOverflowException {
                     ^
.../MySwiftLibrary.java:278: error: method $globalOverloaded(long) is already defined in class MySwiftLibrary
  private static native long $globalOverloaded(long c);

This PR updates the Swift code generator to address this issue.

Note

This PR is currently a Draft as it depends on #741. I will mark it as ready for review once the dependency is merged.

@sidepelican sidepelican force-pushed the jni_labeled_overload branch from d1c71a4 to e2a2458 Compare May 13, 2026 07:06
@sidepelican sidepelican marked this pull request as ready for review May 13, 2026 07:06
@sidepelican sidepelican requested a review from ktoso as a code owner May 13, 2026 07:06
@ktoso ktoso merged commit 061451d into swiftlang:main May 13, 2026
64 checks passed
@sidepelican sidepelican deleted the jni_labeled_overload branch May 13, 2026 23:59
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.

2 participants