@@ -4159,8 +4159,8 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4159
4159
4160
4160
// / Return type if the result type if \p VD should be represented as opaque
4161
4161
// / result type.
4162
- TypeLoc getOpaqueResultTypeLoc (const ValueDecl *VD, DeclVisibilityKind Reason,
4163
- DynamicLookupInfo dynamicLookupInfo) {
4162
+ Type getOpaqueResultType (const ValueDecl *VD, DeclVisibilityKind Reason,
4163
+ DynamicLookupInfo dynamicLookupInfo) {
4164
4164
if (Reason !=
4165
4165
DeclVisibilityKind::MemberOfProtocolImplementedByCurrentNominal)
4166
4166
return nullptr ;
@@ -4179,41 +4179,65 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4179
4179
else
4180
4180
return nullptr ;
4181
4181
4182
- if (!ResultT->is <DependentMemberType>())
4183
- // The result is not associatedtype.
4184
- return nullptr ;
4185
-
4186
- // If associatedtype doesn't have conformance/superclass constraint, we
4187
- // can't use opaque type.
4188
- auto assocTyD = ResultT->castTo <DependentMemberType>()->getAssocType ();
4189
- if (!assocTyD->getInherited ().size ())
4182
+ if (!ResultT->is <DependentMemberType>() ||
4183
+ !ResultT->castTo <DependentMemberType>()->getAssocType ())
4184
+ // The result is not a valid associatedtype.
4190
4185
return nullptr ;
4191
4186
4192
4187
// Try substitution to see if the associated type is resolved to concrete
4193
4188
// type.
4194
4189
auto substMap = currTy->getMemberSubstitutionMap (
4195
4190
CurrDeclContext->getParentModule (), VD);
4196
- ResultT = ResultT.subst (substMap);
4197
- if (!ResultT || !ResultT->is <DependentMemberType>())
4191
+ if (!ResultT.subst (substMap)->is <DependentMemberType>())
4198
4192
// If resolved print it.
4199
4193
return nullptr ;
4200
4194
4201
- return assocTyD->getInherited ()[0 ];
4195
+ // Collect requirements on the associatedtype.
4196
+ ProtocolDecl *protoD =
4197
+ ResultT->castTo <DependentMemberType>()->getAssocType ()->getProtocol ();
4198
+
4199
+ SmallVector<Type, 2 > opaqueTypes;
4200
+ bool hasExplicitAnyObject = false ;
4201
+ for (auto req : protoD->getRequirementSignature ()) {
4202
+ if (!req.getFirstType ()->isEqual (ResultT))
4203
+ continue ;
4204
+
4205
+ switch (req.getKind ()) {
4206
+ case RequirementKind::Conformance:
4207
+ case RequirementKind::Superclass:
4208
+ opaqueTypes.push_back (req.getSecondType ());
4209
+ break ;
4210
+ case RequirementKind::Layout:
4211
+ hasExplicitAnyObject |= req.getLayoutConstraint ()->isClass ();
4212
+ break ;
4213
+ case RequirementKind::SameType:
4214
+ return nullptr ;
4215
+ }
4216
+ }
4217
+
4218
+ if (!hasExplicitAnyObject) {
4219
+ if (opaqueTypes.empty ())
4220
+ return nullptr ;
4221
+ if (opaqueTypes.size () == 1 )
4222
+ return opaqueTypes.front ();
4223
+ }
4224
+ return ProtocolCompositionType::get (
4225
+ VD->getASTContext (), opaqueTypes, hasExplicitAnyObject);
4202
4226
}
4203
4227
4204
4228
void addValueOverride (const ValueDecl *VD, DeclVisibilityKind Reason,
4205
4229
DynamicLookupInfo dynamicLookupInfo,
4206
4230
CodeCompletionResultBuilder &Builder,
4207
4231
bool hasDeclIntroducer) {
4208
4232
class DeclPrinter : public StreamPrinter {
4209
- TypeLoc OpaqueBaseTy;
4233
+ Type OpaqueBaseTy;
4210
4234
4211
4235
public:
4212
4236
using StreamPrinter::StreamPrinter;
4213
4237
4214
4238
Optional<unsigned > NameOffset;
4215
4239
4216
- DeclPrinter (raw_ostream &OS, TypeLoc OpaqueBaseTy)
4240
+ DeclPrinter (raw_ostream &OS, Type OpaqueBaseTy)
4217
4241
: StreamPrinter(OS), OpaqueBaseTy(OpaqueBaseTy) {}
4218
4242
4219
4243
void printDeclLoc (const Decl *D) override {
@@ -4225,7 +4249,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4225
4249
void printDeclResultTypePre (ValueDecl *VD, TypeLoc &TL) override {
4226
4250
if (!OpaqueBaseTy.isNull ()) {
4227
4251
OS << " some " ;
4228
- TL = OpaqueBaseTy;
4252
+ TL = TypeLoc::withoutLoc ( OpaqueBaseTy) ;
4229
4253
}
4230
4254
}
4231
4255
};
@@ -4235,7 +4259,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
4235
4259
{
4236
4260
llvm::raw_svector_ostream OS (DeclStr);
4237
4261
DeclPrinter Printer (
4238
- OS, getOpaqueResultTypeLoc (VD, Reason, dynamicLookupInfo));
4262
+ OS, getOpaqueResultType (VD, Reason, dynamicLookupInfo));
4239
4263
PrintOptions Options;
4240
4264
if (auto transformType = CurrDeclContext->getDeclaredTypeInContext ())
4241
4265
Options.setBaseType (transformType);
0 commit comments