swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.67k stars 10.39k forks source link

Implicitly open implicitly unwrapped existential optionals #74652

Open groue opened 5 months ago

groue commented 5 months ago

Motivation

Since SE-0352, the language can implicitly open existentials, and this has tremendously improved the support for generics.

Implicitly Unwrapped Optionals (IUOs) were forgotten, though. The following code does not compile, and this is a bad surprise for developers:

protocol P { }

func use(_ p: some P) { }

struct S {
    var iuo: P!
    var p: P
    init() { fatalError("irrelevant") }

    func test() {
        use(p)   // OK
        use(iuo) // ❌ Type 'any P' cannot conform to 'P'
    }
}

This bad surprise can happen in the interaction between a library and its client code:

In such a case, the library user complains to the library author about the compiler error discussed above. The library author investigates, and eventually replies that the real issue lies with the compiler (this issue). Everybody has lost time and energy. In the end, the library user needs to workaround the compiler issue and uglify their code with extra properties or local variables:

// workaround
let p: P = iuo
use(p)

Proposed solution

Please complete the support for SE-0352, and make sure implicitly unwrapped optionals are handled as expected by their users.

I recently reported a distinct but similar issue: #72727. It looks like the importance of implicitly unwrapped optionals is under-evaluated. Please do not forget them.

Alternatives considered

No response

Additional information

No response

groue commented 4 months ago

Bug still present in Xcode 16.0 beta 3 (16A5202i)