onekiloparsec / SwiftAA

The most comprehensive collection of accurate astronomical algorithms in (C++, Objective-C and) Swift.
http://www.onekiloparsec.dev/
MIT License
171 stars 31 forks source link

Prepare for Swift 6 strict concurrency: mark `NumericTypes` as `Sendable` #120

Open alex-vasenin opened 5 months ago

alex-vasenin commented 5 months ago

Swift 6 (which expected to be released Sep 2024) is going to have strict concurrency mode to prevent data races. While (AFAIK) SwiftAA does not uses concurrency itself, its clients might want to use some of its primitives in concurrent context. I tried to compile my current project with SWIFT_STRICT_CONCURRENCY = complete and I got a lots of warnings, because I was sending primitives like JulianDay, EquatorialCoordinates, Magnitude, etc between between actor boundaries (i.e. between threads).

While those types are safe to use concurrently because they are value types, they are not explicitly declared as Sendable, which triggers the warnings. Swift 6 declares that Sendable conformance must be declared in the same file where the type is declared, so you can't do it in an extension. So we need to mark types which safe to send across actors as Sendable in the package itself. It might be a little tricky, because the package still supports iOS12.0, i.e. Swift 4.2 and we are risking breaking backward compatibility.

As a workaround it is possible to mark those types in a client app as so: extension Magnitude: @unchecked Sendable {}