Skip to content

Commit 6c13eba

Browse files
committed
Review amendments & fixed gen sig overload
1 parent ede3ba2 commit 6c13eba

File tree

2 files changed

+62
-24
lines changed

2 files changed

+62
-24
lines changed

lib/AST/LookupVisibleDecls.cpp

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
#include "NameLookupImpl.h"
1919
#include "swift/AST/ASTContext.h"
20+
#include "swift/AST/GenericSignature.h"
2021
#include "swift/AST/GenericSignatureBuilder.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/LazyResolver.h"
2324
#include "swift/AST/NameLookup.h"
2425
#include "swift/AST/ProtocolConformance.h"
25-
#include "swift/AST/SubstitutionMap.h"
2626
#include "swift/Basic/SourceManager.h"
2727
#include "swift/Basic/STLExtras.h"
2828
#include "swift/Sema/IDETypeChecking.h"
@@ -479,8 +479,7 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
479479
const DeclContext *DC;
480480
LazyResolver *resolver;
481481
llvm::DenseSet<DeclName> foundVars;
482-
llvm::DenseMap<std::tuple<DeclName, Type>, std::set<ValueDecl *>> foundFuncs;
483-
482+
llvm::DenseSet<std::pair<DeclName, CanType>> foundFuncs;
484483
void validate() {
485484
assert(DC && baseTy && !baseTy->hasLValueType());
486485
}
@@ -494,7 +493,9 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
494493

495494
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override {
496495
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())) {
498499
parentConsumer.foundDecl(VD, Reason);
499500
return;
500501
}
@@ -505,19 +506,35 @@ class RestateFilteringConsumer : public VisibleDeclConsumer {
505506
parentConsumer.foundDecl(VD, Reason);
506507
return;
507508
}
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)
510512
parentConsumer.foundDecl(VD, Reason);
511-
}
512513
return;
513514
}
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);
516530

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) {
518536
parentConsumer.foundDecl(VD, Reason);
519537
}
520-
foundFuncs[{VD->getFullName(), type}].insert(VD);
521538
}
522539
};
523540

@@ -532,11 +549,10 @@ static void
532549
if (!Visited.insert(PT->getDecl()).second)
533550
return;
534551

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);
540556
lookupTypeMembers(BaseTy, PT, Consumer, CurrDC, LS, Reason, TypeResolver);
541557
}
542558

@@ -587,7 +603,7 @@ static void lookupVisibleMemberDeclsImpl(
587603
}
588604

589605
// If the base is a protocol, enumerate its members.
590-
if (auto PT = BaseTy->getAs<ProtocolType>()) {
606+
if (ProtocolType *PT = BaseTy->getAs<ProtocolType>()) {
591607
lookupVisibleProtocolMemberDecls(BaseTy, PT, Consumer, CurrDC, LS, Reason,
592608
TypeResolver, GSB, Visited);
593609
return;
@@ -602,13 +618,13 @@ static void lookupVisibleMemberDeclsImpl(
602618
}
603619

604620
// 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())
612628
lookupVisibleMemberDeclsImpl(superclass, Consumer, CurrDC, LS,
613629
getReasonForSuper(Reason), TypeResolver,
614630
GSB, Visited);

test/IDE/complete_value_expr.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=NODUP_RESTATED_REQ_NODOT1 | %FileCheck %s -check-prefix=CHECK_NODUP_RESTATED_REQ_NODOT
127127
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=NODUP_RESTATED_REQ_NODOT2 | %FileCheck %s -check-prefix=CHECK_NODUP_RESTATED_REQ_NODOT
128128
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=NODUP_RESTATED_REQ_NODOT3 | %FileCheck %s -check-prefix=CHECK_NODUP_RESTATED_REQ_NODOT
129+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CHECK_PROT_OVERRIDES1 | %FileCheck %s -check-prefix=CHECK_PROT_OVERRIDES
130+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=CHECK_PROT_OVERRIDES2 | %FileCheck %s -check-prefix=CHECK_PROT_OVERRIDES
129131
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_P4 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4
130132
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_CONCRETE1 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4_P1
131133
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=PROTOCOL_EXT_CONCRETE2 | %FileCheck %s -check-prefix=PROTOCOL_EXT_P4_P1
@@ -1515,6 +1517,17 @@ protocol NoDupReq6: NoDupReq5 {
15151517
15161518
typealias NoDupReq23 = NoDupReq2 & NoDupReq3
15171519
1520+
protocol Override {
1521+
func foo<T: NoDupReq1>(_ arg: T)
1522+
func foo<T: NoDupReq2>(_ arg: T)
1523+
}
1524+
protocol Override2 {
1525+
func foo<T: NoDupReq1>(_ arg: T)
1526+
}
1527+
protocol Override3: Override2 {
1528+
func foo<T: NoDupReq2>(_ arg: T)
1529+
}
1530+
15181531
func checkRestatementNoDup1(_ arg: NoDupReq1 & NoDupReq2 & NoDupReq3) {
15191532
arg.#^NODUP_RESTATED_REQ1^#
15201533
arg#^NODUP_RESTATED_REQ_NODOT1^#
@@ -1539,6 +1552,12 @@ func checkRestatementNoDup6(_ arg: NoDupReq1 & NoDupReq23) {
15391552
arg.#^NODUP_RESTATED_REQ6^#
15401553
arg#^NODUP_RESTATED_REQ_NODOT3^#
15411554
}
1555+
func checkOverrideInclusion1(_ arg: Override) {
1556+
arg.#^CHECK_PROT_OVERRIDES1^#
1557+
}
1558+
func checkOverrideInclusion2(_ arg: Override3) {
1559+
arg.#^CHECK_PROT_OVERRIDES2^#
1560+
}
15421561
15431562
// CHECK_NODUP_RESTATED_REQ: Begin completions
15441563
// CHECK_NODUP_RESTATED_REQ-DAG: Decl[InstanceMethod]/{{Super|CurrNominal}}: foo()[#Void#]; name=foo()
@@ -1569,6 +1588,9 @@ func checkRestatementNoDup6(_ arg: NoDupReq1 & NoDupReq23) {
15691588
// CHECK_NODUP_RESTATED_REQ_TYPE-NOT: Decl[AssociatedType]/Super: E; name=E
15701589
// CHECK_NODUP_RESTATED_REQ_TYPE: End completions
15711590
1591+
// CHECK_PROT_OVERRIDES: Decl[InstanceMethod]/{{Super|CurrNominal}}: foo({#(arg): NoDupReq1#})[#Void#]; name=foo(arg: NoDupReq1)
1592+
// CHECK_PROT_OVERRIDES: Decl[InstanceMethod]/{{Super|CurrNominal}}: foo({#(arg): NoDupReq2#})[#Void#]; name=foo(arg: NoDupReq2)
1593+
15721594
struct OnlyMe {}
15731595
protocol P4 {
15741596
associatedtype T

0 commit comments

Comments
 (0)