Skip to content

Fix: Identity equality for non Equatable clases.#787

Merged
ktoso merged 3 commits into
swiftlang:mainfrom
madsodgaard:feature/object-equals
Jun 15, 2026
Merged

Fix: Identity equality for non Equatable clases.#787
ktoso merged 3 commits into
swiftlang:mainfrom
madsodgaard:feature/object-equals

Conversation

@madsodgaard

Copy link
Copy Markdown
Contributor

Use identity for equals methods for types that do not implement Equatable, for example classes:

Swift:

final class MySwiftClass {}

Java:

// Extracted by jextract
public final class MySwiftClass {}

MySwiftClass c1 = MySwiftClass.init();
c1.equals(c1) // would return false before

@madsodgaard madsodgaard requested a review from ktoso as a code owner June 14, 2026 10:21
Comment thread Sources/SwiftJava/SwiftObjects.swift Outdated
@JavaMethod
public static func equals(environment: UnsafeMutablePointer<JNIEnv?>!, lhsPointer: Int64, lhsTypePointer: Int64, rhsPointer: Int64, rhsTypePointer: Int64) -> Bool {
// Fallback for non-equatable types (such as classes)
let isEquatableByIdentity = lhsPointer == rhsPointer && lhsTypePointer == rhsTypePointer

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.

A test would be nice to add for these

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.

Also naming nitpick, this is "is equal" and not "is equatable" we're not checking if the types are equatABLE but if they are :)

@ktoso

ktoso commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

I did the tiny followup and will merge, thank you!

@ktoso

ktoso commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Nightly Swift seems broken for unrelated reasons rdar://179674933

@ktoso ktoso merged commit 53c906d into swiftlang:main Jun 15, 2026
81 of 84 checks passed
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