Skip to content

Sema: Gut synthesizeMemberForLookup() #16054

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

Closed
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
81 changes: 26 additions & 55 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5185,25 +5185,25 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (decl->isInvalid() || decl->getOverriddenDecl())
return false;

// Ignore accessor methods (e.g. getters and setters), they will be handled
// when their storage decl is processed.
if (isa<AccessorDecl>(decl))
return false;

auto *dc = decl->getDeclContext();

auto owningTy = dc->getDeclaredInterfaceType();
if (!owningTy)
return false;

auto classDecl = owningTy->getClassOrBoundGenericClass();
auto classDecl = dc->getAsClassOrClassExtensionContext();
if (!classDecl)
return false;

Type superclass = classDecl->getSuperclass();
if (!superclass)
return false;

// Ignore accessor methods (e.g. getters and setters), they will be handled
// when their storage decl is processed.
if (isa<AccessorDecl>(decl))
return false;

auto method = dyn_cast<AbstractFunctionDecl>(decl);
ConstructorDecl *ctor = nullptr;
if (method)
Expand Down Expand Up @@ -5279,12 +5279,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
// visible via dynamic dispatch.
lookupOptions -= NameLookupFlags::DynamicLookup;

// Class methods cannot override declarations only
// visible as protocol requirements or protocol
// extension members.
lookupOptions -= NameLookupFlags::ProtocolMembers;
lookupOptions -= NameLookupFlags::PerformConformanceCheck;

members = TC.lookupMember(dc, superclass,
name, lookupOptions);
}
Expand All @@ -5298,7 +5292,10 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
if (member->getKind() != decl->getKind())
continue;

if (!dc->getAsClassOrClassExtensionContext())
// Class methods cannot override declarations only
// visible as protocol requirements or protocol
// extension members.
if (!member->getDeclContext()->getAsClassOrClassExtensionContext())
continue;

auto parentDecl = cast<ValueDecl>(member);
Expand Down Expand Up @@ -8990,6 +8987,8 @@ void TypeChecker::addImplicitConstructors(NominalTypeDecl *decl) {
void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
DeclName member) {
auto baseName = member.getBaseName();
if (baseName != Context.Id_CodingKeys)
return;

// Checks whether the target conforms to the given protocol. If the
// conformance is incomplete, force the conformance.
Expand All @@ -9014,48 +9013,20 @@ void TypeChecker::synthesizeMemberForLookup(NominalTypeDecl *target,
return false;
};

if (member.isSimpleName() && !baseName.isSpecial()) {
if (baseName.getIdentifier() == Context.Id_CodingKeys) {
// CodingKeys is a special type which may be synthesized as part of
// Encodable/Decodable conformance. If the target conforms to either
// protocol and would derive conformance to either, the type may be
// synthesized.
// If the target conforms to either and the conformance has not yet been
// evaluated, then we should do that here.
//
// Try to synthesize Decodable first. If that fails, try to synthesize
// Encodable. If either succeeds and CodingKeys should have been
// synthesized, it will be synthesized.
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
if (!evaluateTargetConformanceTo(decodableProto))
(void)evaluateTargetConformanceTo(encodableProto);
}
} else {
auto argumentNames = member.getArgumentNames();
if (argumentNames.size() != 1)
return;

auto argumentName = argumentNames.front();
if (baseName == DeclBaseName::createConstructor() &&
argumentName == Context.Id_from) {
// init(from:) may be synthesized as part of derived conformance to the
// Decodable protocol.
// If the target should conform to the Decodable protocol, check the
// conformance here to attempt synthesis.
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
(void)evaluateTargetConformanceTo(decodableProto);
} else if (!baseName.isSpecial() &&
baseName.getIdentifier() == Context.Id_encode &&
argumentName == Context.Id_to) {
// encode(to:) may be synthesized as part of derived conformance to the
// Encodable protocol.
// If the target should conform to the Encodable protocol, check the
// conformance here to attempt synthesis.
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
(void)evaluateTargetConformanceTo(encodableProto);
}
}
// CodingKeys is a special type which may be synthesized as part of
// Encodable/Decodable conformance. If the target conforms to either
// protocol and would derive conformance to either, the type may be
// synthesized.
// If the target conforms to either and the conformance has not yet been
// evaluated, then we should do that here.
//
// Try to synthesize Decodable first. If that fails, try to synthesize
// Encodable. If either succeeds and CodingKeys should have been
// synthesized, it will be synthesized.
auto *decodableProto = Context.getProtocol(KnownProtocolKind::Decodable);
auto *encodableProto = Context.getProtocol(KnownProtocolKind::Encodable);
if (!evaluateTargetConformanceTo(decodableProto))
(void)evaluateTargetConformanceTo(encodableProto);
}

void TypeChecker::defineDefaultConstructor(NominalTypeDecl *decl) {
Expand Down
12 changes: 12 additions & 0 deletions test/decl/class/override.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,15 @@ class DerivedWithFilePrivateSetter: BaseWithFilePrivateSetter {
set { }
}
}

protocol HasExtension {}

extension HasExtension {
func extensionMethod() {}
}

class BaseExtended : HasExtension {}

class DerivedExtended : BaseExtended {
func extensionMethod() {}
}
Loading