Skip to content

[CodeCompletion] Don’t compute isolated parameter captures during code completion #73659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,10 @@ namespace {
llvm::function_ref<Type(Expr *)> getType;
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
getClosureActorIsolation;
/// Whether to check if the closure captures an `isolated` parameter.
/// This is needed as a workaround during code completion, which doesn't
/// have types applied to the AST and thus doesn't have captures computed.
bool checkIsolatedCapture;

SourceLoc requiredIsolationLoc;

Expand Down Expand Up @@ -2578,9 +2582,11 @@ namespace {
const DeclContext *dc,
llvm::function_ref<Type(Expr *)> getType = __Expr_getType,
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
getClosureActorIsolation = __AbstractClosureExpr_getActorIsolation)
getClosureActorIsolation = __AbstractClosureExpr_getActorIsolation,
bool checkIsolatedCapture = true)
: ctx(dc->getASTContext()), getType(getType),
getClosureActorIsolation(getClosureActorIsolation) {
getClosureActorIsolation(getClosureActorIsolation),
checkIsolatedCapture(checkIsolatedCapture) {
contextStack.push_back(dc);
}

Expand Down Expand Up @@ -4193,9 +4199,16 @@ namespace {
}

case ActorIsolation::ActorInstance: {
if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
return ActorIsolation::forActorInstanceCapture(param)
.withPreconcurrency(preconcurrency);
if (checkIsolatedCapture) {
if (auto param = closure->getCaptureInfo().getIsolatedParamCapture())
return ActorIsolation::forActorInstanceCapture(param)
.withPreconcurrency(preconcurrency);
} else {
// If we don't have capture information during code completion, assume
// that the closure captures the `isolated` parameter from the parent
// context.
return parentIsolation;
}
Comment on lines +4202 to +4211
Copy link
Contributor

@hamishknight hamishknight May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess an alternative solution would be to call computeCaptures (or setCaptureInfo) for completion to just populate an empty set of captures. Can't say I really love either solution, but I think I would mildly prefer populating empty captures since it's less invasive.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@slavapestov Do you have a preference on whether we should call computeCaptures in completion even though we know that it will compute incomplete captures or have a parameter like this? Hamish and I don’t have super strong opinions either way.


return ActorIsolation::forNonisolated(/*unsafe=*/false)
.withPreconcurrency(preconcurrency);
Expand Down Expand Up @@ -4325,7 +4338,8 @@ ActorIsolation swift::determineClosureActorIsolation(
llvm::function_ref<ActorIsolation(AbstractClosureExpr *)>
getClosureActorIsolation) {
ActorIsolationChecker checker(closure->getParent(), getType,
getClosureActorIsolation);
getClosureActorIsolation,
/*checkIsolatedCapture=*/false);
return checker.determineClosureIsolation(closure);
}

Expand Down
15 changes: 15 additions & 0 deletions validation-test/IDE/crashers_2_fixed/rdar126923558.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %target-swift-ide-test -code-completion -code-completion-token=COMPLETE -source-filename=%s

actor Foo {
func bar() {}
}

func takeClosure(_ x: () -> Void) {}

actor Foo {
func tests(myInt: Int) {
takeClosure {
myInt.#^COMPLETE^#
}
}
}