[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.
This commit is contained in:
Hamish Knight
2024-01-29 21:26:08 +00:00
parent 3d06f0d7c0
commit d022cf806b
2 changed files with 9 additions and 4 deletions

View File

@@ -47,8 +47,11 @@ void TypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
Type swift::ide::getTypeForCompletion(const constraints::Solution &S,
ASTNode Node) {
// Use the contextual type, unless it is still unresolved, in which case fall
// back to getting the type from the expression.
if (auto ContextualType = S.getContextualType(Node)) {
return ContextualType;
if (!ContextualType->hasUnresolvedType())
return ContextualType;
}
if (!S.hasType(Node)) {

View File

@@ -1,6 +1,4 @@
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=A | %FileCheck %s --check-prefix=A
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=B | %FileCheck %s --check-prefix=B
// RUN: %swift-ide-test -code-completion -source-filename=%s -code-completion-token=D | %FileCheck %s --check-prefix=D
// RUN: %batch-code-completion
// https://github.com/apple/swift/issues/55711
// https://forums.swift.org/t/code-completion-enhancement-request/38677
@@ -37,6 +35,10 @@ func test() {
C(.a) {
.#^A^#
}
C(.a) {
()
return .#^A_MULTISTMT?check=A^#
}
// A: Begin completions, 2 items
// A-DAG: Decl[StaticMethod]/CurrNominal/TypeRelation[Convertible]: foo({#arg: Bool#})[#A<X>#];
// A-DAG: Decl[StaticMethod]/CurrNominal/TypeRelation[Convertible]: bar({#arg: Int#})[#A<Y>#];