Skip to content
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

[SR-2921] Some implicitly coerced from <x> to Any warnings missing a source location #45515

Closed
swift-ci opened this issue Oct 12, 2016 · 24 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-2921
Radar rdar://problem/28722908
Original Reporter benasher44 (JIRA User)
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

macOS Sierra
Swift 3.0.1
Xcode 8.1b3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee benasher44 (JIRA)
Priority Medium

md5: 83cc0bca9622db0226c0caa1153a1c2b

relates to:

  • SR-2928 Collection casts from collections of optionals to collections of Any need custom handling

Issue Description:

The result from compiling a few different files in our project is warnings emitted in Xcode that look like this:

<unknown>:0: warning: expression implicitly coerced from 'String?' to Any
<unknown>:0: note: provide a default value to avoid this warning
<unknown>:0: note: force-unwrap the value to avoid this warning
<unknown>:0: note: explicitly cast to Any with 'as Any' to silence this warning
<unknown>:0: warning: expression implicitly coerced from 'String?' to Any
<unknown>:0: note: provide a default value to avoid this warning
<unknown>:0: note: force-unwrap the value to avoid this warning
<unknown>:0: note: explicitly cast to Any with 'as Any' to silence this warning
<unknown>:0: warning: expression implicitly coerced from 'String?' to Any
<unknown>:0: note: provide a default value to avoid this warning
<unknown>:0: note: force-unwrap the value to avoid this warning
<unknown>:0: note: explicitly cast to Any with 'as Any' to silence this warning

I've gone through and explicitly unwrapped as many String!/? s as I can identify, but I can't seem to get these to go away. I've tried coming up with a small repro case, since I can't attach the files in question, but so far I haven't figured out what triggers this. We have about 13 of them like this.

@swift-ci
Copy link
Collaborator Author

Comment by Ben A (JIRA)

Call sites that I can pick out easily that might trigger this seem to be:

  • dictionary literals where values are being coerced

  • dictionary subscripts where the values are being coerced

But as I said, I've tried explicitly unwrapping values in these cases to no avail.

@belkadan
Copy link
Contributor

@rudkx, CodaFi (JIRA User), can you think of where this would happen?

@rudkx
Copy link
Member

rudkx commented Oct 12, 2016

I saw a report of this yesterday, and in that case the issue was a dictionary [ String : UIColor? ] that was being passed to a function expecting [ String : Any ].

I'm not sure why the source location is wrong in that case.

@swift-ci
Copy link
Collaborator Author

Comment by Ben A (JIRA)

If any headway is made here, I'm happy to try out a snapshot or build a toolchain locally to try it out in Xcode to verify the fix. And of course I'll update here if I can come up with an isolated repro case.

@rudkx
Copy link
Member

rudkx commented Oct 12, 2016

I just figured out what the issue is (at least with the test case I mentioned) and will submit a patch soon.

@rudkx
Copy link
Member

rudkx commented Oct 12, 2016

PR that fixes the source locations: #5259

I opened SR-2928 to track fixing the warning messages and fixits.

@rudkx
Copy link
Member

rudkx commented Oct 12, 2016

Merged: 623a163

@rudkx
Copy link
Member

rudkx commented Oct 12, 2016

The source locations should now appear. Please re-open, or open a new ticket, if you find that it's not happening once you pick up a build that has the fix.

@swift-ci
Copy link
Collaborator Author

Comment by Ben A (JIRA)

@rudkx thanks!

@swift-ci
Copy link
Collaborator Author

Comment by Ben A (JIRA)

Tested with swift-DEVELOPMENT-SNAPSHOT-2016-10-13-a, and this is fixed now. Thanks!

@dmcgloin
Copy link
Mannequin

dmcgloin mannequin commented Nov 3, 2016

Not fixed for me using swift-DEVELOPMENT-SNAPSHOT-2016-11-01 (a)

@rudkx
Copy link
Member

rudkx commented Nov 3, 2016

@dmcgloin Do you have a test case that demonstrates the issue you're seeing?

@dmcgloin
Copy link
Mannequin

