Skip to content

Local functions can capture "self" as isolated. #38010

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 23, 2021
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
25 changes: 12 additions & 13 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,10 @@ namespace {
}
}

// "Defer" blocks are treated as if they are in their enclosing context.
if (auto func = dyn_cast<FuncDecl>(dc)) {
if (func->isDeferBody())
continue;
if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
// @Sendable functions are nonisolated.
if (func->isSendable())
return ReferencedActor(var, ReferencedActor::SendableFunction);
}

// Check isolation of the context itself. We do this separately
Expand All @@ -1325,9 +1325,14 @@ namespace {
switch (auto isolation = getActorIsolationOfContext(dc)) {
case ActorIsolation::Independent:
case ActorIsolation::Unspecified:
if (auto func = dyn_cast<AbstractFunctionDecl>(dc)) {
if (func->isSendable())
return ReferencedActor(var, ReferencedActor::SendableFunction);
// Local functions can capture an isolated parameter.
// FIXME: This really should be modeled by getActorIsolationOfContext.
if (isa<FuncDecl>(dc) && cast<FuncDecl>(dc)->isLocalCapture()) {
// FIXME: Local functions could presumably capture an isolated
// parameter that isn't 'self'.
if (isPotentiallyIsolated &&
(var->isSelfParameter() || var->isSelfParamCapture()))
continue;
}

return ReferencedActor(var, ReferencedActor::NonIsolatedContext);
Expand All @@ -1339,12 +1344,6 @@ namespace {

case ActorIsolation::ActorInstance:
case ActorIsolation::DistributedActorInstance:
// FIXME: Local functions could presumably capture an isolated
// parameter that isn't'self'.
if (isPotentiallyIsolated &&
(var->isSelfParameter() || var->isSelfParamCapture()))
return ReferencedActor(var, ReferencedActor::Isolated);

break;
}
}
Expand Down
9 changes: 9 additions & 0 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -846,4 +846,13 @@ actor Counter {

return counter
}

func localNext() -> Int {
func doIt() {
counter = counter + 1
}
doIt()

return counter
}
}