Skip to content

Bincompat hooks for revised hash/isEqual interop #71620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/swift/Runtime/Bincompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ bool useLegacyObjCBoxingInCasting();
/// Whether to use legacy semantics when unboxing __SwiftValue
bool useLegacySwiftValueUnboxingInCasting();

/// Legacy semantics use trivial implementations for -hashValue/-isEqual:
/// requests from ObjC to Swift values.
/// New semantics attempt to dispatch to Swift Hashable/Equatable conformances
/// if present.
bool useLegacySwiftObjCHashing();

} // namespace bincompat

} // namespace runtime
Expand Down
24 changes: 24 additions & 0 deletions stdlib/public/runtime/Bincompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,30 @@ bool useLegacySwiftValueUnboxingInCasting() {
#endif
}

// Controls how ObjC -hashValue and -isEqual are handled
// by Swift objects.
// There are two basic semantics:
// * pointer: -hashValue returns pointer, -isEqual: tests pointer equality
// * proxy: -hashValue calls on Hashable conformance, -isEqual: calls Equatable conformance
//
// Legacy handling:
// * Swift struct/enum values that implement Hashable: proxy -hashValue and -isEqual:
// * Swift struct/enum values that implement Equatable but not Hashable: pointer semantics
// * Swift class values regardless of hashable/Equatable support: pointer semantics
//
// New behavior:
// * Swift struct/enum/class values that implement Hashable: proxy -hashValue and -isEqual:
// * Swift struct/enum/class values that implement Equatable but not Hashable: proxy -isEqual:, constant -hashValue
// * All other cases: pointer semantics
//
bool useLegacySwiftObjCHashing() {
#if BINARY_COMPATIBILITY_APPLE
return true; // For now, legacy behavior on Apple OSes
#else
return false; // Always use the new behavior on non-Apple OSes
#endif
}

} // namespace bincompat

} // namespace runtime
Expand Down
10 changes: 10 additions & 0 deletions stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,11 @@ + (BOOL)conformsToProtocol:(Protocol*)proto {
}

- (NSUInteger)hash {
if (runtime::bincompat::useLegacySwiftObjCHashing()) {
// Legacy behavior: Don't proxy to Swift Hashable
return (NSUInteger)self;
}

auto selfMetadata = _swift_getClassOfAllocated(self);

// If it's Hashable, use that
Expand Down Expand Up @@ -423,6 +428,11 @@ - (BOOL)isEqual:(id)other {
if (self == other) {
return YES;
}
if (runtime::bincompat::useLegacySwiftObjCHashing()) {
// Legacy behavior: Don't proxy to Swift Hashable or Equatable
return NO; // We know the ids are different
}


// Get Swift type for self and other
auto selfMetadata = _swift_getClassOfAllocated(self);
Expand Down
11 changes: 11 additions & 0 deletions stdlib/public/runtime/SwiftValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "SwiftObject.h"
#include "SwiftValue.h"
#include "swift/Basic/Lazy.h"
#include "swift/Runtime/Bincompat.h"
#include "swift/Runtime/Casting.h"
#include "swift/Runtime/HeapObject.h"
#include "swift/Runtime/Metadata.h"
Expand Down Expand Up @@ -429,6 +430,11 @@ - (BOOL)isEqual:(id)other {
}
}

if (runtime::bincompat::useLegacySwiftObjCHashing()) {
// Legacy behavior only proxies isEqual: for Hashable, not Equatable
return NO;
}

if (auto equatableConformance = selfHeader->getEquatableConformance()) {
if (auto selfEquatableBaseType = selfHeader->getEquatableBaseType()) {
auto otherEquatableBaseType = otherHeader->getEquatableBaseType();
Expand Down Expand Up @@ -458,6 +464,11 @@ - (NSUInteger)hash {
selfHeader->type, hashableConformance);
}

if (!runtime::bincompat::useLegacySwiftObjCHashing()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Darn. That ! should not have been there... Follow-up PR will fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ouch. Lots of trouble from a line and a dot.

// Legacy behavior doesn't honor Equatable conformance, only Hashable
return (NSUInteger)self;
}

// If Swift type is Equatable but not Hashable,
// we have to return something here that is compatible
// with the `isEqual:` above.
Expand Down
5 changes: 3 additions & 2 deletions test/stdlib/Inputs/SwiftValueNSObject/SwiftValueNSObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ void TestSwiftValueNSObjectNotEquals(id e1, id e2)

void TestSwiftValueNSObjectHashValue(id e, NSUInteger hashValue)
{
printf("NSObjectProtocol.hash: Expect [%s hashValue] == %lu\n",
printf("NSObjectProtocol.hash: Expect [%s hashValue] == %lu, Got %lu\n",
[[e description] UTF8String],
(unsigned long)hashValue);
(unsigned long)hashValue,
[e hash]);
expectTrue([e hash] == hashValue);
}

Expand Down
14 changes: 7 additions & 7 deletions test/stdlib/SwiftValueNSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ func TestHashable<T: Hashable>(_ h: T)
// Test Obj-C hashValue for Swift types that are Equatable but not Hashable
func TestEquatableHash<T: Equatable>(_ e: T)
{
// These should have a constant hash value
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
// Legacy behavior used the pointer value, which is
// incompatible with user-defined equality.
TestSwiftValueNSObjectDefaultHashValue(e as AnyObject)
#else
// New behavior uses a constant hash value in this case
TestSwiftValueNSObjectHashValue(e as AnyObject, 1)
#endif
}

func TestNonEquatableHash<T>(_ e: T)
Expand All @@ -113,10 +119,6 @@ func TestNonEquatableHash<T>(_ e: T)
// CHECK-NEXT: d ##This is D's description##
// CHECK-NEXT: S ##{{.*}}__SwiftValue##

// Full message is longer, but this is the essential part...
// CHECK-NEXT: Obj-C `-hash` {{.*}} type `SwiftValueNSObject.E` {{.*}} Equatable but not Hashable
// CHECK-NEXT: Obj-C `-hash` {{.*}} type `SwiftValueNSObject.E1` {{.*}} Equatable but not Hashable

// Temporarily disable this test on older OSes until we have time to
// look into why it's failing there. rdar://problem/47870743
if #available(OSX 10.12, iOS 10.0, *) {
Expand Down Expand Up @@ -156,6 +158,4 @@ if #available(OSX 10.12, iOS 10.0, *) {
fputs("c ##This is C's debug description##\n", stderr)
fputs("d ##This is D's description##\n", stderr)
fputs("S ##__SwiftValue##\n", stderr)
fputs("Obj-C `-hash` ... type `SwiftValueNSObject.E` ... Equatable but not Hashable", stderr)
fputs("Obj-C `-hash` ... type `SwiftValueNSObject.E1` ... Equatable but not Hashable", stderr)
}