Skip to content

One last name lookup cleanup #11243

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
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
5 changes: 0 additions & 5 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3092,11 +3092,6 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
if (includeInaccessibleMembers)
lookupOptions |= NameLookupFlags::IgnoreAccessibility;

// If a constructor is only visible as a witness for a protocol
// requirement, it must be an invalid override. Also, protocol
// extensions cannot yet define designated initializers.
lookupOptions -= NameLookupFlags::PerformConformanceCheck;

LookupResult ctors = TC.lookupConstructors(DC, instanceTy, lookupOptions);
if (!ctors)
return result; // No result.
Expand Down
81 changes: 39 additions & 42 deletions lib/Sema/TypeCheckNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ namespace {

assert(isa<ProtocolDecl>(foundDC));

if (!Options.contains(NameLookupFlags::PerformConformanceCheck))
return;

// 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.
auto conformingType = foundInType;

// When performing a lookup on a subclass existential, we might
Expand All @@ -143,52 +149,43 @@ namespace {
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(conformingType, foundProto, DC,
// Dig out the protocol conformance.
auto conformance = TC.conformsToProtocol(conformingType, foundProto, DC,
conformanceOptions);
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>() ||
foundInType->isExistentialType());
addResult(found);
return;
}

// Dig out the witness.
ValueDecl *witness = nullptr;
auto concrete = conformance->getConcrete();
if (auto assocType = dyn_cast<AssociatedTypeDecl>(found)) {
witness = concrete->getTypeWitnessAndDecl(assocType, &TC)
.second;
} else if (found->isProtocolRequirement()) {
witness = concrete->getWitnessDecl(found, &TC);
}

// 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);
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>() ||
foundInType->isExistentialType());
addResult(found);
return;
}

// Dig out the witness.
ValueDecl *witness = nullptr;
auto concrete = conformance->getConcrete();
if (auto assocType = dyn_cast<AssociatedTypeDecl>(found)) {
witness = concrete->getTypeWitnessAndDecl(assocType, &TC)
.second;
} else if (found->isProtocolRequirement()) {
witness = concrete->getWitnessDecl(found, &TC);
}

// 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);
}
};
} // end anonymous namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class A : P { } // expected-error{{initializer requirement 'init()' can only be
// No further errors

class B : A {
init(x : Int) {} // expected-note {{'init(x:)' declared here}}
init(x : Int) {}
}

class C : B { }

class D : B {
init() {
super.init() // expected-error{{missing argument for parameter 'x' in call}}
super.init()
}
}