Skip to content

jextract: deduplicate protocol requirements before code generation - #916

Open
Hokila wants to merge 3 commits into
swiftlang:mainfrom
Hokila:fix-891-protocol-default-jni
Open

Hokila wants to merge 3 commits into
swiftlang:mainfrom
Hokila:fix-891-protocol-default-jni

Conversation

@Hokila

@Hokila Hokila commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Deduplicate protocol requirements before code generation, so default implementations are not treated as separate callable requirements.
  • Compare protocol requirements using a normalized callable signature that ignores implementation-only differences such as local parameter names, default arguments, and narrower throwing effects.
  • Apply the deduplicated requirements consistently to Swift JNI thunks, Java bindings, and Java callback wrappers.
  • Preserve distinct Swift overloads by keeping argument labels and API kind as part of requirement identity.
  • Add regression tests covering protocol default implementations, differing parameter names, overloads, and non-throwing default implementations satisfying throwing requirements.

Fixes #891

Testing

  • swift test --filter JNIProtocolTests
  • swift test --filter JExtractSwiftTests
  • xcrun swift-format lint --configuration .swift-format on changed Swift files

@Hokila
Hokila requested a review from ktoso as a code owner September 15, 2026 08:17
@Hokila

Hokila commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

@ktoso Gentle ping on this one when you have a chance. The CI checks are green, and this addresses #891. Please let me know if you'd like any changes. Thanks!

@ktoso

ktoso commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

This seems to be fixing the symptom and not the problem itself? We should insttead fix how requirements are collected maybe?


let protocolDefaultImplementationSource = """
public protocol Test {
public func action()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't valid swift tbh, can't have the public here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, sorry for this bug

/// function (including requirements inherited from refined protocols).
private func printExistentialBoxDispatchThunks(_ printer: inout SwiftPrinter, _ type: ExtractedNominalType) {
let boxParentName = SwiftQualifiedTypeName(type.swiftNominal.javaExistentialBoxName)
var emittedCDeclSymbols: Set<String> = []

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit hacky, like fixing the symptom rathe rthan maybe identify them differently to begin with?

@Hokila Hokila Oct 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the deduplication upstream to uniqueProtocolRequirements, so both Swift thunks and Java bindings use the deduplicated protocol requirements.

I also added signature normalization and regression tests to cover cases like different parameter names, overloads, and a non-throwing default implementation satisfying a throwing requirement.

Hokila added 2 commits October 2, 2026 15:13
- Move deduplication logic into allProtocolRequirementMethods so both Swift thunks and Java bindings are deduplicated.
- Include apiKind in the deduplication key to avoid dropping property setters.
- Remove redundant emission-level deduplication and dead helper code.
- Fix invalid 'public' modifier in test extensions (thanks @ktoso).
- Add test for Java-side existential box deduplication.
@Hokila Hokila changed the title jextract: deduplicate protocol default JNI thunks jextract: deduplicate protocol requirements before code generation Oct 2, 2026
@Hokila

Hokila commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

Update PR title and description for current implementation.

@Hokila
Hokila requested a review from ktoso October 8, 2026 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compilation error for default protocol function implementation: "invalid redeclaration of..."

2 participants