We've been trying out -Xfrontend -warn-concurrency
, and I'm getting a warning that I don't understand.
class SendabilityTester {
var name: String = ""
@MainActor
func test() {
print("name outside: \(name)")
Task {
// Capture of 'self' with non-sendable type 'SendabilityTester' in a `@Sendable` closure
print("name inside: \(name)")
}
}
}
SendabilityTester
itself is not Sendable, nor is it isolated to @MainActor
. However, as I understand it, the nested Task
here is going to inherit @MainActor
isolation from the surrounding function. We don't get a warning when accessing self.name
outside the Task, and it seems like it should be equally safe to access name
from within another @MainActor
-isolated task.
The warning the compiler emits is accurate in a sense—we are capturing self
within a @Sendable
closure, and self
is not marked as Sendable. But should this warning be disabled when declared within an actor-isolated function, if the closure itself is known to also run on that same actor?