Apparent compiler bug relating to Optional witnessing failable initializer requirement

I think this must be a bug. The following test passes if the Optional initializer is declared as a failable initializer, but fails if the ? is removed from the Optional initializer.

The Test

@Test
func foo() {
    #expect(
        Demo<Optional<Int>>.test()
    )
}
struct Demo<T: Foo> {
    static func test() -> Bool {
        if let a = T.init() {
            return true
        }
        return false
    }
}
protocol Foo {
    init?()
}

Passes If Optional Conforms Like This:

extension Optional: Foo {
    init?() {
        self = Self.none
    }
}

Fails If Optional Conforms Like This:

extension Optional: Foo {
    init() {
        self = Self.none
    }
}
2 Likes

I filed a bug report: Apparent compiler bug relating to Optional witnessing failable initializer requirement · Issue #78409 · swiftlang/swift · GitHub

1 Like