Skip to content

[Typechecker] Warn when casting a function type to an existential or archetype type #22822

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

Merged
merged 4 commits into from
Mar 7, 2019
Merged

Conversation

theblixguy
Copy link
Collaborator

This PR extends the unrelated downcast warning to cast from a function type to an existential type or a archetype type. If the referenced function has a non-void return type, then a fix-it is offered to call the function instead.

Resolves SR-6022.
Resolves rdar://problem/25723751

@theblixguy
Copy link
Collaborator Author

cc @jrose-apple

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not true. A function type can be unconditionally cast to an 'Any'.

It can also be conditionally cast to a generic parameter with no constraints; the cast will succeed if that generic parameter happens to be bound to an identical function type at runtime.

@theblixguy
Copy link
Collaborator Author

theblixguy commented Feb 22, 2019

@slavapestov Oh right, I should update my comment. If you cast it to Any, then you won't hit this check, because that would've already been handled before this code is called, but I think I should still add a check for !toType->isAny() anyway just to be safe!

This is only for a force-cast and not an conditional cast, but I can extend it to cover the latter scenario.

@slavapestov
Copy link
Contributor

Can you add an assert that you don't get an 'Any' here, and make sure the tests cover the correct behavior?

Conditional-cast and force-cast have the same semantics regarding what casts can ever succeed or not.

So a conditional-cast of a function type to a non-Any existential should also warn that it will always fail.

Similarly, both a force-cast and a conditional-cast of a function type to an unconstrained archetype is OK.

@jrose-apple
Copy link
Contributor

AnyObject should also behave like Any, since it's basically "Any but box it up if you have to".

@jrose-apple jrose-apple requested a review from xedin February 22, 2019 21:37
@theblixguy
Copy link
Collaborator Author

@slavapestov Done, does it look good to you?

@theblixguy
Copy link
Collaborator Author

theblixguy commented Feb 22, 2019

@jrose-apple That's already handled, so you will get the forced cast from '() -> Any' to 'AnyObject' always succeeds; did you mean to use 'as'? diagnostic.

@theblixguy theblixguy changed the title [typechecker] warn when casting a function type to an existential or archetype type [Typechecker] Warn when casting a function type to an existential or archetype type Feb 23, 2019
@jrose-apple
Copy link
Contributor

Running tests for you, still waiting on Slava for final OK.

@swift-ci Please test

@theblixguy
Copy link
Collaborator Author

Thanks Jordan, all tests have passed 🎉

@slavapestov @xedin does this look okay to you?

@theblixguy
Copy link
Collaborator Author

Bump. Is this good to merge now?

@theblixguy
Copy link
Collaborator Author

@slavapestov Can you invoke the CI again?

@xedin
Copy link
Contributor

xedin commented Mar 5, 2019

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Mar 5, 2019

Build failed
Swift Test Linux Platform
Git Sha - 77300c3

@swift-ci
Copy link
Contributor

swift-ci commented Mar 5, 2019

Build failed
Swift Test OS X Platform
Git Sha - 77300c3

@theblixguy
Copy link
Collaborator Author

theblixguy commented Mar 6, 2019

OSX build failed with <unknown>:0: error: no group info found for file: 'main.swift' 🤔 Weird. Could someone re-run the OSX test?

@slavapestov
Copy link
Contributor

@swift-ci Please smoke test macOS

@theblixguy
Copy link
Collaborator Author

The smoke test passed! Do we need to run a full OSX test as well or is this good to merge without it?

@slavapestov slavapestov merged commit 2e2c12f into swiftlang:master Mar 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants