Skip to content

Commit e470f39

Browse files
committed
IDE: Offer completions for dynamic Self
1 parent bcfafc1 commit e470f39

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,6 @@ static void lookupVisibleMemberDeclsImpl(
607607
// The metatype represents an arbitrary named type: dig through to the
608608
// declared type to see what we're dealing with.
609609
Type Ty = MTT->getInstanceType();
610-
if (auto dynSelfTy = Ty->getAs<DynamicSelfType>())
611-
Ty = dynSelfTy->getSelfType();
612610
if (Ty->is<AnyMetatypeType>())
613611
return;
614612

@@ -705,6 +703,12 @@ static void lookupVisibleMemberDeclsImpl(
705703
}
706704
}
707705

706+
// The members of a dynamic 'Self' type are the members of its static
707+
// class type.
708+
if (auto *const DS = BaseTy->getAs<DynamicSelfType>()) {
709+
BaseTy = DS->getSelfType();
710+
}
711+
708712
auto lookupTy = BaseTy;
709713

710714
const auto synthesizeAndLookupTypeMembers = [&](NominalTypeDecl *NTD) {

test/IDE/complete_super_self.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=COVARIANT_RETURN_CONV | %FileCheck %s --check-prefix=COVARIANT_RETURN_CONV
1+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
22

33
class BaseClass {
44
func returnSelf() -> Self {}
@@ -7,14 +7,22 @@ class BaseClass {
77
class DerivedClass: BaseClass {
88
var value: Int
99
func foo() {}
10-
}
1110

12-
func test(value: DerivedClass) {
13-
value.returnSelf().#^COVARIANT_RETURN_CONV^#
14-
// COVARIANT_RETURN_CONV: Begin completions, 4 items
15-
// COVARIANT_RETURN_CONV-DAG: Keyword[self]/CurrNominal: self[#DerivedClass#];
16-
// COVARIANT_RETURN_CONV-DAG: Decl[InstanceVar]/CurrNominal: value[#Int#];
17-
// COVARIANT_RETURN_CONV-DAG: Decl[InstanceMethod]/CurrNominal: foo()[#Void#];
18-
// COVARIANT_RETURN_CONV-DAG: Decl[InstanceMethod]/Super: returnSelf()[#Self#];
19-
// COVARIANT_RETURN_CONV: End completions
11+
func testWithStaticSelf() {
12+
self.returnSelf().#^COVARIANT_SELF_RETURN_STATIC?check=COVARIANT_SELF_RETURN^#
13+
}
14+
15+
func testWithDynamicSelf() -> Self {
16+
self.returnSelf().#^COVARIANT_SELF_RETURN_DYNAMIC?check=COVARIANT_SELF_RETURN^#
17+
return self
18+
}
2019
}
20+
21+
// COVARIANT_SELF_RETURN: Begin completions, 6 items
22+
// COVARIANT_SELF_RETURN-DAG: Keyword[self]/CurrNominal: self[#{{Self|DerivedClass}}#];
23+
// COVARIANT_SELF_RETURN-DAG: Decl[InstanceVar]/CurrNominal: value[#Int#];
24+
// COVARIANT_SELF_RETURN-DAG: Decl[InstanceMethod]/CurrNominal: foo()[#Void#];
25+
// COVARIANT_SELF_RETURN-DAG: Decl[InstanceMethod]/Super: returnSelf()[#Self#];
26+
// COVARIANT_SELF_RETURN-DAG: Decl[InstanceMethod]/CurrNominal: testWithStaticSelf()[#Void#];
27+
// COVARIANT_SELF_RETURN-DAG: Decl[InstanceMethod]/CurrNominal: testWithDynamicSelf()[#Self#];
28+
// COVARIANT_SELF_RETURN: End completions

0 commit comments

Comments
 (0)