Skip to content
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

[4.2][SE-0206][stdlib] Implement Hashable Enhancements proposal #16173

Merged
merged 19 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6c890e7
[test] MinimalTypes: Move ==, < definitions into the corresponding ty…
lorentey Apr 20, 2018
cba682d
[test] MinimalHashable{Value,Class}: Implement CustomStringConvertible
lorentey Apr 20, 2018
7bffb5c
[test] checkHashable: Check that unequal values produce different hashes
lorentey Apr 16, 2018
b792324
[test] MinimalHashable types: Replace hashValue hook with hash(into:)
lorentey Apr 16, 2018
57ba7b6
[test] Override hash, not hashValue in NSObject subclasses
lorentey Apr 16, 2018
d652124
[test] PersistentVector: use a custom hashing interface
lorentey Apr 17, 2018
3ed610d
[SE-0206][stdlib] De-underscore Hasher
lorentey Apr 20, 2018
5a8f94e
[SE-0206][stdlib] Document Hasher
lorentey Apr 20, 2018
b22e0de
[SE-0206][stdlib] Make Hasher.finalize() nonmutating but __consuming
lorentey Apr 21, 2018
954a14e
[lit] Add %target-run-stdlib-swiftgyb
lorentey Apr 21, 2018
3570d33
[SE-0206][stdlib] Hasher, SipHash: Make non-public declarations internal
lorentey Apr 21, 2018
c8455b1
[SE-0206][stdlib] Clarify Hasher.finalize() documentation
lorentey Apr 22, 2018
b8f9aa1
[SE-0206][stdlib] De-underscore Hashable.hash(into:)
lorentey Apr 22, 2018
c15574b
[SE-0206][Sema] Update Hashable synthesizer to support hash(into:)
lorentey Mar 1, 2018
3178d74
[SE-0206][CoreFoundation] _CFObject: Provide default implementation f…
lorentey Mar 2, 2018
317fb54
[SE-0206][test] Fix tests that look at Hashable requirements
lorentey Apr 22, 2018
42e7f15
[SE-0206][Sema] Add separate hash(into:) impl for enums with no assoc…
lorentey Apr 22, 2018
5fcdb7a
[SE-0206][test] Update diagnostic tests for Hashable derivation
lorentey Apr 23, 2018
501de1c
[stdlib] Remove obsolete hashing interfaces
lorentey Apr 24, 2018
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
8 changes: 2 additions & 6 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,12 +471,8 @@ class ASTContext {
/// Retrieve the declaration of Swift.==(Int, Int) -> Bool.
FuncDecl *getEqualIntDecl() const;

/// Retrieve the declaration of
/// Swift._combineHashValues(Int, Int) -> Int.
FuncDecl *getCombineHashValuesDecl() const;

/// Retrieve the declaration of Swift._mixInt(Int) -> Int.
FuncDecl *getMixIntDecl() const;
/// Retrieve the declaration of Swift._hashValue<H>(for: H) -> Int.
FuncDecl *getHashValueForDecl() const;

/// Retrieve the declaration of Array.append(element:)
FuncDecl *getArrayAppendElementDecl() const;
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,8 @@ ERROR(broken_equatable_requirement,none,
"Equatable protocol is broken: unexpected requirement", ())
ERROR(broken_hashable_requirement,none,
"Hashable protocol is broken: unexpected requirement", ())
ERROR(broken_hashable_no_hasher,none,
"Hashable protocol is broken: Hasher type not found", ())
ERROR(broken_errortype_requirement,none,
"Error protocol is broken: unexpected requirement", ())
ERROR(broken_int_hashable_conformance,none,
Expand Down
5 changes: 5 additions & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IDENTIFIER(atIndexedSubscript)
IDENTIFIER_(bridgeToObjectiveC)
IDENTIFIER_WITH_NAME(code_, "_code")
IDENTIFIER(CodingKeys)
IDENTIFIER(combine)
IDENTIFIER(container)
IDENTIFIER(CoreGraphics)
IDENTIFIER(CoreMedia)
Expand All @@ -57,13 +58,17 @@ IDENTIFIER(error)
IDENTIFIER(errorDomain)
IDENTIFIER(forKeyedSubscript)
IDENTIFIER(Foundation)
IDENTIFIER(for)
IDENTIFIER(forKey)
IDENTIFIER(from)
IDENTIFIER(fromRaw)
IDENTIFIER(hash)
IDENTIFIER(hasher)
IDENTIFIER(hashValue)
IDENTIFIER(initialize)
IDENTIFIER(initStorage)
IDENTIFIER(initialValue)
IDENTIFIER(into)
IDENTIFIER(intValue)
IDENTIFIER(Key)
IDENTIFIER(KeyedDecodingContainer)
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownStdlibTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ KNOWN_STDLIB_TYPE_DECL(Sequence, NominalTypeDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Dictionary, NominalTypeDecl, 2)
KNOWN_STDLIB_TYPE_DECL(AnyHashable, NominalTypeDecl, 0)
KNOWN_STDLIB_TYPE_DECL(MutableCollection, ProtocolDecl, 1)
KNOWN_STDLIB_TYPE_DECL(Hasher, NominalTypeDecl, 0)

KNOWN_STDLIB_TYPE_DECL(AnyKeyPath, NominalTypeDecl, 0)
KNOWN_STDLIB_TYPE_DECL(PartialKeyPath, NominalTypeDecl, 1)
Expand Down
66 changes: 24 additions & 42 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,9 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
/// func ==(Int, Int) -> Bool
FuncDecl *EqualIntDecl = nullptr;

/// func _combineHashValues(Int, Int) -> Int
FuncDecl *CombineHashValuesDecl = nullptr;
/// func _hashValue<H: Hashable>(for: H) -> Int
FuncDecl *HashValueForDecl = nullptr;

/// func _mixInt(Int) -> Int
FuncDecl *MixIntDecl = nullptr;

/// func append(Element) -> void
FuncDecl *ArrayAppendElementDecl = nullptr;

Expand Down Expand Up @@ -989,44 +986,29 @@ FuncDecl *ASTContext::getGetBoolDecl(LazyResolver *resolver) const {
return decl;
}

FuncDecl *ASTContext::getCombineHashValuesDecl() const {
if (Impl.CombineHashValuesDecl)
return Impl.CombineHashValuesDecl;

auto resolver = getLazyResolver();
auto intType = getIntDecl()->getDeclaredType();

auto callback = [&](Type inputType, Type resultType) {
// Look for the signature (Int, Int) -> Int
auto tupleType = dyn_cast<TupleType>(inputType.getPointer());
assert(tupleType);
return tupleType->getNumElements() == 2 &&
tupleType->getElementType(0)->isEqual(intType) &&
tupleType->getElementType(1)->isEqual(intType) &&
resultType->isEqual(intType);
};

auto decl = lookupLibraryIntrinsicFunc(
*this, "_combineHashValues", resolver, callback);
Impl.CombineHashValuesDecl = decl;
return decl;
}

FuncDecl *ASTContext::getMixIntDecl() const {
if (Impl.MixIntDecl)
return Impl.MixIntDecl;
FuncDecl *ASTContext::getHashValueForDecl() const {
if (Impl.HashValueForDecl)
return Impl.HashValueForDecl;

auto resolver = getLazyResolver();
auto intType = getIntDecl()->getDeclaredType();

auto callback = [&](Type inputType, Type resultType) {
// Look for the signature (Int) -> Int
return inputType->isEqual(intType) && resultType->isEqual(intType);
};

auto decl = lookupLibraryIntrinsicFunc(*this, "_mixInt", resolver, callback);
Impl.MixIntDecl = decl;
return decl;
SmallVector<ValueDecl *, 1> results;
lookupInSwiftModule("_hashValue", results);
for (auto result : results) {
auto *fd = dyn_cast<FuncDecl>(result);
if (!fd)
continue;
auto paramLists = fd->getParameterLists();
if (paramLists.size() != 1 || paramLists[0]->size() != 1)
continue;
auto paramDecl = paramLists[0]->get(0);
if (paramDecl->getArgumentName() != Id_for)
continue;
auto genericParams = fd->getGenericParams();
if (!genericParams || genericParams->size() != 1)
continue;
Impl.HashValueForDecl = fd;
return fd;
}
return nullptr;
}

FuncDecl *ASTContext::getArrayAppendElementDecl() const {
Expand Down
2 changes: 1 addition & 1 deletion lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3640,7 +3640,7 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,

SILValue hashCode;

// TODO: Combine hashes of the indexes using an inout _Hasher
// TODO: Combine hashes of the indexes using an inout Hasher
{
auto &index = indexes[0];

Expand Down
Loading