Skip to content

[5.8] Collection literal inferred as Any when function type argument is Any #63982

@keith

Description

@keith

With this code:

func asJSON(_ a: [String: Any]) { print(a) }

asJSON([
    "a": [
        "b": "c",
        "d": [
            "e": "f",
        ],
    ],
])

Swift 5.8 from Xcode 14.3 beta 2 warns that the collection is inferred as Any:

foo.swift:4:10: warning: heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional
    "a": [
         ^

In this case I'm surprised to see this warning since the function it's being passed to explicitly takes [String: Any], so it being inferred as Any seems expected and I don't think needs a warning

Metadata

Metadata

Assignees

Labels

bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of ImplementationexpressionsFeature: expressionsliteralsFeature → expressions: Literals such as an integer or string literalswift 5.9type checkerArea → compiler: Semantic analysistype inferenceFeature: type inferenceunexpected warningBug: Unexpected warning

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions