@@ -4196,8 +4196,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4196
4196
4197
4197
// / Return type if the result type if \p VD should be represented as opaque
4198
4198
// / result type.
4199
- TypeLoc getOpaqueResultTypeLoc (const ValueDecl *VD, DeclVisibilityKind Reason,
4200
- DynamicLookupInfo dynamicLookupInfo) {
4199
+ Type getOpaqueResultType (const ValueDecl *VD, DeclVisibilityKind Reason,
4200
+ DynamicLookupInfo dynamicLookupInfo) {
4201
4201
if (Reason !=
4202
4202
DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal)
4203
4203
return nullptr ;
@@ -4216,41 +4216,65 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4216
4216
else
4217
4217
return nullptr ;
4218
4218
4219
- if (!ResultT->is <DependentMemberType>())
4220
- // The result is not associatedtype.
4221
- return nullptr ;
4222
-
4223
- // If associatedtype doesn't have conformance/superclass constraint, we
4224
- // can't use opaque type.
4225
- auto assocTyD = ResultT->castTo <DependentMemberType>()->getAssocType ();
4226
- if (!assocTyD->getInherited ().size ())
4219
+ if (!ResultT->is <DependentMemberType>() ||
4220
+ !ResultT->castTo <DependentMemberType>()->getAssocType ())
4221
+ // The result is not a valid associatedtype.
4227
4222
return nullptr ;
4228
4223
4229
4224
// Try substitution to see if the associated type is resolved to concrete
4230
4225
// type.
4231
4226
auto substMap = currTy->getMemberSubstitutionMap (
4232
4227
CurrDeclContext->getParentModule (), VD);
4233
- ResultT = ResultT.subst (substMap);
4234
- if (!ResultT || !ResultT->is <DependentMemberType>())
4228
+ if (!ResultT.subst (substMap)->is <DependentMemberType>())
4235
4229
// If resolved print it.
4236
4230
return nullptr ;
4237
4231
4238
- return assocTyD->getInherited ()[0 ];
4232
+ // Collect requirements on the associatedtype.
4233
+ ProtocolDecl *protoD =
4234
+ ResultT->castTo <DependentMemberType>()->getAssocType ()->getProtocol ();
4235
+
4236
+ SmallVector<Type, 2 > opaqueTypes;
4237
+ bool hasExplicitAnyObject = false ;
4238
+ for (auto req : protoD->getRequirementSignature ()) {
4239
+ if (!req.getFirstType ()->isEqual (ResultT))
4240
+ continue ;
4241
+
4242
+ switch (req.getKind ()) {
4243
+ case RequirementKind::Conformance:
4244
+ case RequirementKind::Superclass:
4245
+ opaqueTypes.push_back (req.getSecondType ());
4246
+ break ;
4247
+ case RequirementKind::Layout:
4248
+ hasExplicitAnyObject |= req.getLayoutConstraint ()->isClass ();
4249
+ break ;
4250
+ case RequirementKind::SameType:
4251
+ return nullptr ;
4252
+ }
4253
+ }
4254
+
4255
+ if (!hasExplicitAnyObject) {
4256
+ if (opaqueTypes.empty ())
4257
+ return nullptr ;
4258
+ if (opaqueTypes.size () == 1 )
4259
+ return opaqueTypes.front ();
4260
+ }
4261
+ return ProtocolCompositionType::get (
4262
+ VD->getASTContext (), opaqueTypes, hasExplicitAnyObject);
4239
4263
}
4240
4264
4241
4265
void addValueOverride (const ValueDecl *VD, DeclVisibilityKind Reason,
4242
4266
DynamicLookupInfo dynamicLookupInfo,
4243
4267
CodeCompletionResultBuilder &Builder,
4244
4268
bool hasDeclIntroducer) {
4245
4269
class DeclPrinter : public StreamPrinter {
4246
- TypeLoc OpaqueBaseTy;
4270
+ Type OpaqueBaseTy;
4247
4271
4248
4272
public:
4249
4273
using StreamPrinter::StreamPrinter;
4250
4274
4251
4275
Optional<unsigned > NameOffset;
4252
4276
4253
- DeclPrinter (raw_ostream &OS, TypeLoc OpaqueBaseTy)
4277
+ DeclPrinter (raw_ostream &OS, Type OpaqueBaseTy)
4254
4278
: StreamPrinter(OS), OpaqueBaseTy(OpaqueBaseTy) {}
4255
4279
4256
4280
void printDeclLoc (const Decl *D) override {
@@ -4262,7 +4286,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4262
4286
void printDeclResultTypePre (ValueDecl *VD, TypeLoc &TL) override {
4263
4287
if (!OpaqueBaseTy.isNull ()) {
4264
4288
OS << " some " ;
4265
- TL = OpaqueBaseTy;
4289
+ TL = TypeLoc::withoutLoc ( OpaqueBaseTy) ;
4266
4290
}
4267
4291
}
4268
4292
};
@@ -4272,7 +4296,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4272
4296
{
4273
4297
llvm::raw_svector_ostream OS (DeclStr);
4274
4298
DeclPrinter Printer (
4275
- OS, getOpaqueResultTypeLoc (VD, Reason, dynamicLookupInfo));
4299
+ OS, getOpaqueResultType (VD, Reason, dynamicLookupInfo));
4276
4300
PrintOptions Options;
4277
4301
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext ())
4278
4302
Options.setBaseType (transformType);
0 commit comments