dmcgloin mannequin commented Nov 3, 2016

I will try to extract a simple example as soon as possible

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 3, 2016

Comment by Mitchell Clay (JIRA)

Noticed this error in XCode 8.1 release, sample code:

func nullToNil(value : Any?) -> Any? {
        if value is NSNull {
            return nil
        }
        return value
}

func parse(json: [String: Any]) {
    let childObject = json["child"] as! [String: Any?]
    let desc = nullToNil(childObject["description"]) as? String
}

I will get "Expression implicitly coerced from Any? to Any" on the "let desc" line

EDIT:

I have realized that this may not be a bug with current version more that it was a bug with previous version that is now fixed. The correct code is:

func nullToNil(value : Any?) -> Any? {
        if value is NSNull {
            return nil
        }
        return value
}

func parse(json: [String: Any]) {
    let childObject = json["child"] as! [String: *Any*]
    let desc = nullToNil(childObject["description"]) as? String
}

Because childObject["description"] is automatically considering the fact that the key may be wrong so a nil value could be returned, but for parsing JSON it will be a value or NSNull.

@dmcgloin
Copy link
Mannequin

dmcgloin mannequin commented Nov 4, 2016

Here's a snippet that produces a warning without source location.

This occurs with Xcode 8 version 8.1 (8B62) with default Swift 3.
It also occurs with Xcode 8 version 8.1 (8B62) with swift-DEVELOPMENT-SNAPSHOT-2016-10-13-a toolchain.

Please disregard the two warnings about using let instead of var.

    func doTest()
    {
        var color: String? = "Blue"
        var flavor: String? = "Vanilla"
        
        let additionalProperties = ["Color": color, "Flavor": flavor]
        self.logEvent("Something happened", additionalProperties: additionalProperties)
    }

    func logEvent(_ eventName: String, additionalProperties: [String: Any]? = nil)
    {
    }

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 5, 2016

Comment by Tal (JIRA)

does this appear to be caused by this bug. See attachment.

let message = messageFactory.message(meetHereType: meetHereType, image: image, conversation: conversation, meetLocation: meetLocation, senderLocation: senderLocation, receiverLocation: receiverLocation)
conversation.insert(message) { (error) in
print(error)
}
dismiss()

@swift-ci
Copy link
Collaborator Author

Comment by Benjamin Encz (JIRA)

Still happening for me in Xcode 8.1 (8B62).

One example:

        let string = NSMutableAttributedString(string: text)
        let linkAttributes =  [NSForegroundColorAttributeName: UIColor.plangridBlue()]
        string.setAttributes(linkAttributes, range: buttonRange)}

Happens in 5 different locations in our codebase.

@saagarjha
Copy link
Contributor

+1, on Xcode Version 8.2 (8C30a)

Very similar to @dmcgloin's issue–I'm seeing this happen with NSLayout.constraints(withVisualFormat:options:metrics:views:), which expects a [String: Any], when I'm passing in a [String: UITextView?].

@rudkx
Copy link
Member

rudkx commented Dec 11, 2016

Can someone who is hitting this try with a recent snapshot?

It appears to be fixed on master, e.g. for @dmcgloin's example I get:

  7:63: warning: expression implicitly coerced from 'String?' to Any
    self.logEvent("Something happened", additionalProperties: additionalProperties)

@saagarjha
Copy link
Contributor

I'm still getting this with the 3.0.2 PREVIEW 1 toolchain. Is there anything newer than this that I should try?

@rudkx
Copy link
Member

rudkx commented Dec 11, 2016

Yes, the December 9 snapshot here:

https://swift.org/download/#snapshots

@saagarjha
Copy link
Contributor

No luck with the 12/9 Snapshot 🙁

[EDIT]: Cleaned the project and tried again, and it's reporting the correct location now.

@swift-ci
Copy link
Collaborator Author

Comment by Danut Pralea (JIRA)

I keep having the warning, along with no location information 🙁

@belkadan
Copy link
Contributor

It's fixed in Swift 3.1 (Xcode 8.3). Any 3.0.x releases still have the bug.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 in itself type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants