Repository navigation
jextract: keep Swift names that are Java keywords out of generated Java - #954
Open
piercifani wants to merge 1 commit into
Open
piercifani wants to merge 1 commit into
piercifani wants to merge 1 commit into
Conversation
Swift parameters may be named `long`, `int`, `double`, `default` or `class`, and properties may be written with backticks, such as `` `default` ``. In JNI mode those names reached the generated Java as they are: `boolean long`, `record Quote(String slug, long long)` and `get`default`()` don't compile, and the Swift thunks had the same problem with `class` and `default`. - Parameter names, closure parameter names and enum case payload names get a `_` suffix when they are Java keywords or reserved Swift keywords, in both the Java bindings and the Swift thunks. Argument labels stay as they are. - Getter and setter names drop a property's backticks: `getDefault()`. - Method names that would redeclare a final method of `java.lang.Object` (`getClass`, `notify`, `notifyAll`, `wait`), including the getter of a property named `class`, get a `_` suffix, as Java keywords already did. JNIJavaKitTests expected `long int` as a Java parameter, which doesn't compile; it now expects `long int_`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
In JNI mode, Swift names that are Java keywords, or that Swift writes with backticks, reached the generated Java as they are:
That generated:
boolean longandlong intas parameters;record Quote(java.lang.String slug, long long);get`default`();wait()that redeclaresObject.wait().None of it compiles with javac. The Swift thunks broke the same way for parameters named
classordefault.JNIJavaKitTestseven expectedlong intas a Java parameter.Changes
_suffix when they are a Java keyword or a reserved Swift keyword:long_,int_,default_. This applies in both the Java bindings and the Swift thunks; argument labels are unchanged, so the thunk still callsscale(double: …, int: …, default: …).getDefault().java.lang.Objectmethod (getClass,notify,notifyAll,wait) get a_suffix, the same way Java-keyword method names already do. That includes the getter of a property namedclass.FFM mode builds its parameter names separately and is unchanged here.
Testing
JavaKeywordTests(4 tests): keyword parameter names in the Java bindings and the Swift thunks, enum-case payload names, a backticked property, andwait/notify/theclassgetter.JNIJavaKitTestsupdated:long int_instead oflong int.JExtractSwiftPluginwhose API uses all of these names. The generated Java compiles with javac and the generated Swift builds in Swift 6 mode. From Java,wait_(),notify_(1),getClass_()andgetDefault()reach Swift through their renamed JNI symbols.swift testpasses.🤖 Generated with Claude Code