Repository navigation
kotlin: use renamed names for struct and enum field types - #1309
Open
MaxFreedomPollard wants to merge 1 commit into
Open
MaxFreedomPollard wants to merge 1 commit into
MaxFreedomPollard wants to merge 1 commit into
Conversation
A struct field whose type is a renamed struct or enum produced Kotlin that does not compile. The renamed type's own classes are generated under the renamed name (`RenamedInnerNative`, `OptionRenamedInnerNative`, `RenamedEnum`), but two helpers in tool/src/kotlin/formatter.rs built the name from the Rust identifier instead: - `fmt_struct_field_type_native` declared a struct-typed field as `InnerNative` and a `DiplomatOption<Inner>` field as `OptionInnerNative`, while `fmt_field_default` initialised the struct-typed field with `RenamedInnerNative()`. `gen_kt_to_c_for_type` also uses this helper to name the option wrapper for `DiplomatOption` parameters, so a method taking `Option<Inner>` broke the same way. - `fmt_struct_field_native_to_kt` converted an enum-typed field with `MyEnum.fromNative(...)`, while `fmt_struct_field_type_kt` declared it as `RenamedEnum`. These are the two places rust-diplomat#1069 did not switch over to `fmt_type_name`. Use `fmt_type_name` in both. Types without a rename format exactly as before, and no struct field or `DiplomatOption` parameter in feature_tests or example has a renamed type, so no generated bindings change. Adds `test_struct_field_renamed_types`, which checks both helpers on a struct with renamed struct, enum and `DiplomatOption` fields. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Hm, did none of our existing On balance I'd probably prefer a |
This branch has not been deployed
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.
If a struct field's type is a renamed struct or enum, the Kotlin backend generates code that does not compile. For example, with a module-level rename:
the classes come out as
RenamedInner,RenamedInnerNativeandRenamedColor, butRenamedOuter.ktstill uses the Rust names in two places:The cause is two helpers in
tool/src/kotlin/formatter.rsthat read.namedirectly. #1069 moved the rest of the backend over tofmt_type_name, but these two were left:fmt_struct_field_type_native,Type::Structarm: this gives the field's JNA type. It also names theOption...Nativewrapper forDiplomatOption<Inner>fields and, throughgen_kt_to_c_for_type, forOption<Inner>method parameters. Those break the same way, asOptionInnerNative.fmt_struct_field_native_to_kt,Type::Enumarm: this generates the enum'sfromNativeconversion.The neighbouring helpers
fmt_field_defaultandfmt_struct_field_type_ktalready callfmt_type_name. That is why the declared type and the initializer above disagree.The fix calls
fmt_type_namein both arms. Types without a rename produce the same output as before. Nothing infeature_testsorexamplehas a struct field orDiplomatOptionparameter of a renamed type, so the checked-in bindings do not change.test_struct_field_renamed_typesin the formatter tests builds a struct with a renamed struct field, a renamed enum field, andDiplomatOptionfields of each. It checks the strings both helpers return.This has not been compiled or run locally, because I don't have a Rust toolchain on this machine. I checked the change and the expected strings by reading the formatter,
Struct.kt.jinjaandOption.kt.jinja, and laid out the new code by hand to match rustfmt. CI will be its first build.🤖 Generated with Claude Code