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-11042] Incorrect cast fix-it when type does not conform to a protocol #53431

Closed
theblixguy opened this issue Jun 28, 2019 · 8 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@theblixguy
Copy link
Collaborator

Previous ID SR-11042
Radar None
Original Reporter @theblixguy
Type Bug
Status Closed
Resolution Done
Environment

Swift 5.1, Xcode 11 beta 2

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee None
Priority Medium

md5: 712350f192dab89c0863b1de05963282

relates to:

  • SR-4630 Fixit for adding a protocol conformance when a type is compatible, but not declared so

Issue Description:

Example:

protocol Foo {}
class Bar {}

let anyFoo: Foo = Bar() // fix-it: Insert ' as! Foo'

The fix-it is incorrect and will cause a runtime crash.

@belkadan
Copy link
Contributor

I mean, it's possible there's a retroactive conformance…but yeah.

@theblixguy
Copy link
Collaborator Author

Maybe we can remove the fix-it to be safe (or make it a conditional cast instead)? or use the `isRetroactive()` check in ProtocolConformance.cpp?

@belkadan
Copy link
Contributor

I mean it's possible there's a conformance in a module that hasn't been imported (usually a downstream module, I'd imagine). A conditional cast is a bad idea because either the types won't match up or it will silently do the wrong thing if the type really doesn't conform. Run-time crashes are good when they represent programmer error.

…but not over compile-time errors, so yeah, we could just omit the fix-it when it's casting a concrete type to a protocol. Doesn't prevent someone from writing it explicitly. @xedin, what do you think?

@theblixguy
Copy link
Collaborator Author

Sounds good! Although I can't figure out where the fix-it is coming from, because there's nothing in `

MissingContextualConformanceFailure::diagnoseAsError()` (which is emitting the diagnostic) that is emitting the fix-it. It must be coming from somewhere else...

@theblixguy
Copy link
Collaborator Author

Also, it doesn't show up in command line (but does in Playgrounds). @xedin do you know where this fix-it is coming from?

@xedin
Copy link
Member

xedin commented Jul 3, 2019

@theblixguy My best guess is that this is Playgrounds with an old compiler (I can reproduce it with 5.1 toolchain) but it doesn't reproduce on master. It is probably one of the diagnostics which has been ported on master but didn't make it to 5.1 branch.

@theblixguy
Copy link
Collaborator Author

Oh that’s great! Would be nice to cherry pick it to 5.1 if possible. I’ll close this ticket now.

@xedin
Copy link
Member

xedin commented Jul 3, 2019

Unfortunately that's already too late for it to get into 5.1

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

3 participants