Skip to content

Make SwiftValue hash/equality work the same as SwiftObject #69618

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
Nov 7, 2023
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
42 changes: 36 additions & 6 deletions stdlib/public/runtime/SwiftValue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
#include "swift/Runtime/Metadata.h"
#include "swift/Runtime/ObjCBridge.h"
#include "swift/Runtime/Debug.h"
#include "swift/Threading/Mutex.h"
#include "Private.h"
#include "SwiftEquatableSupport.h"
#include "SwiftHashableSupport.h"
#include <objc/runtime.h>
#include <Foundation/Foundation.h>

#include <new>
#include <unordered_set>

using namespace swift;
using namespace swift::hashable_support;
Expand Down Expand Up @@ -446,15 +448,43 @@ - (BOOL)isEqual:(id)other {
}

- (NSUInteger)hash {
// If Swift type is Hashable, get the hash value from there
auto selfHeader = getSwiftValueHeader(self);
auto hashableConformance = selfHeader->getHashableConformance();
if (!hashableConformance) {
return (NSUInteger)self;
if (hashableConformance) {
return _swift_stdlib_Hashable_hashValue_indirect(
getSwiftValuePayload(self,
getSwiftValuePayloadAlignMask(selfHeader->type)),
selfHeader->type, hashableConformance);
}
return _swift_stdlib_Hashable_hashValue_indirect(
getSwiftValuePayload(self,
getSwiftValuePayloadAlignMask(selfHeader->type)),
selfHeader->type, hashableConformance);

// If Swift type is Equatable but not Hashable,
// we have to return something here that is compatible
// with the `isEqual:` above.
auto equatableConformance = selfHeader->getEquatableConformance();
if (equatableConformance) {
// Warn once per type about this
auto metadata = getSwiftValueTypeMetadata(self);
static Lazy<std::unordered_set<const Metadata *>> warned;
static LazyMutex warnedLock;
LazyMutex::ScopedLock guard(warnedLock);
auto result = warned.get().insert(metadata);
auto inserted = std::get<1>(result);
if (inserted) {
TypeNamePair typeName = swift_getTypeName(metadata, true);
warning(0,
"Obj-C `-hash` invoked on a Swift value of type `%s` that is Equatable but not Hashable; "
"this can lead to severe performance problems.\n",
typeName.data);
}
// Constant value (yuck!) is the only choice here
return (NSUInteger)1;
}

// If the Swift type is neither Equatable nor Hashable,
// then we can hash the identity, which should be pretty
// good in practice.
return (NSUInteger)self;
}

static id getValueDescription(__SwiftValue *self) {
Expand Down
Loading