Skip to content

Sema: Fix requirement/witness disambiguation for subclass existentials [4.0] #11242

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

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
36 changes: 31 additions & 5 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ProtocolConformance.h"
Expand Down Expand Up @@ -120,27 +121,48 @@ namespace {
if (!Options.contains(NameLookupFlags::ProtocolMembers) ||
!isa<ProtocolDecl>(foundDC) ||
isa<GenericTypeParamDecl>(found) ||
(isa<FuncDecl>(found) && cast<FuncDecl>(found)->isOperator()) ||
foundInType->isAnyExistentialType()) {
(isa<FuncDecl>(found) && cast<FuncDecl>(found)->isOperator())) {
addResult(found);
return;
}

assert(isa<ProtocolDecl>(foundDC));

auto conformingType = foundInType;

// When performing a lookup on a subclass existential, we might
// find a member of the class that witnesses a requirement on a
// protocol that the class conforms to.
//
// Since subclass existentials don't normally conform to protocols,
// pull out the superclass instead, and use that below.
if (foundInType->isExistentialType()) {
auto layout = foundInType->getExistentialLayout();
if (layout.superclass)
conformingType = layout.superclass;
}

// If we found something within the protocol itself, and our
// search began somewhere that is not in a protocol or extension
// thereof, remap this declaration to the witness.
if (foundInType->is<ArchetypeType>() ||
foundInType->isExistentialType() ||
Options.contains(NameLookupFlags::PerformConformanceCheck)) {
// Dig out the protocol conformance.
auto conformance = TC.conformsToProtocol(foundInType, foundProto, DC,
auto conformance = TC.conformsToProtocol(conformingType, foundProto, DC,
conformanceOptions);
if (!conformance)
if (!conformance) {
// If there's no conformance, we have an existential
// and we found a member from one of the protocols, and
// not a class constraint if any.
assert(foundInType->isExistentialType());
addResult(found);
return;
}

if (conformance->isAbstract()) {
assert(foundInType->is<ArchetypeType>());
assert(foundInType->is<ArchetypeType>() ||
foundInType->isExistentialType());
addResult(found);
return;
}
Expand All @@ -161,6 +183,10 @@ namespace {

// FIXME: the "isa<ProtocolDecl>()" check will be wrong for
// default implementations in protocols.
//
// If we have an imported conformance or the witness could
// not be deserialized, getWitnessDecl() will just return
// the requirement, so just drop the lookup result here.
if (witness && !isa<ProtocolDecl>(witness->getDeclContext()))
addResult(witness);

Expand Down
4 changes: 4 additions & 0 deletions test/ClangImporter/subclass_existentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ class OldSwiftLaundryService : NSLaundry {
return g!
}
}

// Make sure the method lookup is not ambiguous

_ = Coat.fashionStatement.wear()
10 changes: 9 additions & 1 deletion test/Inputs/clang-importer-sdk/usr/include/Foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,13 +1086,21 @@ typedef enum __attribute__((ns_error_domain(FictionalServerErrorDomain))) Fictio
FictionalServerErrorMeltedDown = 1
} FictionalServerErrorCode;

@protocol Wearable
- (void)wear;
@end

@protocol Garment
@end

@protocol Cotton
@end

@interface Coat
@interface Coat : NSObject<Wearable>

- (void)wear;
@property (class) Coat <Wearable> *fashionStatement;

@end

@protocol NSLaundry
Expand Down