Skip to content

Commit d8b4d99

Browse files
committed
Sema: synthesizeMemberForLookup() doesn't have to handle Codable requirements
Since init(from:) and encode(to:) are protocol requirements, any name lookup of them will find the protocol requirement, and if the witness is not present we will synthesize it on the spot. Only CodingKeys requires special handling here, since it is not a protocol requirement, but has to get synthesized anyway.
1 parent 0165e3e commit d8b4d99

File tree

5 files changed

+80
-168
lines changed

5 files changed

+80
-168
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8987,6 +8987,8 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
89878987
void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
89888988
DeclName member) {
89898989
auto baseName = member.getBaseName();
8990+
if (baseName != Context.Id_CodingKeys)
8991+
return;
89908992

89918993
// Checks whether the target conforms to the given protocol. If the
89928994
// conformance is incomplete, force the conformance.
@@ -9011,48 +9013,20 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
90119013
return false;
90129014
};
90139015

9014-
if (member.isSimpleName() && !baseName.isSpecial()) {
9015-
if (baseName.getIdentifier() == Context.Id_CodingKeys) {
9016-
// CodingKeys is a special type which may be synthesized as part of
9017-
// Encodable/Decodable conformance. If the target conforms to either
9018-
// protocol and would derive conformance to either, the type may be
9019-
// synthesized.
9020-
// If the target conforms to either and the conformance has not yet been
9021-
// evaluated, then we should do that here.
9022-
//
9023-
// Try to synthesize Decodable first. If that fails, try to synthesize
9024-
// Encodable. If either succeeds and CodingKeys should have been
9025-
// synthesized, it will be synthesized.
9026-
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
9027-
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
9028-
if (!evaluateTargetConformanceTo(decodableProto))
9029-
(void)evaluateTargetConformanceTo(encodableProto);
9030-
}
9031-
} else {
9032-
auto argumentNames = member.getArgumentNames();
9033-
if (argumentNames.size() != 1)
9034-
return;
9035-
9036-
auto argumentName = argumentNames.front();
9037-
if (baseName == DeclBaseName::createConstructor() &&
9038-
argumentName == Context.Id_from) {
9039-
// init(from:) may be synthesized as part of derived conformance to the
9040-
// Decodable protocol.
9041-
// If the target should conform to the Decodable protocol, check the
9042-
// conformance here to attempt synthesis.
9043-
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
9044-
(void)evaluateTargetConformanceTo(decodableProto);
9045-
} else if (!baseName.isSpecial() &&
9046-
baseName.getIdentifier() == Context.Id_encode &&
9047-
argumentName == Context.Id_to) {
9048-
// encode(to:) may be synthesized as part of derived conformance to the
9049-
// Encodable protocol.
9050-
// If the target should conform to the Encodable protocol, check the
9051-
// conformance here to attempt synthesis.
9052-
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
9053-
(void)evaluateTargetConformanceTo(encodableProto);
9054-
}
9055-
}
9016+
// CodingKeys is a special type which may be synthesized as part of
9017+
// Encodable/Decodable conformance. If the target conforms to either
9018+
// protocol and would derive conformance to either, the type may be
9019+
// synthesized.
9020+
// If the target conforms to either and the conformance has not yet been
9021+
// evaluated, then we should do that here.
9022+
//
9023+
// Try to synthesize Decodable first. If that fails, try to synthesize
9024+
// Encodable. If either succeeds and CodingKeys should have been
9025+
// synthesized, it will be synthesized.
9026+
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
9027+
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
9028+
if (!evaluateTargetConformanceTo(decodableProto))
9029+
(void)evaluateTargetConformanceTo(encodableProto);
90569030
}
90579031

90589032
void TypeChecker::defineDefaultConstructor(NominalTypeDecl *decl) {

0 commit comments

Comments
 (0)