Repository navigation
JNI: fix existential result boxing in Swift 6 language mode - #930
Open
Om-singhaI wants to merge 2 commits into
Open
Om-singhaI wants to merge 2 commits into
Om-singhaI wants to merge 2 commits into
Conversation
Results of type `any P` or `some P` are boxed in a branch guarded by `#if hasFeature(ImplicitOpenExistentials)`. That branch used `UnsafeMutablePointer<type(of: value)>`, which is not valid Swift, so the generated thunks failed to compile in Swift 6 language mode or with the upcoming feature enabled. Define the generic box helper above the `#if` and call it directly when the feature is on, since the call then opens the existential. Swift 5 mode still uses `_openExistential` and compiles the same code as before.
ktoso
reviewed
Oct 6, 2026
The JNI sample builds MySwiftLibrary in Swift 5 mode, so its runtime tests only went through the `_openExistential` branch of the generated thunks, and the branch taken in Swift 6 mode was never compiled in CI. Enable the ImplicitOpenExistentials upcoming feature for that target so the protocol tests run through that branch, and add GreeterFactory to cover a method and a computed property returning `any Greeter`, and a method returning `some Greeter`. The new tests also check that the returned value carries the concrete type's metadata.
Contributor
Author
|
I turned on the ImplicitOpenExistentials upcoming feature for MySwiftLibrary in the JNI sample, so ReturnProtocolTest and the other protocol tests now run the |
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.
When a JNI function returns
any Porsome P, the thunk boxes the result with this:type(of: value)is a value, not a type, so it can't be a generic argument. Swift 5 mode never takes that branch, and the JNI sample app is the only package in CI that extractsany Porsome Preturns (ReturnProtocol.swift and others). It builds with.swiftLanguageMode(.v5), so CI doesn't compile it. In Swift 6 mode, or with theImplicitOpenExistentialsupcoming feature on, the generated file fails to build:This is the Swift 6 failure I mentioned at the end of #925, and one of the cases behind #472.
The
resultBox$<T>helper now sits above the#if, and the feature branch calls it directly. With implicit opening that call opens the existential, soTis the concrete type, the same as_openExistentialgives in Swift 5. The existential property setter already does it this way. Swift 5 mode compiles the same code as before.I added
returnsExistentialProtocol_boxing_swiftto JNIProtocolTests. It fails without the source change, and JNIProtocolTests and JNIAsyncTests pass with it.I also typechecked the generated thunks with swiftc (Swift 6.4) for the JNI sample's ReturnProtocol.swift, plus a computed property and a class method returning
any P. On main they fail with the errors above in Swift 6 mode and with-enable-upcoming-feature ImplicitOpenExistentials. With this change they are clean in both of those and in Swift 5 mode. A small runtime check confirmed that in Swift 6 the helper gets the concrete type, notany P.