Skip to content

Commit a024d4c

Browse files
authored
Merge pull request #67300 from ahoppen/ahoppen/5.9/result-builder-signature-crash
[5.9][CodeComplete] Fix a crash when completing in the signature of a function annotated with result builder
2 parents 9fc33da + 5c00c9e commit a024d4c

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,22 +2583,25 @@ bool TypeCheckASTNodeAtLocRequest::evaluate(
25832583
// Function builder function doesn't support partial type checking.
25842584
if (auto *func = dyn_cast<FuncDecl>(DC)) {
25852585
if (Type builderType = getResultBuilderType(func)) {
2586-
auto optBody = TypeChecker::applyResultBuilderBodyTransform(
2587-
func, builderType,
2588-
/*ClosuresInResultBuilderDontParticipateInInference=*/
2589-
ctx.CompletionCallback == nullptr && ctx.SolutionCallback == nullptr);
2590-
if (optBody && *optBody) {
2591-
// Wire up the function body now.
2592-
func->setBody(*optBody, AbstractFunctionDecl::BodyKind::TypeChecked);
2593-
return false;
2586+
if (func->getBody()) {
2587+
auto optBody = TypeChecker::applyResultBuilderBodyTransform(
2588+
func, builderType,
2589+
/*ClosuresInResultBuilderDontParticipateInInference=*/
2590+
ctx.CompletionCallback == nullptr &&
2591+
ctx.SolutionCallback == nullptr);
2592+
if (optBody && *optBody) {
2593+
// Wire up the function body now.
2594+
func->setBody(*optBody, AbstractFunctionDecl::BodyKind::TypeChecked);
2595+
return false;
2596+
}
2597+
// FIXME: We failed to apply the result builder transform. Fall back to
2598+
// just type checking the node that contains the code completion token.
2599+
// This may be missing some context from the result builder but in
2600+
// practice it often contains sufficient information to provide a decent
2601+
// level of code completion that's better than providing nothing at all.
2602+
// The proper solution would be to only partially type check the result
2603+
// builder so that this fall back would not be necessary.
25942604
}
2595-
// FIXME: We failed to apply the result builder transform. Fall back to
2596-
// just type checking the node that contains the code completion token.
2597-
// This may be missing some context from the result builder but in
2598-
// practice it often contains sufficient information to provide a decent
2599-
// level of code completion that's better than providing nothing at all.
2600-
// The proper solution would be to only partially type check the result
2601-
// builder so that this fall back would not be necessary.
26022605
} else if (func->hasSingleExpressionBody() &&
26032606
func->getResultInterfaceType()->isVoid()) {
26042607
// The function returns void. We don't need an explicit return, no matter
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
@resultBuilder struct MyBuilder {
5+
static func buildBlock() -> Int
6+
static func buildBlock<Content>(_ content: Content) -> Content
7+
}
8+
9+
@MyBuilder func test(action: () -> #^COMPLETE^#Void) {}
10+
11+
// COMPLETE: Decl[TypeAlias]/OtherModule[Swift]/IsSystem: Void[#Void#]; name=Void

0 commit comments

Comments
 (0)