Skip to content
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

Swift Concurrency inappropriately flags main-actor bound code in non-Sendable closure as not running on main actor #75003

Open
dfed opened this issue Jul 5, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features

Comments

@dfed
Copy link

dfed commented Jul 5, 2024

Description

@MainActor-bound code that declares a function inside a non-escaping, non-Sendable anonymous closure fails to compile if the innermost function interacts with the @MainActor unless the innermost function is declared @MainActor.

According to #72382 (comment), a non-sendable closure can never leave the isolation domain it's formed in, and therefore if a non-sendable closure is declared in a @MainActor-bound context, the non-sendable closure should also be @MainActor-bound.

This issue makes utilizing anonymous closures in code-gen in code where strict concurrency code is turned on problematic.

Reproduction

@MainActor
func doSomethingOnMainActor() {}

@MainActor
func testCallingDoSomethingOnMainActor() {
    func doSomething() {
        doSomethingOnMainActor() // Compiles just fine, as expected.
    }
    _ = {
        doSomethingOnMainActor() // Compiles just fine, as expected.
    }()
    func declareDoSomethingInNestedFunction() {
        func doSomething() {
            doSomethingOnMainActor() // Compiles just fine, as expected.
        }
    }
    _ = {
        func doSomething() {
            // The following line should compile, because we are in a method within an anonymous non-escaping, non-sendable closure that is `@MainActor`-bound, which is semantically similar to the `declareDoSomethingInNestedFunction()` function declaration above.
            doSomethingOnMainActor() // Error: Call to main actor-isolated global function 'doSomethingOnMainActor()' in a synchronous nonisolated context
        }
    }()
}

Expected behavior

The code compiles

Environment

Reproduces on both:

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
\swift-driver version: 1.110 Apple Swift version 6.0 (swiftlang-6.0.0.4.52 clang-1600.0.21.1.3)
Target: arm64-apple-macosx14.0

Additional information

No response

@dfed dfed added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jul 5, 2024
@hborla hborla added concurrency Feature: umbrella label for concurrency language features and removed triage needed This issue needs more specific labels labels Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. concurrency Feature: umbrella label for concurrency language features
Projects
None yet
Development

No branches or pull requests

2 participants