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-10615] Delegation From Protocol Extension With Base Class Calls Incorrect Method #53015

Closed
swift-ci opened this issue May 3, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 3, 2019

Previous ID SR-10615
Radar None
Original Reporter trevorthoele (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 10.2

Swift 5

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

md5: 84059db668155a2baefc0c5c9b308a6d

duplicates:

  • SR-10482 Incorrect method dispatch with multiple (overlapping) constrained protocol extensions when invoked on subclass of conforming type inside generic function

Issue Description:

If you have a base class conforming to a protocol, then conform to a separate protocol in the derived class which overrides functionality in the base class' protocol, calling the methods directly on that class results in the correctly overridden method to be called, but as soon as you delegate from a method in the protocol chain, you always get the most basic protocol method.

In the below, I would expect it to print "one", "two", "three" every time.

protocol P1 {
    func t()
}


extension P1 {
    func t() {
        print("one")
    }
    
    func q() {
        self.t()
    }
}


protocol P2 : P1 {}


extension P2 {
    func t() {
        print("two")
    }
}


protocol P3 : P1 {}


extension P3 {
    func t() {
        print("three")
    }
    
    func q() {
        self.t()
    }
}


class A : P1 {}
class B : A, P2 {}
class C : A, P3 {}
class D : P1 {}
class E : P2 {}
class F : P3 {}


func test() {
    let a = A()
    let b = B()
    let c = C()
    
    // one
    a.q()
    // one
    b.q()
    // one
    c.q()
    
    print("---")
    
    // one
    a.t()
    // two
    b.t()
    // three
    c.t()
    
    print("---")
    
    let d = D()
    let e = E()
    let f = F()
    
    // one
    d.q()
    // two
    e.q()
    // three
    f.q()
    
    print("---")
    
    // one
    d.t()
    // two
    e.t()
    // three
    f.t()
}
@belkadan
Copy link
Contributor

belkadan commented May 7, 2019

This is expected behavior. See the discussion on SR-10482 for more information.

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

No branches or pull requests

2 participants