Skip to content

Commit 712927c

Browse files
committed
Sema: Go back to synthesizing hashValue on _StoredBridgedNSError conformers
Commit e0bba70 added a default implementation, however this is wrong for non-imported types. Instead, synthesize the body as before. Since this is one of the few derived methods that can appear on an imported type, make sure to build fully type-checked AST. Fixes <rdar://problem/51322302>.
1 parent 61e7a9a commit 712927c

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,18 +1055,41 @@ deriveBodyHashable_hashValue(AbstractFunctionDecl *hashValueDecl, void *) {
10551055

10561056
// return _hashValue(for: self)
10571057
auto *hashFunc = C.getHashValueForDecl();
1058-
auto hashExpr = new (C) DeclRefExpr(hashFunc, DeclNameLoc(),
1058+
if (!hashFunc->hasInterfaceType())
1059+
C.getLazyResolver()->resolveDeclSignature(hashFunc);
1060+
1061+
auto selfType = hashValueDecl->mapTypeIntoContext(
1062+
parentDC->getSelfInterfaceType());
1063+
auto hashableProto = C.getProtocol(KnownProtocolKind::Hashable);
1064+
auto conformance = TypeChecker::conformsToProtocol(selfType, hashableProto,
1065+
parentDC, None);
1066+
auto subs = SubstitutionMap::get(hashFunc->getGenericSignature(),
1067+
ArrayRef<Type>(selfType),
1068+
ArrayRef<ProtocolConformanceRef>(*conformance));
1069+
ConcreteDeclRef hashRef(hashFunc, subs);
1070+
1071+
auto hashExpr = new (C) DeclRefExpr(hashRef, DeclNameLoc(),
10591072
/*implicit*/ true);
1073+
1074+
Type intType = C.getIntDecl()->getDeclaredType();
1075+
hashExpr->setType(FunctionType::get(AnyFunctionType::Param(selfType),
1076+
intType));
1077+
10601078
auto selfDecl = hashValueDecl->getImplicitSelfDecl();
10611079
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),
10621080
/*implicit*/ true);
1063-
auto callExpr = CallExpr::createImplicit(C, hashExpr,
1064-
{ selfRef }, { C.Id_for });
1081+
selfRef->setType(selfType);
1082+
1083+
auto callExpr = CallExpr::createImplicit(C, hashExpr, { selfRef }, { });
1084+
callExpr->setType(intType);
1085+
callExpr->setThrows(false);
1086+
10651087
auto returnStmt = new (C) ReturnStmt(SourceLoc(), callExpr);
10661088

10671089
auto body = BraceStmt::create(C, SourceLoc(), {returnStmt}, SourceLoc(),
10681090
/*implicit*/ true);
10691091
hashValueDecl->setBody(body);
1092+
hashValueDecl->setBodyTypeCheckedIfPresent();
10701093
}
10711094

10721095
/// Derive a 'hashValue' implementation.

stdlib/public/Darwin/Foundation/NSError.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,10 +489,6 @@ extension _BridgedStoredNSError {
489489
public func hash(into hasher: inout Hasher) {
490490
hasher.combine(_nsError)
491491
}
492-
493-
@_alwaysEmitIntoClient public var hashValue: Int {
494-
return _nsError.hashValue
495-
}
496492
}
497493

498494
/// Describes the code of an error.

0 commit comments

Comments
 (0)