@available doesn't affect literal initialization

I stumbled across a seemingly rough edge with availability annotations:

struct Foo {
    let bar: String
}

@available(*, deprecated)
extension Foo: ExpressibleByStringLiteral {
    init(stringLiteral value: StringLiteralType) {
        self.bar = value
    }
}

let a: Foo = Foo(stringLiteral: "") // warning: 'init(stringLiteral:)' is deprecated
let b: Foo = "" // no warning

It even compiles fine when using unavailable instead of deprecated.

I tried searching the forums but nobody seems to have mentioned it. I'm not sure if this behavior is intended or if it's an oversight in the compiler.

Any thoughts would be appreciated.

IMO, this seems like a bug; could you please file an issue on GitHub?

1 Like

Thank you for the fast response. I filed it on GitHub.

Update: it's now fixed in main! [Sema] Perform availability checks in literals initializers by LucianoPAlmeida · Pull Request #61805 · apple/swift · GitHub

1 Like