I'm trying to better understand whether Task
s 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.