Skip to content

[Concurrency] Don't allow erasing global actor isolation when the function value crosses an isolation boundary. #72607

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
Mar 27, 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
11 changes: 9 additions & 2 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,7 +1917,7 @@ bool swift::isAsyncDecl(ConcreteDeclRef declRef) {
/// \param ty a function type where \c globalActor was removed from it.
/// \return true if it is safe to drop the global-actor qualifier.
static bool safeToDropGlobalActor(
DeclContext *dc, Type globalActor, Type ty) {
DeclContext *dc, Type globalActor, Type ty, ApplyExpr *call) {
auto funcTy = ty->getAs<AnyFunctionType>();
if (!funcTy)
return false;
Expand All @@ -1942,6 +1942,12 @@ static bool safeToDropGlobalActor(
if (funcTy->isAsync())
return true;

// If the argument is passed over an isolation boundary, it's not
// safe to erase actor isolation, because the callee can call the
// function synchronously from outside the isolation domain.
if (call && call->getIsolationCrossing())
return false;

// fundamentally cannot be sendable if we want to drop isolation info
if (funcTy->isSendable())
return false;
Expand Down Expand Up @@ -2364,7 +2370,8 @@ namespace {
return;

auto dc = const_cast<DeclContext*>(getDeclContext());
if (!safeToDropGlobalActor(dc, fromActor, toType)) {
if (!safeToDropGlobalActor(dc, fromActor, toType,
getImmediateApply())) {
// otherwise, it's not a safe cast.
dc->getASTContext()
.Diags
Expand Down
11 changes: 11 additions & 0 deletions test/Concurrency/actor_isolation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,8 @@ actor Crystal {
await asyncGlobalActorFunc()
}

func crossIsolationBoundary(_ closure: () -> Void) async {}

@available(SwiftStdlib 5.1, *)
func testGlobalActorClosures() {
let _: Int = acceptAsyncClosure { @SomeGlobalActor in
Expand All @@ -480,6 +482,15 @@ func testGlobalActorClosures() {
}

acceptConcurrentClosure { @SomeGlobalActor in 5 } // expected-warning {{converting function value of type '@SomeGlobalActor @Sendable () -> Int' to '@Sendable () -> Int' loses global actor 'SomeGlobalActor'}}

@MainActor func test() async {
let closure = { @MainActor @Sendable in
MainActor.assertIsolated()
}

await crossIsolationBoundary(closure)
// expected-warning@-1 {{converting function value of type '@MainActor @Sendable () -> ()' to '() -> Void' loses global actor 'MainActor'; this is an error in the Swift 6 language mode}}
}
}

@available(SwiftStdlib 5.1, *)
Expand Down