Await required on sub-class initialiser call. Why?

Any idea why the call to PlatformImage(data:) needs to be marked await, whereas call to UIImage(data:) does not? PlatformImage is a UIImage and no UIImage initialisers are marked async
The Playground example is contrived, of course, but distills the essentials of something in my code

import Foundation
import SwiftUI

// Need to add @unchecked Sendable, otherwise get this message:
//      Class 'PlatformImage' must restate inherited '@unchecked Sendable' conformance
class PlatformImage: UIImage, @unchecked Sendable {
}

let d = Data()

Task {
    let i = UIImage(data: d)

    // The line below gives message:
    //    Expression is 'async' but is not marked with 'await'; this is an error in the Swift 6 language mode
    // Have to add `await` to call to dismiss warning, but call is not async AFAIK
    let pi = PlatformImage(data: d)
}

Your problem is not present for me in Xcode 16.3 or 16.4.

1 Like

Forgot to say, the playground needs to be set to Swift 5. Despite the warning saying "this is an error in the Swift 6 language mode", there is no warning or error in Swift 6.

I can see in both 16.3 and 16.4 beta on two different machines.

Compiler bug I guess?

Swift 5 has a lot of features disabled, that completely change compiler’s reasoning about concurrency. Wording about Swift 6 mode to raise an error here might be outdated somewhere, and confuse. But it is totally possible that what has given a warning in Swift 5, then completely go away in Swift 6. I recommend turning on region-based isolation and main actor inference features for the Swift 5 mode with concurrency.

I've tried your snippet in an “App Playground” in Xcode 16.3 in both Swift 5 (a swiftLanguageVersions: [.v5]) and Swift 6 (.v6) in Xcode 16.3 and neither produced the warning you shared with us. Then, again, your code snippet suggests you have top-level code (which doesn't make sense in an “App Playground” project).

Or are you doing this in a standalone .playground? (And if so, how are you specifying Swift versions? Changing toolchains?)

Can you help us reproduce your problem?

I'm using a playground in Xcode (File->New->Playground...) and the Blank iOS template.
Xcode 16.4, at least, defaults the playground to Swift 6. You can change Swift version by opening the File Inspector panel for the playground.

There isn't any other code. This warning is something I found in a Swift 5 app project, which I have distilled down to just this example.

The warning is not displayed if using a macOS platform playground

Thanks for clarifying.

Yes, I now see the warning you describe. And if you expand the warning, it explains why it thinks you need await, namely because it inferred main actor isolation:

It evidently incorrectly concluded that the inherited initializer for PlatformImage was isolated, whereas the one for UIImage is not.

Interestingly, you say that you experience this problem in a “Swift 5 app project”, not just a playground. I cannot reproduce the problem there.