Files
swift-mirror/test/Serialization/Inputs/opaque_with_limited_availability.swift
Pavel Yaskevich 3c03449797 [MiscDiagnostics] OpaqueTypes: Handle unrelated #available conditions gracefully
`OpaqueUnderlyingTypeChecker` should skip conditional availability
blocks if none of the conditions restrict the current target.

Resolves: rdar://103445432
2023-01-04 15:36:46 -08:00

81 lines
1.6 KiB
Swift

public protocol P {
func hello()
}
public struct Empty : P {
public func hello() { print("Hello from Empty") }
}
public struct Tuple<T>: P {
public init(_ tuple: T) {}
public func hello() { print("Hello from Tuple") }
}
@available(macOS 10.15, *)
struct Named : P {
public func hello() { print("Hello from Named") }
}
@resultBuilder
public struct Example {
public static func buildOptional<T: P>(_ v: T?) -> some P {
if #available(macOS 100.0.1, *) {
let result = v!
result.hello()
return result
} else {
let result = Empty()
result.hello()
return result
}
}
public static func buildLimitedAvailability<T: P>(_ component: T) -> some P {
component
}
public static func buildBlock<T: P>(_ components: T) -> T {
return components
}
public static func buildBlock<T1: P, T2: P>(_ v1: T1, _ v2: T2) -> Tuple<(T1, T2)> {
return Tuple((v1, v2))
}
}
public func test() -> some P {
if #available(macOS 100.0.1, *) {
return Tuple<(Int, Int)>((0, 0))
}
return Empty()
}
public func testUnavailable() -> some P {
if #unavailable(macOS 100.0.1) {
return Tuple<(Int, Int)>((0, 1))
}
return Empty()
}
public func test_return_from_conditional() -> some P {
if #available(macOS 10.15, *) {
return Named()
}
return Tuple<(String, Int)>(("", 0))
}
// This used to crash during serialization because
// @available negates availability condition.
@available(macOS, unavailable)
public func testUnusable() -> some P {
if #available(iOS 50, *) {
return Named()
}
return Tuple<(String, Int)>(("", 0))
}