17
17
18
18
#include " NameLookupImpl.h"
19
19
#include " swift/AST/ASTContext.h"
20
+ #include " swift/AST/GenericSignature.h"
20
21
#include " swift/AST/GenericSignatureBuilder.h"
21
22
#include " swift/AST/Initializer.h"
22
23
#include " swift/AST/LazyResolver.h"
23
24
#include " swift/AST/NameLookup.h"
24
25
#include " swift/AST/ProtocolConformance.h"
25
- #include " swift/AST/SubstitutionMap.h"
26
26
#include " swift/Basic/SourceManager.h"
27
27
#include " swift/Basic/STLExtras.h"
28
28
#include " swift/Sema/IDETypeChecking.h"
@@ -479,8 +479,7 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
479
479
const DeclContext *DC;
480
480
LazyResolver *resolver;
481
481
llvm::DenseSet<DeclName> foundVars;
482
- llvm::DenseMap<std::tuple<DeclName, Type>, std::set<ValueDecl *>> foundFuncs;
483
-
482
+ llvm::DenseSet<std::pair<DeclName, CanType>> foundFuncs;
484
483
void validate () {
485
484
assert (DC && baseTy && !baseTy->hasLValueType ());
486
485
}
@@ -494,7 +493,9 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
494
493
495
494
void foundDecl (ValueDecl *VD, DeclVisibilityKind Reason) override {
496
495
assert (VD);
497
- if (!VD->getDeclContext ()->getAsProtocolOrProtocolExtensionContext ()) {
496
+ // If this isn't a protocol context, hand over the decl
497
+ // to the parent consumer.
498
+ if (!isa<ProtocolDecl>(VD->getDeclContext ())) {
498
499
parentConsumer.foundDecl (VD, Reason);
499
500
return ;
500
501
}
@@ -505,19 +506,35 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
505
506
parentConsumer.foundDecl (VD, Reason);
506
507
return ;
507
508
}
508
- if (!VD->getInterfaceType ()->is <AnyFunctionType>()) {
509
- if (foundVars.insert (VD->getFullName ()).second ) {
509
+ auto AFT = VD->getInterfaceType ()->getAs <AnyFunctionType>();
510
+ if (!AFT) {
511
+ if (foundVars.insert (VD->getFullName ()).second )
510
512
parentConsumer.foundDecl (VD, Reason);
511
- }
512
513
return ;
513
514
}
514
- auto type =
515
- VD->getOverloadSignatureType ()->getAs <AnyFunctionType>()->getResult ();
515
+ // Preserve the generic signature if this is a subscript, which are uncurried,
516
+ // or if we have genenic params other than Self. Otherwise, strip if off
517
+ // and use the resultType of the curried function type.
518
+ // When preserving the generic signature, we remove the requirement
519
+ // from Self to make sure it doesn't prevent us from recognizing restatements.
520
+ CanType type;
521
+ // This can't be null since we are dealing with method or
522
+ // subscript requirements, where Self is an implicit generic param.
523
+ auto sig = AFT->getOptGenericSignature ();
524
+ auto params = sig->getGenericParams ();
525
+ if (params.size () == 1 && !isa<SubscriptDecl>(VD)) {
526
+ type = AFT->getResult ()->getCanonicalType ();
527
+ } else {
528
+ auto newReqs = sig->getRequirements ().drop_front ();
529
+ auto newSig = GenericSignature::get (params, newReqs, false );
516
530
517
- if (foundFuncs[{VD->getFullName (), type}].empty ()) {
531
+ type = GenericFunctionType::get (newSig, AFT->getInput (),
532
+ AFT->getResult (), AFT->getExtInfo ())
533
+ ->getCanonicalType (newSig);
534
+ }
535
+ if (foundFuncs.insert ({VD->getFullName (), type}).second ) {
518
536
parentConsumer.foundDecl (VD, Reason);
519
537
}
520
- foundFuncs[{VD->getFullName (), type}].insert (VD);
521
538
}
522
539
};
523
540
@@ -532,11 +549,10 @@ static void
532
549
if (!Visited.insert (PT->getDecl ()).second )
533
550
return ;
534
551
535
- for (auto Proto : PT->getDecl ()->getInheritedProtocols ()){
536
- lookupVisibleProtocolMemberDecls (BaseTy, Proto->getDeclaredType (),
537
- Consumer, CurrDC, LS,
538
- getReasonForSuper (Reason), TypeResolver,
539
- GSB, Visited);}
552
+ for (auto Proto : PT->getDecl ()->getInheritedProtocols ())
553
+ lookupVisibleProtocolMemberDecls (BaseTy, Proto->getDeclaredType (), Consumer, CurrDC,
554
+ LS, getReasonForSuper (Reason), TypeResolver,
555
+ GSB, Visited);
540
556
lookupTypeMembers (BaseTy, PT, Consumer, CurrDC, LS, Reason, TypeResolver);
541
557
}
542
558
@@ -587,7 +603,7 @@ static void lookupVisibleMemberDeclsImpl(
587
603
}
588
604
589
605
// If the base is a protocol, enumerate its members.
590
- if (auto PT = BaseTy->getAs <ProtocolType>()) {
606
+ if (ProtocolType * PT = BaseTy->getAs <ProtocolType>()) {
591
607
lookupVisibleProtocolMemberDecls (BaseTy, PT, Consumer, CurrDC, LS, Reason,
592
608
TypeResolver, GSB, Visited);
593
609
return ;
@@ -602,13 +618,13 @@ static void lookupVisibleMemberDeclsImpl(
602
618
}
603
619
604
620
// Enumerate members of archetype's requirements.
605
- if (ArchetypeType *AT = BaseTy->getAs <ArchetypeType>()) {
606
- for (auto Proto : AT ->getConformsTo ())
607
- lookupVisibleProtocolMemberDecls (BaseTy, Proto-> getDeclaredType (),
608
- Consumer, CurrDC, LS,
609
- getReasonForSuper (Reason),
610
- TypeResolver, GSB, Visited);
611
- if (auto superclass = AT ->getSuperclass ())
621
+ if (ArchetypeType *Archetype = BaseTy->getAs <ArchetypeType>()) {
622
+ for (auto Proto : Archetype ->getConformsTo ())
623
+ lookupVisibleProtocolMemberDecls (
624
+ BaseTy, Proto-> getDeclaredType (), Consumer, CurrDC, LS,
625
+ getReasonForSuper (Reason), TypeResolver, GSB, Visited);
626
+
627
+ if (auto superclass = Archetype ->getSuperclass ())
612
628
lookupVisibleMemberDeclsImpl (superclass, Consumer, CurrDC, LS,
613
629
getReasonForSuper (Reason), TypeResolver,
614
630
GSB, Visited);
0 commit comments