Fix builds using Swift 5 language mode with newer compilers (#497)

### Motivation:

We're using `#if swift(...)` in some places, which breaks builds using
Swift 5 language mode with Swift 6.3+ compilers.

### Modifications:

- Check `compiler(<6.4)` instead of `swift(<6.4)`.
- Use `@_lifetime(self: copy self)` on the setter, which 6.2, 6.3, and
6.4 all accept.

### Result:

Build succeeds when compiled with `-Xswiftc -swift-version -Xswiftc 5`
on 6.2, 6.3, and 6.4.
This commit is contained in:
Si Beaumont authored and GitHub committed 2026-11-03 13:15:54 +00:00
1 parent 6ff0a3b147
commit f8fad2f95a
1 file changed
+2 -11
+2 -11
View File
@@ -18,29 +18,20 @@ import FoundationEssentials
import Foundation
#endif
#if swift(<6.4)
#if compiler(<6.4)
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
extension ContiguousBytes {
func withBytes<R>(_ body: (RawSpan) throws -> R) rethrows -> R {
try withUnsafeBytes { try body(RawSpan(_unsafeBytes: $0)) }
}
}
#endif
#if swift(<6.4)
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0, *)
extension MutableRawSpan {
#if swift(>=6.3)
subscript(_ byteOffset: Int) -> UInt8 {
get { unsafeLoad(fromByteOffset: byteOffset, as: UInt8.self) }
@_lifetime(self: copy self)
set { storeBytes(of: newValue, toByteOffset: byteOffset, as: UInt8.self) }
}
#elseif swift(<6.3)
subscript(_ byteOffset: Int) -> UInt8 {
get { unsafeLoad(fromByteOffset: byteOffset, as: UInt8.self) }
@_lifetime(&self)
set { storeBytes(of: newValue, toByteOffset: byteOffset, as: UInt8.self) }
}
#endif
}
#endif