SE-0020 introduced the #if swift
compile-time conditional for Swift language versions.
However, Apple have repeatedly shipped newer compilers with old SDKs for macOS (including an older version of the standard library). That means that a condition such as #if swift(>=5.7)
can pass, even though the standard library is actually from 5.6 and may not have adopted language features introduced in 5.7.
Most recently, this occurred with primary associated types. The following code fails to compile with Xcode 14.0.1 on macOS, despite the #if swift
guard:
#if swift(>=5.7)
// Fails when building for macOS:
// error: cannot specialize protocol type 'Collection'
func foo() -> some Collection<Int> { ... }
#endif
This is technically allowed by SE-0020 (as it refers only to "language version"), but Swift developers will likely consider this behaviour undesirable. I would suggest that there are two ways to rectify it:
-
Introduce an
#if stdlib
compile-time conditional so we can distinguish between the availability of language features and standard library features (if we accept that they may not match in all toolchains), or -
Amend SE-0020 and require that the Swift language version match the standard library version. This would require a change in Apple's deployment practice and preclude macOS users from taking advantage of new language features until a matching SDK is ready, but that may also be an improvement. See:
While it is true that other parts of the Apple's SDKs have similar issues, and some sort of #if SDK
may also be warranted for compatibility with multiple versions of the SDK, I think the idea that #if swift
does not match the standard library version is a particularly severe situation and needs an urgent remedy.