## Fix Swift overloads producing uncompilable Java wrappers#544
## Fix Swift overloads producing uncompilable Java wrappers#544Clemo97 wants to merge 6 commits intoswiftlang:mainfrom
Conversation
|
Thanks for the request.what’s a reasonable direction however we should only be doing this if we detect a method has conflicts, otherwise it could be pretty annoying. Many members don’t have overloads and we can still call them with her normal names after all. |
ktoso
left a comment
There was a problem hiding this comment.
Yeap still missing the required logic.
ktoso
left a comment
There was a problem hiding this comment.
marking as requesting changes
Yeah, my thought is to add a conflict detection logic one of the FFM directories that will collect all methods that will be generated from a class, compute each methods java signtature without suffixes then use that to detect duplicates from overloading and add parameter label suffixes to conflicting ones. @copilot |
|
I appreciate the will to help out but please don’t just at a coding assistant and expect it to figure out the implementation. when you’re contributing you are ultimately responsible for the quality of the code. Feel free to use coding assistants but please verify and adjust their results such that they are passing our standards for quality and achieve the goals set out. Back on the topic of implementation: yeah basically we’ll need such “conflicting names” mapping and then do another pass over them applying the renaming |
Thanks for the feedback. |
|
@Clemo97 are you working on this actively? I'm a bit confused with the periodic merges here but no action taken on the requested change? |
| MySwiftLibrary.globalReceiveOptional_o1_o2(OptionalLong.of(12), Optional.of(dat)); | ||
| var dat = Data.init(bytes, bytes.byteSize(), arena); | ||
| MySwiftLibrary.globalReceiveSomeDataProtocol(dat); | ||
| MySwiftLibrary.globalReceiveOptional(OptionalLong.of(12), Optional.of(dat)); |
There was a problem hiding this comment.
cool, now we need actual tests for the conflicting behavior please :)
| /// Detects method name conflicts within a nominal type and marks methods that need parameter label suffixes. | ||
| /// This implements a two-pass approach: first detect conflicts, then apply suffixes only where needed. | ||
| /// Only marks methods as conflicting if they would have identical Java signatures (same parameter types). | ||
| func detectMethodNameConflicts(for decl: ImportedNominalType) { |
There was a problem hiding this comment.
Can this be "detectJavaMethodNameDupes"? And can it please return a value instead of mutating self directly?
| // Only mark methods as conflicting if they share the same Java signature | ||
| for group in signatureGroups where group.count > 1 { | ||
| for method in group { | ||
| methodsNeedingSuffixes.insert(ObjectIdentifier(method)) |
There was a problem hiding this comment.
I don't love the ObjectIdentifier because it will be hard to debug and print those; can you please use some readable representation for the storage? Can be string names I guess
There was a problem hiding this comment.
Maybe all this needs to become a class which is "DuplicateNames" and inside there do the storage, detection and everything -- that'll be much cleaner
| struct JavaTranslation { | ||
| let config: Configuration | ||
| var knownTypes: SwiftKnownTypes | ||
| let shouldAppendParameterLabels: Bool |
There was a problem hiding this comment.
Do we need this explicitly or should we pass the DuplicateNames call then during translate(), that might be cleaner than adding bools
| // Conflict detected: append parameter labels for disambiguation | ||
| suffix = decl.functionSignature.parameters | ||
| .map { "_" + ($0.argumentLabel ?? "_") } | ||
| .joined() |
There was a problem hiding this comment.
Make this into a private method makeMethodNameWithParamsSuffix()
There was a problem hiding this comment.
The whole "javaName = makeJavaMethodName(decl)" and put the logic there
Fixes #526
Problem
Swift allows method overloading using parameter labels (argument labels), where methods can have the same base name but different parameter labels:
In Swift, these are distinct methods because parameter labels are part of the function signature. However, when translated to Java, both were being generated with the same signature, causing compilation failures:
Error:
Solution
Modified the Java method name generation in
FFMSwift2JavaGenerator+JavaTranslation.swiftto append parameter labels as suffixes, creating unique method names that preserve Swift semantics while ensuring valid Java code.Implementation:
_a,_b)Example transformation:
Changes
Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift: Added logic to append parameter labels to method names for disambiguationSamples/SwiftJavaExtractFFMSampleApp/src/main/java/com/example/swift/HelloJava2Swift.java: Updated sample code to use new method naming convention (e.g.,globalTakeInt_i(),init_len_cap())Samples/SwiftJavaExtractFFMSampleApp/Sources/MySwiftLibrary/OverloadSample.swift: Added test cases with overloaded methods.