Skip to content

Commit ba773d5

Browse files
authored
Merge pull request #73659 from ahoppen/ahoppen/fix-capture-crash
[CodeCompletion] Don’t compute isolated parameter captures during code completion
2 parents a6f5f21 + 22f0daf commit ba773d5

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,6 +2082,10 @@ namespace {
20822082
llvm::function_ref<Type(Expr *)> getType;
20832083
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
20842084
getClosureActorIsolation;
2085+
/// Whether to check if the closure captures an `isolated` parameter.
2086+
/// This is needed as a workaround during code completion, which doesn't
2087+
/// have types applied to the AST and thus doesn't have captures computed.
2088+
bool checkIsolatedCapture;
20852089

20862090
SourceLoc requiredIsolationLoc;
20872091

@@ -2578,9 +2582,11 @@ namespace {
25782582
const DeclContext *dc,
25792583
llvm::function_ref<Type(Expr *)> getType = __Expr_getType,
25802584
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
2581-
getClosureActorIsolation = __AbstractClosureExpr_getActorIsolation)
2585+
getClosureActorIsolation = __AbstractClosureExpr_getActorIsolation,
2586+
bool checkIsolatedCapture = true)
25822587
: ctx(dc->getASTContext()), getType(getType),
2583-
getClosureActorIsolation(getClosureActorIsolation) {
2588+
getClosureActorIsolation(getClosureActorIsolation),
2589+
checkIsolatedCapture(checkIsolatedCapture) {
25842590
contextStack.push_back(dc);
25852591
}
25862592

@@ -4193,9 +4199,16 @@ namespace {
41934199
}
41944200

41954201
case ActorIsolation::ActorInstance: {
4196-
if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
4197-
return ActorIsolation::forActorInstanceCapture(param)
4198-
.withPreconcurrency(preconcurrency);
4202+
if (checkIsolatedCapture) {
4203+
if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
4204+
return ActorIsolation::forActorInstanceCapture(param)
4205+
.withPreconcurrency(preconcurrency);
4206+
} else {
4207+
// If we don't have capture information during code completion, assume
4208+
// that the closure captures the `isolated` parameter from the parent
4209+
// context.
4210+
return parentIsolation;
4211+
}
41994212

42004213
return ActorIsolation::forNonisolated(/*unsafe=*/false)
42014214
.withPreconcurrency(preconcurrency);
@@ -4325,7 +4338,8 @@ ActorIsolation swift::determineClosureActorIsolation(
43254338
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
43264339
getClosureActorIsolation) {
43274340
ActorIsolationChecker checker(closure->getParent(), getType,
4328-
getClosureActorIsolation);
4341+
getClosureActorIsolation,
4342+
/*checkIsolatedCapture=*/false);
43294343
return checker.determineClosureIsolation(closure);
43304344
}
43314345

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %target-swift-ide-test -code-completion -code-completion-token=COMPLETE -source-filename=%s
2+
3+
actor Foo {
4+
func bar() {}
5+
}
6+
7+
func takeClosure(_ x: () -> Void) {}
8+
9+
actor Foo {
10+
func tests(myInt: Int) {
11+
takeClosure {
12+
myInt.#^COMPLETE^#
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)