[Pitch] Feature Availability Conditions for Toolchain Libraries

This reminded me of the Custom Availability Domain pitch, so one way this could roughly look using that feature would be:

// Somewhere in Swift Testing
@availabilityDomain(ExitTests)
@const
#if os(macOS) || os(Linux) || os(FreeBSD) || os(OpenBSD) || os(Windows) // note that this list is now maintained in one place, the original package
var exitTestsAvailable = true
#else
var exitTestsAvailable = false
#endif

// ...

@freestanding(expression)
@available(ExitTests)
public macro expect(
  processExitsWith: ExitTest.Condition,
  performing: @escaping @Sendable () async throws -> Void
) -> ExitTest.Result?

then in the downstream consumers the usage simply becomes like this

// In a downstream package's test file
#if compiler(>=6.2)
@Test
@available(ExitTests)
func testExitBehavior() async {
    await #expect(processExitsWith: .failure) {
        fatalError("expected failure")
    }
}
#endif

you can also use if #available(ExitTests) { ... } anywhere you like and the compiler should be able to constant fold the related branch at compile-time.


I'm not sure what happened to the original pitch, but this additional use case might be able to revive the disscusion around that feature

1 Like