How do Swift 5.1 Opaque Result Types interact with older OSs (e.g. iOS 12)

Viewed 1412

In WWDC 2019 session 402 "What's New in Swift", the speaker, when discussing the Swift 5.1 feature Opaque Result Type (SE-0244), mentions that the feature will only work on new OSes:

Requires new Swift runtime support

    Available on macOS Catalina, iOS 13, tvOS 13, watchOS 6 and later

    Guard uses with availability checking when deploying to earlier OS releases

In Xcode 11, I don't get any build errors (or warnings) if I write code using this feature, when targeting iOS 11 and up. I haven't wrapped any of the code in if #available(iOS 13.0, *) checks. E.g.:

protocol Shape { }
class Square: Shape { }
class Triangle: Shape { }
    
func foo() -> some Shape {
    return Square()
}

and then calling foo() from some code in my app.

What happens if this code runs on pre-iOS 13 devices? Is the lack of build error itself an error? Is there a definitive list of which Swift 5.1 features require new runtime support, and thus a specific OS version?

1 Answers

Is the lack of build error itself an error?

Yes. And it is clearly documented. As the release notes clearly tell you:

Declarations with some Protocol return types require the Swift 5.1 runtime in iOS 13, macOS 10.15, watchOS 6, or tvOS 13, but the Swift compiler doesn’t enforce this. Running an app that uses some return types on previous operating system versions might crash at runtime... Workaround: Only deploy binaries that use some return types to iOS 13, macOS 10.15, watchOS 6, and tvOS 13. Avoid them in code that must run on previous operating system versions.

So, do what you are told: use availability guard or prepare to die.

EDIT This bug is now fixed, meaning that the availability of some return types is enforced by the compiler:

enter image description here

Related