Skip to content

Commit fd9b6a1

Browse files
committed
[CodeCompletion] Analyze the parent of initializer
For `Initializer` decl context, type context analyzer should look into its parent context because that might constrain initializer's type. rdar://problem/48208253
1 parent 4b44fed commit fd9b6a1

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/Decl.h"
1717
#include "swift/AST/DeclContext.h"
1818
#include "swift/AST/Expr.h"
19+
#include "swift/AST/Initializer.h"
1920
#include "swift/AST/LazyResolver.h"
2021
#include "swift/AST/Module.h"
2122
#include "swift/AST/Pattern.h"
@@ -691,7 +692,11 @@ class ExprContextAnalyzer {
691692
} else
692693
return false;
693694
});
694-
DC->walkContext(Finder);
695+
696+
// For 'Initializer' context, we need to look into its parent because it
697+
// might constrain the initializer's type.
698+
auto analyzeDC = isa<Initializer>(DC) ? DC->getParent() : DC;
699+
analyzeDC->walkContext(Finder);
695700

696701
if (Finder.Ancestors.empty())
697702
return;

test/IDE/complete_in_accessors.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func returnsInt() -> Int {}
150150

151151
// WITH_MEMBER_DECLS_INIT: Begin completions
152152
// WITH_MEMBER_DECLS_INIT-DAG: Decl[Struct]/CurrModule: FooStruct[#FooStruct#]{{; name=.+$}}
153-
// WITH_MEMBER_DECLS_INIT-DAG: Decl[FreeFunction]/CurrModule: returnsInt()[#Int#]{{; name=.+$}}
153+
// WITH_MEMBER_DECLS_INIT-DAG: Decl[FreeFunction]/CurrModule/TypeRelation[Identical]: returnsInt()[#Int#]{{; name=.+$}}
154154
// WITH_MEMBER_DECLS_INIT-DAG: Decl[InstanceMethod]/CurrNominal: instanceFunc({#self: MemberAccessors#})[#(Int) -> Float#]{{; name=.+$}}
155155
// WITH_MEMBER_DECLS_INIT: End completions
156156

test/IDE/complete_unresolved_members.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@
100100
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERICPARAM_20 | %FileCheck %s -check-prefix=GENERICPARAM_1
101101
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERICPARAM_21 | %FileCheck %s -check-prefix=GENERICPARAM_1
102102

103+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DECL_MEMBER_INIT_1 | %FileCheck %s -check-prefix=UNRESOLVED_3
104+
103105
enum SomeEnum1 {
104106
case South
105107
case North
@@ -645,3 +647,7 @@ func testingGenericParam2<X>(obj: C<X>) {
645647
obj.t = .#^GENERICPARAM_21^#
646648
// Same as GENERICPARAM_1.
647649
}
650+
651+
struct TestingStruct {
652+
var value: SomeEnum1 = .#^DECL_MEMBER_INIT_1^#
653+
}

0 commit comments

Comments
 (0)