When building a package with Xcode 26 beta I get now a couple of concurrency related warnings where the Swift 6.1 compiler was happy with.
I was able to fix some of those by conforming a protocol to Sendable. However, I'm actually not totally convinced that this should be required, but as it didn't hurt in my use case, this is probably for another topic.
Here, I will show the issue where I suspect that the warning is not justified, and where I don't have a solution:
protocol P: Sendable {
associatedtype State: P2
}
protocol P2 {}
@MainActor
final class Foo<T: P> {
private(set) var state: T.State
init(initialState: T.State) {
self.state = initialState
Task {
try await self.run(keyPath: \.state)
^ Warning: Capture of non-sendable type 'T.State.Type' in an isolated closure
}
}
private func run(
keyPath: ReferenceWritableKeyPath<Foo, T.State>
) async throws {
}
}
Note: the Swift 6.2 compiler does want protocol P
to conform to Sendable, otherwise I do get more warnings (these are the cases, which I can easily fix in my use case)
Important:
If associatedtype State
in protocol P
will not be constrained to protocol P2
, i.e.:
protocol P: Sendable {
associatedtype State
}
then, the compiler does not emit the warning in this line:
try await self.run(keyPath: \.state)
So, what would be the correct way to resolve the warning?
Is the warning correct/reasonable, and if yes, how would one benefit from it?
(If it should turn out in cases where the code above is used, that there is actually a concurrency issue, the compiler should be able to emit an error anyway, IMHO)
A more reduced example:
Summary
protocol P2 {}
@MainActor
final class Foo<State: P2> {
private(set) var state: State
init(initialState: State) {
self.state = initialState
Task {
try await self.run(keyPath: \.state)
}
}
private func run(
keyPath: ReferenceWritableKeyPath<Foo, State>
) async throws {
}
}