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-2919] optional method of protocol implemented in child class do not get called #45513

Closed
swift-ci opened this issue Oct 11, 2016 · 5 comments
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-2919
Radar None
Original Reporter mreichenbach (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: a8e5045579f682f39718f2d78d858e23

Issue Description:

Apologies if this is being filed in the wrong place.

I'd expect both implementations to behave the same way. However, implementation 1 never calls the method in class B. Noticed this when porting my project to swift 3.

import UIKit
// IMPLEMENTATION 1
class A: NSObject, UITableViewDelegate {
    var tableView = UITableView()
    override init() {
        super.init()
        tableView.delegate = self
    }
}

class B: A {
    var count = 0
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        count += 1
    }
}

let b = B()
b.tableView.delegate!.tableView?(b.tableView, didSelectRowAt: IndexPath())
b.count // => 0

// IMPLEMENTATION 2
class C: NSObject, UITableViewDelegate {
    var tableView = UITableView()
    override init() {
        super.init()
        tableView.delegate = self
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    }
}

class D: C {
    var count = 0
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        count += 1
    }
}

let d = D()
d.tableView.delegate!.tableView?(b.tableView, didSelectRowAt: IndexPath())
d.count // => 1

I tried to simplify the example more by providing my own protocol and "table" class, but I couldn't get it to behave the same way again. This leads me to believe that the observed behavior in this example is not expected and actually a bug.

@belkadan
Copy link
Contributor

@DougGregor, you fixed this, right?

@DougGregor
Copy link
Member

Yes, this was done here:

7e3c13c

@swift-ci
Copy link
Collaborator Author

Comment by Fernando Mazzon (JIRA)

This appears to have been regressed in Swift 5. See linked SO post for more information by several people that have ran into this: https://stackoverflow.com/a/55393950/1943064 . The issue randomly appears and goes away when making changes to the affected file. Once built however, the issue either persists 100% of the time or doesn't exist at all, so it has to be a compile time issue. I've had instances where one subclass was affected and other wasn't. This is pretty serious as swift 5 is deemed production ready.

@slavapestov
Copy link
Member

I just fixed this – #24256

@swift-ci
Copy link
Collaborator Author

Comment by Fernando Mazzon (JIRA)

@slavapestov awesome! From the linked pull request I gather this should be in whatever version of xCode has swift 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
Projects
None yet
Development

No branches or pull requests

4 participants