@@ -61,10 +61,13 @@ struct LookupState {
6161 // / This option is only for override completion lookup.
6262 unsigned IncludeDerivedRequirements : 1 ;
6363
64+ // / Should protocol extension members be included?
65+ unsigned IncludeProtocolExtensionMembers : 1 ;
66+
6467 LookupState ()
6568 : IsQualified(0 ), IsOnMetatype(0 ), IsOnSuperclass(0 ),
6669 InheritsSuperclassInitializers (0 ), IncludeInstanceMembers(0 ),
67- IncludeDerivedRequirements(0 ) {}
70+ IncludeDerivedRequirements(0 ), IncludeProtocolExtensionMembers( 0 ) {}
6871
6972public:
7073 LookupState (const LookupState &) = default;
@@ -91,6 +94,9 @@ struct LookupState {
9194 bool isIncludingDerivedRequirements () const {
9295 return IncludeDerivedRequirements;
9396 }
97+ bool isIncludingProtocolExtensionMembers () const {
98+ return IncludeProtocolExtensionMembers;
99+ }
94100
95101 LookupState withOnMetatype () const {
96102 auto Result = *this ;
@@ -127,6 +133,12 @@ struct LookupState {
127133 Result.IncludeDerivedRequirements = 1 ;
128134 return Result;
129135 }
136+
137+ LookupState withIncludeProtocolExtensionMembers () const {
138+ auto Result = *this ;
139+ Result.IncludeProtocolExtensionMembers = 1 ;
140+ return Result;
141+ }
130142};
131143} // end anonymous namespace
132144
@@ -463,13 +475,20 @@ static void lookupDeclsFromProtocolsBeingConformedTo(
463475 };
464476 DeclVisibilityKind ReasonForThisDecl = ReasonForThisProtocol;
465477 if (const auto Witness = NormalConformance->getWitness (VD)) {
466- if (Witness.getDecl ()->getFullName () == VD->getFullName ()) {
478+ auto *WD = Witness.getDecl ();
479+ if (WD->getFullName () == VD->getFullName ()) {
467480 if (LS.isIncludingDerivedRequirements () &&
468481 Reason == DeclVisibilityKind::MemberOfCurrentNominal &&
469- isDerivedRequirement (Witness. getDecl () )) {
482+ isDerivedRequirement (WD )) {
470483 ReasonForThisDecl =
471484 DeclVisibilityKind::MemberOfProtocolDerivedByCurrentNominal;
485+ } else if (!LS.isIncludingProtocolExtensionMembers () &&
486+ WD->getDeclContext ()->getExtendedProtocolDecl ()) {
487+ // Don't skip this requiement.
488+ // Witnesses in protocol extensions aren't reported.
472489 } else {
490+ // lookupVisibleMemberDecls() generally prefers witness members
491+ // over requirements.
473492 continue ;
474493 }
475494 }
@@ -481,11 +500,13 @@ static void lookupDeclsFromProtocolsBeingConformedTo(
481500 }
482501
483502 // Add members from any extensions.
484- SmallVector<ValueDecl *, 2 > FoundDecls;
485- doGlobalExtensionLookup (BaseTy, Proto->getDeclaredType (), FoundDecls,
486- FromContext, LS, ReasonForThisProtocol);
487- for (auto *VD : FoundDecls)
488- Consumer.foundDecl (VD, ReasonForThisProtocol);
503+ if (LS.isIncludingProtocolExtensionMembers ()) {
504+ SmallVector<ValueDecl *, 2 > FoundDecls;
505+ doGlobalExtensionLookup (BaseTy, Proto->getDeclaredType (), FoundDecls,
506+ FromContext, LS, ReasonForThisProtocol);
507+ for (auto *VD : FoundDecls)
508+ Consumer.foundDecl (VD, ReasonForThisProtocol);
509+ }
489510 }
490511}
491512
@@ -582,6 +603,9 @@ static void lookupVisibleMemberDeclsImpl(
582603 if (LS.isIncludingDerivedRequirements ()) {
583604 subLS = subLS.withIncludedDerivedRequirements ();
584605 }
606+ if (LS.isIncludingProtocolExtensionMembers ()) {
607+ subLS = subLS.withIncludeProtocolExtensionMembers ();
608+ }
585609
586610 // Just perform normal dot lookup on the type see if we find extensions or
587611 // anything else. For example, type SomeTy.SomeMember can look up static
@@ -1111,6 +1135,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,
11111135 GenericParamList *GenericParams = nullptr ;
11121136 Type ExtendedType;
11131137 auto LS = LookupState::makeUnqualified ();
1138+ LS = LS.withIncludeProtocolExtensionMembers ();
11141139
11151140 // Skip initializer contexts, we will not find any declarations there.
11161141 if (isa<Initializer>(DC)) {
@@ -1308,6 +1333,7 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
13081333 const DeclContext *CurrDC,
13091334 bool includeInstanceMembers,
13101335 bool includeDerivedRequirements,
1336+ bool includeProtocolExtensionMembers,
13111337 GenericSignatureBuilder *GSB) {
13121338 assert (CurrDC);
13131339 LookupState ls = LookupState::makeQualified ();
@@ -1317,6 +1343,9 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
13171343 if (includeDerivedRequirements) {
13181344 ls = ls.withIncludedDerivedRequirements ();
13191345 }
1346+ if (includeProtocolExtensionMembers) {
1347+ ls = ls.withIncludeProtocolExtensionMembers ();
1348+ }
13201349
13211350 ::lookupVisibleMemberDecls (BaseTy, Consumer, CurrDC, ls,
13221351 DeclVisibilityKind::MemberOfCurrentNominal,
0 commit comments