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-1786] Wrong behaviour of protocol extension static methods #44395

Closed
swift-ci opened this issue Jun 16, 2016 · 3 comments
Closed

[SR-1786] Wrong behaviour of protocol extension static methods #44395

swift-ci opened this issue Jun 16, 2016 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

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

Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.31)
Target: x86_64-apple-macosx10.9

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

md5: e06491478ba3becdc45a66a1787453eb

duplicates:

  • SR-617 Self not always resolved dynamically with Generics

Issue Description:

There seems to be an issue in the way static methods are dynamically dispatched when a protocol extension is involved.

IF the inferred type of a variable is the protocol:

  • AND the method is defined in the original protocol THEN the runtime type’s implementation is called, irrespective of whether there is a default implementation in the extension.

  • AND the method is not defined in the original protocol, THEN the default implementation is called.

  • ELSE IF the inferred type of the variable is the type THEN the type’s implementation is called.

When static variables are involved, the behaviour seems to be the opposite.
Consider the following code:

protocol Reusable: class {
    static var reuseID: String { get }
}

extension Reusable {
    static var reuseID: String { return String(Self) }
}

func register<T where T: Reusable>(cell: T.Type) {
    print(cell)
    print(cell.reuseID)
}

class Cell: Reusable {}
class SubCell: Cell {}

register(SubCell.self)

This prints:

SubCell
Cell

instead of the expected

SubCell
SubCell

What's more, commenting out the reuseID declaration on the Reusable protocol (leaving only the implementation in the extension) seems to fix the problem.

@belkadan
Copy link
Contributor

I think specifically it's your use of Self instead of self that makes the difference here. But I'm not 100% sure what the correct behavior is. @rjmccall, @jckarter?

@jckarter
Copy link
Member

`T == SubCell` seems like the right thing either way. I'd expect the same behavior Fabio does.

@slavapestov
Copy link
Member

I fixed this recently.

@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

4 participants