Skip to content

Commit d022cf8

Browse files
committed
[CodeComplete] Avoid using an unresolved contextual type
This can occur for e.g multi-statement closure returns (and soon for singe-expression closures too), so fall back to evaluating the expression type if it's not fully resolved.
1 parent 3d06f0d commit d022cf8

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lib/IDE/TypeCheckCompletionCallback.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,11 @@ void TypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
4747

4848
Type swift::ide::getTypeForCompletion(const constraints::Solution &S,
4949
ASTNode Node) {
50+
// Use the contextual type, unless it is still unresolved, in which case fall
51+
// back to getting the type from the expression.
5052
if (auto ContextualType = S.getContextualType(Node)) {
51-
return ContextualType;
53+
if (!ContextualType->hasUnresolvedType())
54+
return ContextualType;
5255
}
5356

5457
if (!S.hasType(Node)) {

test/IDE/complete_issue-55711.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=A | %FileCheck %s --check-prefix=A
2-
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=B | %FileCheck %s --check-prefix=B
3-
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=D | %FileCheck %s --check-prefix=D
1+
// RUN: %batch-code-completion
42

53
// https://github.com/apple/swift/issues/55711
64
// https://forums.swift.org/t/code-completion-enhancement-request/38677
@@ -37,6 +35,10 @@ func test() {
3735
C(.a) {
3836
.#^A^#
3937
}
38+
C(.a) {
39+
()
40+
return .#^A_MULTISTMT?check=A^#
41+
}
4042
// A: Begin completions, 2 items
4143
// A-DAG: Decl[StaticMethod]/CurrNominal/TypeRelation[Convertible]: foo({#arg: Bool#})[#A<X>#];
4244
// A-DAG: Decl[StaticMethod]/CurrNominal/TypeRelation[Convertible]: bar({#arg: Int#})[#A<Y>#];

0 commit comments

Comments
 (0)