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-5252] Warning: Cast from … to unrelated type false positive? #47827

Closed
swift-ci opened this issue Jun 19, 2017 · 11 comments
Closed

[SR-5252] Warning: Cast from … to unrelated type false positive? #47827

swift-ci opened this issue Jun 19, 2017 · 11 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-5252
Radar rdar://problem/33702548
Original Reporter hannesoid (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Version 9.0 and 9.1 beta

Additional Detail from JIRA
Votes 18
Component/s Compiler
Labels Bug
Assignee @slavapestov
Priority Medium

md5: 36726ef60ce123c2e833d053b96ea84d

is duplicated by:

  • SR-6353 Cast from to unrelated type always fails but doesn't fail
  • SR-6771 "Cast always fails" false positive warning when casting to a generic subclass

Issue Description:

I get a warning, which I think is a false positive, or am I missing something?

The warning arrives as soon as I constrain the Generic Type parameter to comply to a certain protocol. There should imho be no warning, as the attempted cast casts to a (generic) subclass of the given one.

For a few examples compile the attached swift file with Xcode 9 beta 1, swift 4.
Or look at this snippet:

import UIKit

public protocol PropsConsumer: class {
    associatedtype Props
    func update(props: Props)
}

public final class TableViewContainerCell<ContentViewType: UIView>: UITableViewCell {}

extension TableViewContainerCell where ContentViewType: PropsConsumer {
    func update(props: ContentViewType.Props) {}
}

// MARK: - Surprising warnings

// simplified
func update3<ViewType: UIView & PropsConsumer>(cell: UITableViewCell, viewType: ViewType.Type) -> Bool {
    // this generates a warning: Cast from 'UITableViewCell' to unrelated type 'TableViewContainerCell<ViewType>' always fails
    guard let castCell = cell as? TableViewContainerCell<ViewType> else {
        return false
    }
    // do sth with the cast cell…
    castCell.backgroundColor = UIColor.white
    return true
}
@belkadan
Copy link
Contributor

@slavapestov, look familiar?

@swift-ci
Copy link
Collaborator Author

Comment by Hannes Oud (JIRA)

Still getting this in Xcode 9.0 beta 3

@swift-ci
Copy link
Collaborator Author

swift-ci commented Aug 3, 2017

Comment by Hannes Oud (JIRA)

@slavapestov @belkadan Still gettting this in Xcode 9.0 beta 4

@belkadan
Copy link
Contributor

belkadan commented Aug 3, 2017

@swift-ci create

@swift-ci
Copy link
Collaborator Author

Comment by Erik Kerber (JIRA)

I get the same in Xcode 9 and 9.1 beta. Similarly, I observe this when making a generic subclass of UICollectionViewCell.

@swift-ci
Copy link
Collaborator Author

Comment by Igor Silva (JIRA)

It is still happening on 9.1 beta. It's definitely a bug, since "unreachable code" (according to the warning) runs.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Mar 3, 2019

Comment by Joanna Carter (JIRA)

This is still happening in Xcode Version 10.1 (10B61).

The problem is definitely connected to the joining of two types, as in this thread on the Swift forums:

https://forums.swift.org/t/generics-causing-pain-yet-again/21156?u=joanna_carter

It might not show up in a playground

@AnthonyLatsis
Copy link
Collaborator

This is fixed on master, apparently without direct intention. I'm going to add a regression test just in case.
The reproduction code can be reduced to this:

protocol P {}
class Helper {}

class Base {}
class Sub<T>: Base {}

// The subclass existential constraint was the culprit.
func foo<T: Helper & P>(base: Base, arg: T) {
    _ = base as? Sub<T>
}

@AnthonyLatsis
Copy link
Collaborator

#27431

@AnthonyLatsis
Copy link
Collaborator

"Suddenly fixed" 🙂

@swift-ci
Copy link
Collaborator Author

swift-ci commented Oct 1, 2019

Comment by Hannes Oud (JIRA)

@AnthonyLatsis great, glad to hear and thanks for following up on this!!

@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
Projects
None yet
Development

No branches or pull requests

3 participants