Skip to content

Fix: Expand Swift variadic functions into explicit Java overloads - #859

Merged
ktoso merged 13 commits into
swiftlang:mainfrom
amanmaurya92:fix/830-jextract-variadic
Aug 6, 2026
Merged

Fix: Expand Swift variadic functions into explicit Java overloads#859
ktoso merged 13 commits into
swiftlang:mainfrom
amanmaurya92:fix/830-jextract-variadic

Conversation

@amanmaurya92

@amanmaurya92 amanmaurya92 commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #830.

Description

This PR introduces support for generating Java bindings for Swift variadic functions. Previously, variadic functions were not handled correctly in the generated Java code.

Changes Made

  • Variadic Expansion: Added a mechanism to expand variadic Swift functions (e.g., func log(items: String...)) into multiple explicit Java overloads up to a configurable limit (effectiveMaxVariadicOverloads).
  • Immutability Maintained: Refactored FFMSwift2JavaGenerator and JNISwift2JavaGenerator to perform the overload expansion using a local variable during initialization. This keeps analysis as an immutable let property on the generator classes, addressing previous review feedback.
  • Initialization Order Fix: Ensured that self.analysis is fully initialized before invoking instance methods like generateInterfaceWrappers() in JNISwift2JavaGenerator, resolving Swift compiler errors regarding premature use of self.

This ensures we get comprehensive Java bindings for variadic functions without sacrificing the immutability of the generator's internal state.

@amanmaurya92
amanmaurya92 requested a review from ktoso as a code owner August 3, 2026 11:00
@amanmaurya92
amanmaurya92 marked this pull request as draft August 3, 2026 11:02
@ktoso

ktoso commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Though I wonder it it would be better to support it by generating a number of overloads with up to N parameters where we take the N from configuration maybe?

@amanmaurya92

Copy link
Copy Markdown
Contributor Author

Yeah, I like that approach. It seems like a much cleaner solution.
I'll update the PR to do that.

@ktoso

ktoso commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Thanks!

@amanmaurya92 amanmaurya92 changed the title Fix #830: Gracefully skip unsupported variadic parameters Fix: Expand Swift variadic functions into explicit Java overloads Aug 4, 2026
@amanmaurya92
amanmaurya92 marked this pull request as ready for review August 4, 2026 18:22
@amanmaurya92
amanmaurya92 force-pushed the fix/830-jextract-variadic branch from 65e20f3 to 8c5a30f Compare August 5, 2026 02:41

@ktoso ktoso left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is shaping up great, some comments still and please add runtime testing

Comment thread Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator.swift
Comment thread Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator.swift Outdated
Comment thread Sources/SwiftExtract/ExtractedDecls.swift
Comment thread Sources/SwiftJavaConfigurationShared/Configuration.swift Outdated
"sum(long arg0, long arg1, long arg2, long arg3)"
]
)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make a runtime test; we make those by adding a func in the Samples/SwiftJavaExtractJNISampleApp and exercise the functions in Java test code in the same project. This verifies it all works at runtime as well

@ktoso

ktoso commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Thanks this is pretty good, we might want to move the expand... outside the initializer though, but we can do it in a follow up

@amanmaurya92

Copy link
Copy Markdown
Contributor Author

Thanks! I'll fix the failing checks and update the PR.

@amanmaurya92
amanmaurya92 requested a review from ktoso August 6, 2026 02:00
Comment thread .github/actions/prepare_env/action.yml Outdated
@amanmaurya92
amanmaurya92 force-pushed the fix/830-jextract-variadic branch from 2533d08 to fb2d5cd Compare August 6, 2026 02:28
@amanmaurya92
amanmaurya92 requested a review from ktoso August 6, 2026 03:19
Comment thread Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+SwiftThunkPrinting.swift Outdated
@amanmaurya92
amanmaurya92 requested a review from ktoso August 6, 2026 09:38
@amanmaurya92

Copy link
Copy Markdown
Contributor Author

I didn't expect to run into so many formatting issues. I'll get the formatter working locally before my next PR so I can catch these ahead of time.

@ktoso

ktoso commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Hah yeah, sorry about this -- sadly the formatting can be pretty picky, definitely worth getting working locally

@ktoso

ktoso commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

I'll apply fixes, since I'm cutting a release now -- please look into getting formatter working locally though, it should mostly just work tbh 🤔

@ktoso
ktoso force-pushed the fix/830-jextract-variadic branch from a268a44 to 277545e Compare August 6, 2026 13:02
@ktoso
ktoso merged commit 7fd6685 into swiftlang:main Aug 6, 2026
42 checks passed
@ktoso

ktoso commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Thanks again!

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.

jextract: Variadic parameters silently narrowed to single element

2 participants