Availability macro support for back-deployed Span

This commit is contained in:
Guillaume Lessard committed 2026-01-23 18:38:58 -08:00
1 parent 81fded4dd9
commit 0f949fecf3
4 files changed
+83 -66

No files matched your search

+40 -46
View File
@@ -12,63 +12,57 @@
import PackageDescription
struct Available {
struct Availability {
var name: String
var version: String
var osAvailability: String
var sourceAvailability: String
init(
_ version: String,
_ osAvailability: String
) {
self.name = "System"
self.version = version
self.osAvailability = osAvailability
self.sourceAvailability = "macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, visionOS 1.0"
var minOSVersions: String {
"macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, visionOS 1.0"
}
var spanOSVersions: String {
"macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1.0"
}
var swiftSetting: SwiftSetting {
#if SYSTEM_ABI_STABLE
// Use availability matching Darwin API.
let availability = self.osAvailability
#else
// Use availability matching SwiftPM default.
let availability = self.sourceAvailability
#endif
return .enableExperimentalFeature(
"AvailabilityMacro=\(self.name) \(version):\(availability)")
}
var isStandardAvailability: Bool { name == "System" }
}
let availability: [Available] = [
Available("0.0.1", "macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0"),
let availabilityMap: [(String, String)] = [
("0.0.1", "macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0"),
("0.0.2", "macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0"),
("1.1.0", "macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4"),
("1.2.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4"),
("1.4.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"),
Available("0.0.2", "macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0"),
Available("0.0.3", "macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4"),
Available("1.1.0", "macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4"),
Available("1.1.1", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4"),
Available("1.2.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4"),
Available("1.2.1", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4"),
Available("1.3.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4"),
Available("1.3.1", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"),
Available("1.3.2", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"),
Available("1.4.0", "macOS 14.4, iOS 17.4, watchOS 10.4, tvOS 17.4, visionOS 1.0"),
Available("1.4.1", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
Available("1.4.2", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
Available("1.5.0", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
Available("1.6.0", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
Available("1.6.1", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
Available("99", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
("99", "macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, visionOS 9999"),
]
let swiftSettingsAvailability = availability.map(\.swiftSetting)
let availabilities: [Availability] =
availabilityMap.map {
Availability(name: "System", version: $0.0, osAvailability: $0.1)
} + availabilityMap.prefix(5).map {
Availability(name: "SystemWithSpan", version: $0.0, osAvailability: $0.1)
}
let swiftSettingsAvailability = availabilities.map {
availability -> SwiftSetting in
let osVersionList: String
#if SYSTEM_ABI_STABLE
// Use availability matching Darwin API.
availability = availability.osAvailability
#else
if availability.isStandardAvailability {
// Use availability matching SwiftPM minimum.
osVersionList = availability.minOSVersions
} else {
// Use availability matching Span deployment minimum.
osVersionList = availability.spanOSVersions
}
#endif
return .enableExperimentalFeature(
"AvailabilityMacro=\(availability.name) \(availability.version):\(osVersionList)"
)
}
#if SYSTEM_CI
let swiftSettingsCI: [SwiftSetting] = [
+11 -4
View File
@@ -20,6 +20,7 @@ extension FileDescriptor {
/// If `body` throws an error
/// or an error occurs while closing the file descriptor,
/// this method rethrows that error.
@available(System 0.0.1, *)
public func closeAfter<R>(_ body: () throws -> R) throws -> R {
// No underscore helper, since the closure's throw isn't necessarily typed.
let result: R
@@ -49,6 +50,7 @@ extension FileDescriptor {
/// If `sequence` doesn't implement
/// the <doc://com.apple.documentation/documentation/swift/sequence/3128824-withcontiguousstorageifavailable> method,
/// temporary space will be allocated as needed.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
@discardableResult
public func writeAll<S: Sequence>(
@@ -98,6 +100,7 @@ extension FileDescriptor {
/// If `sequence` doesn't implement
/// the <doc://com.apple.documentation/documentation/swift/sequence/3128824-withcontiguousstorageifavailable> method,
/// temporary space will be allocated as needed.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
@discardableResult
public func writeAll<S: Sequence>(
@@ -106,6 +109,7 @@ extension FileDescriptor {
try _writeAll(toAbsoluteOffset: offset, sequence).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _writeAll<S: Sequence>(
toAbsoluteOffset offset: Int64, _ sequence: S
@@ -115,6 +119,7 @@ extension FileDescriptor {
}
}
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
internal func _writeAllBuffer(
toAbsoluteOffset offset: Int64, _ buffer: UnsafeRawBufferPointer
@@ -134,6 +139,7 @@ extension FileDescriptor {
return .success(buffer.count)
}
#if compiler(>=6.2)
/// Writes the entire contents of a buffer, retrying on partial writes.
///
/// - Parameters:
@@ -150,7 +156,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `write`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func writeAll(
_ data: RawSpan,
@@ -177,7 +183,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pwrite`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func writeAll(
toAbsoluteOffset offset: Int64,
@@ -205,7 +211,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `read`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
@discardableResult
public func read(
@@ -238,7 +244,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pread`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
@discardableResult
public func read(
@@ -257,4 +263,5 @@ extension FileDescriptor {
}
return originalCapacity - buffer.freeCapacity
}
#endif // swift(>=6.2)
}
+21 -5
View File
@@ -23,6 +23,7 @@ extension FileDescriptor {
/// - Returns: A file descriptor for the open file
///
/// The corresponding C function is `open`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public static func open(
_ path: FilePath,
@@ -62,6 +63,7 @@ extension FileDescriptor {
/// - Returns: A file descriptor for the open file
///
/// The corresponding C function is `open`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public static func open(
_ path: UnsafePointer<CChar>,
@@ -75,6 +77,7 @@ extension FileDescriptor {
).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal static func _open(
_ path: UnsafePointer<CChar>,
@@ -148,6 +151,7 @@ extension FileDescriptor {
/// the object will be deactivated.
///
/// The corresponding C function is `close`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public func close() throws { try _close().get() }
@@ -165,6 +169,7 @@ extension FileDescriptor {
/// in bytes from the beginning of the file.
///
/// The corresponding C function is `lseek`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
@discardableResult
public func seek(
@@ -173,6 +178,7 @@ extension FileDescriptor {
try _seek(offset: offset, from: whence).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _seek(
offset: Int64, from whence: FileDescriptor.SeekOrigin
@@ -210,6 +216,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `read`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public func read(
into buffer: UnsafeMutableRawBufferPointer,
@@ -218,6 +225,7 @@ extension FileDescriptor {
try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _read(
into buffer: UnsafeMutableRawBufferPointer,
@@ -246,6 +254,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pread`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public func read(
fromAbsoluteOffset offset: Int64,
@@ -259,6 +268,7 @@ extension FileDescriptor {
).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _read(
fromAbsoluteOffset offset: Int64,
@@ -299,6 +309,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `write`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public func write(
_ buffer: UnsafeRawBufferPointer,
@@ -307,6 +318,7 @@ extension FileDescriptor {
try _write(buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _write(
_ buffer: UnsafeRawBufferPointer,
@@ -332,6 +344,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pwrite`.
@available(System 0.0.1, *)
@_alwaysEmitIntoClient
public func write(
toAbsoluteOffset offset: Int64,
@@ -341,6 +354,7 @@ extension FileDescriptor {
try _write(toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt).get()
}
@available(System 0.0.1, *)
@usableFromInline
internal func _write(
toAbsoluteOffset offset: Int64,
@@ -366,6 +380,7 @@ extension FileDescriptor {
retryOnInterrupt: retryOnInterrupt)
}
#if compiler(>=6.2)
/// Writes the contents of a buffer at the current file offset.
///
/// - Parameters:
@@ -382,7 +397,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `write`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func write(
_ data: RawSpan,
@@ -408,7 +423,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pwrite`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func write(
toAbsoluteOffset offset: Int64,
@@ -420,7 +435,7 @@ extension FileDescriptor {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
internal func _readIntoOutputBuffer(
_ buffer: UnsafeMutableRawBufferPointer,
@@ -459,7 +474,7 @@ extension FileDescriptor {
/// call the ``seek(offset:from:)`` method.
///
/// The corresponding C function is `read`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func read(
into buffer: inout OutputRawSpan,
@@ -485,7 +500,7 @@ extension FileDescriptor {
/// this method leaves the file's existing offset unchanged.
///
/// The corresponding C function is `pread`.
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@_alwaysEmitIntoClient
public func read(
fromAbsoluteOffset offset: Int64,
@@ -509,6 +524,7 @@ extension FileDescriptor {
fatalError("Unexpected error type")
}
}
#endif // compiler(>=6.2)
}
#if !os(WASI)
+11 -11
View File
@@ -22,7 +22,7 @@ private struct SpanFileIOTests {
// MARK: - Basic Functionality
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func basicReadWriteOperations() async throws {
try withTemporaryFilePath(basename: "basicReadWriteOperations") { path in
let fd = try FileDescriptor.open(
@@ -95,7 +95,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func writeOperations() async throws {
try withTemporaryFilePath(basename: "writeOperations") { path in
let fd = try FileDescriptor.open(
@@ -131,7 +131,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(macOS 10.14.4, iOS 12.2, watchOS 5.2, tvOS 12.2, visionOS 1, *)
@Test func readOperations() async throws {
try withTemporaryFilePath(basename: "readOperations") { path in
let fd = try FileDescriptor.open(
@@ -162,7 +162,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func absoluteOffsetOperations() async throws {
try withTemporaryFilePath(basename: "absoluteOffsetOperations") { path in
let fd = try FileDescriptor.open(
@@ -198,7 +198,7 @@ private struct SpanFileIOTests {
// MARK: - Semantic Behavior
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func partialReadBehavior() async throws {
try withTemporaryFilePath(basename: "partialReadBehavior") { path in
let fd = try FileDescriptor.open(
@@ -232,7 +232,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func readFillingBehavior() async throws {
try withTemporaryFilePath(basename: "readFillingBehavior") { path in
let fd = try FileDescriptor.open(
@@ -271,7 +271,7 @@ private struct SpanFileIOTests {
// MARK: - Edge Cases
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func emptyIO() async throws {
try withTemporaryFilePath(basename: "emptyIO") { path in
let fd = try FileDescriptor.open(
@@ -308,7 +308,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func eofBehavior() async throws {
try withTemporaryFilePath(basename: "eofBehavior") { path in
let fd = try FileDescriptor.open(
@@ -348,7 +348,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func preInitializedSpan() async throws {
try withTemporaryFilePath(basename: "preInitializedSpan") { path in
let fd = try FileDescriptor.open(
@@ -384,7 +384,7 @@ private struct SpanFileIOTests {
}
}
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func errorHandling() throws {
// Test 1: Reading from closed FD
let fd1 = try FileDescriptor.open(FilePath("/dev/null"), .readOnly)
@@ -436,7 +436,7 @@ private struct SpanFileIOTests {
// MARK: - Regression/Examples
@available(macOS 15, iOS 18, watchOS 11, tvOS 18, visionOS 2, *)
@available(SystemWithSpan 0.0.1, *)
@Test func proposalExample() async throws {
// Example from the proposal
try withTemporaryFilePath(basename: "proposalExample") { path in