Repository navigation
jextract: keep Swift names that are Java keywords out of generated Java - #954
Merged
Merged
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>
ktoso
approved these changes
Nov 8, 2026
ktoso
left a comment
Collaborator
There was a problem hiding this comment.
I'm happy with the change, thanks.
For future contributions I would request you write at least your commit messages with some of your own voice and not entirely generate them -- this is outlined in the project policy: https://github.com/swiftlang/project-operations/blob/main/contributing/ai-tools.md#writing-and-voice It can be hard to identify how much a contributor thought through a change if it is entirely generated. In this case I'm confident in it, but in others I'm not so sure, so some more argumentation would be helpful, thanks in advance 🙏
Contributor
Author
|
@ktoso fair point! Will do next time! |
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