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

[SE-0206][stdlib] Implement Hashable Enhancements proposal #16073

Merged
merged 13 commits into from
Apr 24, 2018
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
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