Should Task inherit the execution context of an isolated parameter?

I'm trying to better understand whether Tasks spawned from a method that executes within the isolated context of a passed-in actor should inherit the execution context of the passed-in actor.

Theoretically, I believe the Task should inherit the execution context of the passed-in actor, since a not-detached Task "inherits the priority and actor context of the caller" (per the Task documentation). However, the compiler is telling me that my understanding is wrong, and I'm trying to understand if the compiler is exhibiting a bug or not.

Here's some simple code to illustrate the issue:

func execute<ActorType: Actor>(
    on isolatedActor: isolated ActorType,
    task: @Sendable (isolated ActorType) -> Void)
{
    // Compiler correctly allows this task to execute synchronously.
    task(isolatedActor)
    // Start a task that inherits the current execution context (i.e. that of the isolatedActor)
    Task {
        // POSSIBLE BUG: compiler insists that we must `await` the `task`, meaning that this Task is not executing in the isolatedActor's execution context
        task(isolatedActor) // Compiler error: Expression is 'async' but is not marked with 'await'
    }
}

And here's a screenshot of the same code with the inlined compiler error for folk who like Xcode's syntax hilighting:

I've seen this compiler error on Xcode 14.1 (14B47b) and Xcode 14.2 Release Candidate (14C18). I have not tried to compile the above on other Xcode versions.

3 Likes

Personally I think you're right that that'd be a fair thing to expect. I don't think this was ever implemented, worth opening an issue on GitHub - apple/swift: The Swift Programming Language - would you mind opening up an issue about this?

1 Like

Happy to file an issue! Will do later today once Iā€™m back at a computer :slightly_smiling_face:

1 Like

Closing the loop: this issue has been filed!

1 Like