Skip to content

@available doesn’t affect literal initialization #61564

@davdroman

Description

@davdroman

Describe the bug
When a literal initializer is marked with an availability annotation, it's seemingly ignored by the compiler at the point of use, unless the initializer is used explicitly.

Steps To Reproduce

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

Expected behavior
Both assignments should cause a deprecation warning.

Note: every other availability status (e.g. unavailable) is also ignored.

Screenshots

Screenshot 2022-10-13 at 11 29 21

Metadata

Metadata

Assignees

No one assigned

    Labels

    availabilityThe @available attribute and availability checking in generalbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselftype checkerArea → compiler: Semantic analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions