Skip to content

jextract: Fix compilation errors in constrained generic extensions#734

Merged
ktoso merged 8 commits into
swiftlang:mainfrom
sidepelican:conditional_extension
May 12, 2026
Merged

jextract: Fix compilation errors in constrained generic extensions#734
ktoso merged 8 commits into
swiftlang:mainfrom
sidepelican:conditional_extension

Conversation

@sidepelican
Copy link
Copy Markdown
Contributor

This PR addresses compilation errors occurring in the generated "opener" code for certain constrained generic extensions.

  1. Protocol-constrained extensions
extension Foo where Element: SomeProtocol {
    public func something() {} 
}

When an extension has a protocol requirement, the functions were being exported into "opener" code that does not satisfy the protocol constraint, leading to a build failure.

  1. Same-type constraints with type parameters on the RHS
public struct Box<Element> {}
public struct Bait {}

extension Box where Bait == Element {
  public func swappedObserveTheBait() {}
}

In cases like where Bait == Element, the generator was incorrectly exporting functions.
Similar to the protocol case, the opener fails to satisfy the same-type requirement when a type parameter is involved on the right-hand side.

I have updated the Swift2JavaVisitor implementation to correctly identify these problematic constraints.

@sidepelican sidepelican requested a review from ktoso as a code owner May 11, 2026 03:23
# Conflicts:
#	Tests/JExtractSwiftTests/JNI/JNIGenericTypeTests.swift
Comment thread Sources/JExtractSwiftLib/Swift2JavaVisitor.swift Outdated
}
return true
return false
}
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.

Hmmm this seems suspicious, one sec

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.

Yeah this is wrong because we must match all same type requirements, I'll push a fix.

@ktoso ktoso merged commit 6aeb052 into swiftlang:main May 12, 2026
64 checks passed
ktoso added a commit to ktoso/swift-java that referenced this pull request May 12, 2026
All same type constraints must match, not just one of them.
This is a follow up after swiftlang#734 (comment)

swiftlang#734
ktoso added a commit to ktoso/swift-java that referenced this pull request May 12, 2026
All same type constraints must match, not just one of them.
This is a follow up after swiftlang#734 (comment)

swiftlang#734
ktoso added a commit that referenced this pull request May 12, 2026
All same type constraints must match, not just one of them.
This is a follow up after #734 (comment)

#734
@sidepelican sidepelican deleted the conditional_extension branch May 12, 2026 02:57
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.

2 participants