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-10610] Generic Method Participates Strangely In Overload Resolution #53010

Open
swift-ci opened this issue May 2, 2019 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 2, 2019

Previous ID SR-10610
Radar None
Original Reporter trevorthoele (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 814d17451f959376bfe0bef5fb854f39

Issue Description:

It appears that even if the generic type ends up being more specific than a regular method (taking in the generic constraint as the actual type instead), the regular method is still the one that ends up being selected. In the below, I would expect the program to print "one", "two", "three".

Note: Changing the "two" method to be func f(_ p: P1 & P2) causes it to be selected instead of the "one" method.

protocol P1 {}
protocol P2 {}
protocol P3 {}


class Accepter {
    func f(_ p: P1) {
        print("one")
    }
    
    func f<T>(_ t: T) where T : P1 & P2 {
        print("two")
    }
    
    func f(_ p: P1 & P3) {
        print("three")
    }
    
    func f<T>(_ t: T) where T : P1 & P3 {
        print("four")
    }
}


class C1 : P1 {}
class C2 : P1 & P2 {}
class C3 : P1 & P3 {}


func test() {
    let accepter = Accepter()
    let c1 = C1()
    let c2 = C2()
    let c3 = C3()
    
    // one
    accepter.f(c1)
    // one
    accepter.f(c2)
    // three
    accepter.f(c3)
}
@belkadan
Copy link
Contributor

belkadan commented May 3, 2019

Hm. I think there's a general rule that non-generics are preferred over generics, but you're right that that doesn't make sense in the presence of protocol (existential) values with identical constraints. It could be source-breaking to change this, though.

cc @xedin, @DougGregor

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants