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-11993] Possible to create different protocol witness table function calling from same type. #54427

Open
swift-ci opened this issue Jan 6, 2020 · 2 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

swift-ci commented Jan 6, 2020

Previous ID SR-11993
Radar rdar://problem/58455455
Original Reporter tarunon (JIRA User)
Type Bug
Environment

Xcode Version 11.1 (11A1027)
Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)

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

md5: 7ff2d04eb9e0e0bae53cab422092055a

Issue Description:

Next code can make different function calling from same type.
If passing the Type from generics, if the Type apply to associatedtype, then protocol witness table create different way.

protocol A {}
protocol P {
    associatedtype T
    var value: T { get }
    func tIsA() -> Bool
}


extension P {
    func tIsA() -> Bool {
        false
    }
}


extension P where T: A {
    func tIsA() -> Bool {
        true
    }
}


struct X: A {}
struct AnyP<T>: P {
    var value: T
}


func check(x: X) {
    print(AnyP(value: x).tIsA())
}


func check2<X2>(x: X2) {
    print(AnyP(value: x).tIsA())
}


check(x: X()) // true
check2(x: X()) // false
@beccadax
Copy link
Contributor

beccadax commented Jan 9, 2020

@swift-ci create

@DougGregor
Copy link
Member

This is behaving as designed, although one could imagine other designs. Protocol conformances select the most specific function to call that is guaranteed to work for all specializations of the type. Because AnyP's T argument is not guaranteed to conform to A (it's not a requirement), that means the conformance has to pick the tIsA that returns false. When we have full type information, we can pick something more specific.

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

No branches or pull requests

3 participants