Skip to content

Commit 9bf51a9

Browse files
committed
IDE: Use GenericSignature::getUpperBound()
1 parent 9820d38 commit 9bf51a9

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

lib/IDE/CompletionLookup.cpp

+20-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include "swift/IDE/CompletionLookup.h"
1414
#include "CodeCompletionResultBuilder.h"
1515
#include "ExprContextAnalysis.h"
16+
#include "swift/AST/GenericEnvironment.h"
17+
#include "swift/AST/GenericSignature.h"
1618
#include "swift/AST/ParameterList.h"
1719
#include "swift/AST/SourceFile.h"
1820

@@ -555,15 +557,10 @@ Type CompletionLookup::eraseArchetypes(Type type, GenericSignature genericSig) {
555557
if (!genericSig)
556558
return type;
557559

558-
auto buildProtocolComposition = [&](ArrayRef<ProtocolDecl *> protos) -> Type {
559-
SmallVector<Type, 2> types;
560-
for (auto proto : protos)
561-
types.push_back(proto->getDeclaredInterfaceType());
562-
return ProtocolCompositionType::get(Ctx, types,
563-
/*HasExplicitAnyObject=*/false);
564-
};
565-
566560
if (auto *genericFuncType = type->getAs<GenericFunctionType>()) {
561+
assert(genericFuncType->getGenericSignature()->isEqual(genericSig) &&
562+
"if not, just use the GFT's signature instead below");
563+
567564
SmallVector<AnyFunctionType::Param, 8> erasedParams;
568565
for (const auto &param : genericFuncType->getParams()) {
569566
auto erasedTy = eraseArchetypes(param.getPlainType(), genericSig);
@@ -584,15 +581,24 @@ Type CompletionLookup::eraseArchetypes(Type type, GenericSignature genericSig) {
584581
archetypeType->isRoot())
585582
return t;
586583

587-
auto protos = archetypeType->getConformsTo();
588-
if (!protos.empty())
589-
return buildProtocolComposition(protos);
584+
auto genericSig = archetypeType->getGenericEnvironment()->getGenericSignature();
585+
auto upperBound = genericSig->getUpperBound(
586+
archetypeType->getInterfaceType(),
587+
/*forExistentialSelf=*/false,
588+
/*withParameterizedProtocols=*/false);
589+
590+
if (!upperBound->isAny())
591+
return upperBound;
590592
}
591593

592594
if (t->isTypeParameter()) {
593-
const auto protos = genericSig->getRequiredProtocols(t);
594-
if (!protos.empty())
595-
return buildProtocolComposition(protos);
595+
auto upperBound = genericSig->getUpperBound(
596+
t,
597+
/*forExistentialSelf=*/false,
598+
/*withParameterizedProtocols=*/false);
599+
600+
if (!upperBound->isAny())
601+
return upperBound;
596602
}
597603

598604
return t;

lib/IDE/CompletionOverrideLookup.cpp

+9-18
Original file line numberDiff line numberDiff line change
@@ -87,24 +87,15 @@ Type CompletionOverrideLookup::getOpaqueResultType(
8787
// If it has same type requrement, we will emit the concrete type.
8888
return nullptr;
8989

90-
// Collect requirements on the associatedtype.
91-
SmallVector<Type, 2> opaqueTypes;
92-
bool hasExplicitAnyObject = false;
93-
if (auto superTy = genericSig->getSuperclassBound(ResultT))
94-
opaqueTypes.push_back(superTy);
95-
for (const auto proto : genericSig->getRequiredProtocols(ResultT))
96-
opaqueTypes.push_back(proto->getDeclaredInterfaceType());
97-
if (auto layout = genericSig->getLayoutConstraint(ResultT))
98-
hasExplicitAnyObject = layout->isClass();
99-
100-
if (!hasExplicitAnyObject) {
101-
if (opaqueTypes.empty())
102-
return nullptr;
103-
if (opaqueTypes.size() == 1)
104-
return opaqueTypes.front();
105-
}
106-
return ProtocolCompositionType::get(VD->getASTContext(), opaqueTypes,
107-
hasExplicitAnyObject);
90+
auto upperBound = genericSig->getUpperBound(
91+
ResultT,
92+
/*forExistentialSelf=*/false,
93+
/*withParameterizedProtocols=*/false);
94+
95+
if (upperBound->isAny())
96+
return nullptr;
97+
98+
return upperBound;
10899
}
109100

110101
void CompletionOverrideLookup::addValueOverride(

0 commit comments

Comments
 (0)