Skip to content

Funnel all "get local conformances" queries through a request. #36223

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
1 change: 1 addition & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3004,6 +3004,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
friend class DeclContext;
friend class IterableDeclContext;
friend class DirectLookupRequest;
friend class LookupAllConformancesInContextRequest;
friend ArrayRef<ValueDecl *>
ValueDecl::getSatisfiedProtocolRequirements(bool Sorted) const;

Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ class IterableDeclContext {

static IterableDeclContext *castDeclToIterableDeclContext(const Decl *D);

friend class LookupAllConformancesInContextRequest;

/// Retrieve the \c ASTContext in which this iterable context occurs.
ASTContext &getASTContext() const;

Expand Down
34 changes: 1 addition & 33 deletions lib/AST/ConformanceLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,7 @@ bool ConformanceLookupTable::lookupConformance(
void ConformanceLookupTable::lookupConformances(
NominalTypeDecl *nominal,
DeclContext *dc,
ConformanceLookupKind lookupKind,
SmallVectorImpl<ProtocolDecl *> *protocols,
SmallVectorImpl<ProtocolConformance *> *conformances,
std::vector<ProtocolConformance *> *conformances,
SmallVectorImpl<ConformanceDiagnostic> *diagnostics) {
// We need to expand all implied conformances before we can find
// those conformances that pertain to this declaration context.
Expand All @@ -980,36 +978,6 @@ void ConformanceLookupTable::lookupConformances(
if (entry->isSuperseded())
return true;

// If we are to filter out this result, do so now.
switch (lookupKind) {
case ConformanceLookupKind::OnlyExplicit:
switch (entry->getKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Synthesized:
break;
case ConformanceEntryKind::Implied:
case ConformanceEntryKind::Inherited:
return false;
}
break;
case ConformanceLookupKind::NonInherited:
switch (entry->getKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::Implied:
break;
case ConformanceEntryKind::Inherited:
return false;
}
break;
case ConformanceLookupKind::All:
break;
}

// Record the protocol.
if (protocols)
protocols->push_back(entry->getProtocol());

// Record the conformance.
if (conformances) {
if (auto conformance = getConformance(nominal, entry))
Expand Down
4 changes: 1 addition & 3 deletions lib/AST/ConformanceLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,7 @@ class ConformanceLookupTable {
/// Look for all of the conformances within the given declaration context.
void lookupConformances(NominalTypeDecl *nominal,
DeclContext *dc,
ConformanceLookupKind lookupKind,
SmallVectorImpl<ProtocolDecl *> *protocols,
SmallVectorImpl<ProtocolConformance *> *conformances,
std::vector<ProtocolConformance *> *conformances,
SmallVectorImpl<ConformanceDiagnostic> *diagnostics);

/// Retrieve the complete set of protocols to which this nominal
Expand Down
92 changes: 57 additions & 35 deletions lib/AST/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1321,59 +1321,83 @@ NominalTypeDecl::getSatisfiedProtocolRequirementsForMember(
SmallVector<ProtocolDecl *, 2>
IterableDeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
SmallVector<ProtocolDecl *, 2> result;

// Dig out the nominal type.
const auto dc = getAsGenericContext();
const auto nominal = dc->getSelfNominalTypeDecl();
if (!nominal) {
return result;
}

// Update to record all potential conformances.
nominal->prepareConformanceTable();
nominal->ConformanceTable->lookupConformances(
nominal,
const_cast<GenericContext *>(dc),
lookupKind,
&result,
nullptr,
nullptr);

for (auto conformance : getLocalConformances(lookupKind))
result.push_back(conformance->getProtocol());
return result;
}

SmallVector<ProtocolConformance *, 2>
IterableDeclContext::getLocalConformances(ConformanceLookupKind lookupKind)
const {
SmallVector<ProtocolConformance *, 2> result;

std::vector<ProtocolConformance *>
LookupAllConformancesInContextRequest::evaluate(
Evaluator &eval, const IterableDeclContext *IDC) const {
// Dig out the nominal type.
const auto dc = getAsGenericContext();
const auto dc = IDC->getAsGenericContext();
const auto nominal = dc->getSelfNominalTypeDecl();
if (!nominal) {
return result;
return { };
}

// Protocols only have self-conformances.
if (auto protocol = dyn_cast<ProtocolDecl>(nominal)) {
if (protocol->requiresSelfConformanceWitnessTable()) {
return SmallVector<ProtocolConformance *, 2>{
protocol->getASTContext().getSelfConformance(protocol)
};
return { protocol->getASTContext().getSelfConformance(protocol) };
}
return SmallVector<ProtocolConformance *, 2>();

return { };
}

// Update to record all potential conformances.
// Record all potential conformances.
nominal->prepareConformanceTable();
std::vector<ProtocolConformance *> conformances;
nominal->ConformanceTable->lookupConformances(
nominal,
const_cast<GenericContext *>(dc),
lookupKind,
nullptr,
&result,
&conformances,
nullptr);

return conformances;
}

SmallVector<ProtocolConformance *, 2>
IterableDeclContext::getLocalConformances(ConformanceLookupKind lookupKind)
const {
// Look up the cached set of all of the conformances.
std::vector<ProtocolConformance *> conformances =
evaluateOrDefault(
getASTContext().evaluator, LookupAllConformancesInContextRequest{this},
{ });

// Copy all of the conformances we want.
SmallVector<ProtocolConformance *, 2> result;
std::copy_if(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could bypass the copy_if() if the lookupKind is All, which is the default here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a silly SmallVector-vs-vector difference to deal with as well, but yes, thanks!

conformances.begin(), conformances.end(), std::back_inserter(result),
[&](ProtocolConformance *conformance) {
// If we are to filter out this result, do so now.
switch (lookupKind) {
case ConformanceLookupKind::OnlyExplicit:
switch (conformance->getSourceKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Synthesized:
return true;
case ConformanceEntryKind::Implied:
case ConformanceEntryKind::Inherited:
return false;
}

case ConformanceLookupKind::NonInherited:
switch (conformance->getSourceKind()) {
case ConformanceEntryKind::Explicit:
case ConformanceEntryKind::Synthesized:
case ConformanceEntryKind::Implied:
return true;
case ConformanceEntryKind::Inherited:
return false;
}

case ConformanceLookupKind::All:
return true;
}
});

return result;
}

Expand All @@ -1399,8 +1423,6 @@ IterableDeclContext::takeConformanceDiagnostics() const {
nominal->ConformanceTable->lookupConformances(
nominal,
const_cast<GenericContext *>(dc),
ConformanceLookupKind::All,
nullptr,
nullptr,
&result);

Expand Down
8 changes: 2 additions & 6 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ bool IsAsyncHandlerRequest::evaluate(
// implies @asyncHandler.
{
auto idc = cast<IterableDeclContext>(dc->getAsDecl());
auto conformances = evaluateOrDefault(
dc->getASTContext().evaluator,
LookupAllConformancesInContextRequest{idc}, { });
auto conformances = idc->getLocalConformances();

for (auto conformance : conformances) {
auto protocol = conformance->getProtocol();
Expand Down Expand Up @@ -2050,9 +2048,7 @@ static Optional<ActorIsolation> getIsolationFromWitnessedRequirements(

// Walk through each of the conformances in this context, collecting any
// requirements that have actor isolation.
auto conformances = evaluateOrDefault(
dc->getASTContext().evaluator,
LookupAllConformancesInContextRequest{idc}, { });
auto conformances = idc->getLocalConformances();
using IsolatedRequirement =
std::tuple<ProtocolConformance *, ActorIsolation, ValueDecl *>;
SmallVector<IsolatedRequirement, 2> isolatedRequirements;
Expand Down
11 changes: 1 addition & 10 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5650,13 +5650,6 @@ diagnoseMissingAppendInterpolationMethod(NominalTypeDecl *typeDecl) {
}
}

std::vector<ProtocolConformance *>
LookupAllConformancesInContextRequest::evaluate(
Evaluator &eval, const IterableDeclContext *IDC) const {
auto result = IDC->getLocalConformances(ConformanceLookupKind::All);
return std::vector<ProtocolConformance *>(result.begin(), result.end());
}

void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
auto *const dc = idc->getAsGenericContext();

Expand All @@ -5672,9 +5665,7 @@ void TypeChecker::checkConformancesInContext(IterableDeclContext *idc) {
const auto defaultAccess = nominal->getFormalAccess();

// Check each of the conformances associated with this context.
auto conformances =
evaluateOrDefault(dc->getASTContext().evaluator,
LookupAllConformancesInContextRequest{idc}, {});
auto conformances = idc->getLocalConformances();

// The conformance checker bundle that checks all conformances in the context.
auto &Context = dc->getASTContext();
Expand Down
4 changes: 1 addition & 3 deletions lib/Sema/TypeCheckRequestFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,7 @@ static Type inferResultBuilderType(ValueDecl *decl) {
auto addConformanceMatches = [&matches](ValueDecl *lookupDecl) {
DeclContext *dc = lookupDecl->getDeclContext();
auto idc = cast<IterableDeclContext>(dc->getAsDecl());
auto conformances = evaluateOrDefault(
dc->getASTContext().evaluator,
LookupAllConformancesInContextRequest{idc}, { });
auto conformances = idc->getLocalConformances();

for (auto conformance : conformances) {
auto protocol = conformance->getProtocol();
Expand Down