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-11402] False positive 'cast from unrelated type' warning when casting a protocol type to a superclass of a conforming type #53803

Closed
harlanhaskins opened this issue Aug 30, 2019 · 5 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 5.0 type checker Area → compiler: Semantic analysis

Comments

@harlanhaskins
Copy link
Collaborator

Previous ID SR-11402
Radar None
Original Reporter @harlanhaskins
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 5.0Regression, TypeChecker
Assignee @theblixguy
Priority Medium

md5: 0622edaf1bff979bfbed97c83c6635e5

Issue Description:

The following code:

protocol MyProtocol {}
class Superclass {} // Superclass does not conform to MyProtocol
class Subclass: Superclass, MyProtocol {} // Subclass conforms to MyProtocol

func f(_ x: MyProtocol) { // Expects existential of type MyProtocol
  if let y = x as? Superclass { // Superclass is 'unrelated', but cast still succeeds
    print(y)
  }
}

f(Subclass())

Produces the following diagnostic:

/tmp/bad-diag.swift:6:16: warning: cast from 'MyProtocol' to unrelated type 'Superclass' always fails
  if let y = x as? Superclass {
             ~ ^   ~~~~~~~~~~

But at runtime, prints:

main.Subclass
@theblixguy
Copy link
Collaborator

This is due to SR-6400, however what we can do is relax this diagnostic if we're performing a conditional cast.

@theblixguy
Copy link
Collaborator

The diagnostic is correct though because superclass does not conform to the protocol and we can statically determine that in this case. But yeah, maybe we can relax this if it's a conditional cast.

cc @belkadan @xedin what do you think?

@harlanhaskins
Copy link
Collaborator Author

It seems like, because subclasses can conform to this protocol, we should be extra conservative and not emit this diagnostic for classes. We could enable it still for public classes when we statically know none of the subclasses down the whole hierarchy conform, but that gets tricky too.

@xedin
Copy link
Member

xedin commented Aug 30, 2019

I think what @harlanhaskins is proposing makes sense in this case.

@theblixguy
Copy link
Collaborator

Okay, I have fixed it here: #26971

@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 regression swift 5.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

4 participants