Skip to content

Array needs explicit type to compile #62726

Open
@sarastro-nl

Description

@sarastro-nl

Describe the bug
swift gives a compile error about incompatible array types whereas the type should be inferred from the context.

Steps To Reproduce
Try to compile this:

extension Array where Element == Int {
    static let foo: [String] = Array(["foo"])
}

It complains with Cannot assign value of type 'Array<Int>' to type '[String]'

It compiles fine when adding an explicit type:

extension Array where Element == Int {
    static let foo: [String] = Array<String>(["foo"])
}

Expected behavior
It compiles fine without an error. The type [String] should be inferred but instead the compiler thinks we're talking about [Int] since we're in an extension of [Int].

Environment (please fill out the following information)

  • OS: macOS 12.6
  • Xcode 14.1
  • swift: swift-driver version: 1.62.15 Apple Swift version 5.7.1 (swiftlang-5.7.1.135.3 clang-1400.0.29.51)

Metadata

Metadata

Assignees

No one assigned

    Labels

    TypeResolverbugA 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