Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
keith opened this issue Mar 1, 2023 · 10 comments · Fixed by #65318, #65319 or #65320
Closed

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

keith opened this issue Mar 1, 2023 · 10 comments · Fixed by #65318, #65319 or #65320
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions literals Feature → expressions: Literals such as an integer or string literal swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected warning Bug: Unexpected warning

Comments

@keith
Copy link
Member

keith commented Mar 1, 2023

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

@keith keith added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 1, 2023
@xedin
Copy link
Contributor

xedin commented Mar 1, 2023

The type of dictionary is inferred to [String: Any] before it's converted to the parameter type. The warning has been made more aggressive by #60323. cc @AnthonyLatsis

@xedin
Copy link
Contributor

xedin commented Mar 1, 2023

I'm not sure how I feel about this myself, on one side this seems reasonable because collection is indeed heterogeneous but on other other hand we could consider parameter to be an explicit type annotation as well.

@keith
Copy link
Member Author

keith commented Mar 1, 2023

on other other hand we could consider parameter to be an explicit type annotation as well

yea that's what I was expecting

@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation type checker Area → compiler: Semantic analysis literals Feature → expressions: Literals such as an integer or string literal unexpected warning Bug: Unexpected warning swift 5.9 and removed triage needed This issue needs more specific labels labels Mar 1, 2023
@AnthonyLatsis AnthonyLatsis self-assigned this Mar 1, 2023
@AnthonyLatsis
Copy link
Collaborator

After looking at 1d21b4e, which introduced this diagnostic, I think I might have misinterpreted its purpose. I agree with Keith that whether the literal has a defaulted type should not matter if it gets type-erased anyway. On its turn, an erasure expression implies the presence of an explicit type annotation somewhere.

@ian-sayles
Copy link

ian-sayles commented Apr 5, 2023

Since upgrading to Xcode 14.3 I I get the following warning, which I think maybe related:
Heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional

My app uses Firestore and as many statements like below which are impacted by the new change:

 assetDocument.setData([
                "alerts": [
                    "service": [
                        "end": Timestamp(date: newDate),
                        ],
                    ],
            ], merge: true) { err in
                if let err = err {
                    print("Error updating document: \(err)")
                }
            }

Does anyone know if this is a bug that will be fixed in the next release of Xcode or do I have to find a fix to the issue that does not break my app?

@xedin
Copy link
Contributor

xedin commented Apr 5, 2023

Should the changes be reverted then, @AnthonyLatsis?

@AnthonyLatsis
Copy link
Collaborator

Yes, I will see if there is anything useful worth keeping while there’s still time until 5.9 converges. Do you want me to fully revert it for upcoming 5.8 patches now?

@xedin
Copy link
Contributor

xedin commented Apr 5, 2023

Let’s revert on main and 5.9.

@AnthonyLatsis AnthonyLatsis added the type inference Feature: type inference label Apr 19, 2023
@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Apr 19, 2023

@lattner Do you remember why you added this diagnostic (1d21b4e)? Was it to avoid semantic source breaks caused by potential improvements to the type join?

@lattner
Copy link
Contributor

lattner commented Apr 20, 2023

no recollection sorry

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation expressions Feature: expressions literals Feature → expressions: Literals such as an integer or string literal swift 5.9 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected warning Bug: Unexpected warning
Projects
None yet
5 participants