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-10832] Generic encapsulating type erases optional protocol methods #53222

Closed
swift-ci opened this issue Jun 4, 2019 · 1 comment
Closed
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 Jun 4, 2019

Previous ID SR-10832
Radar None
Original Reporter lhunath (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: e6f20fa7ecaf987311c0b5bf2b9a7c8c

duplicates:

  • SR-9479 Subclasses of generic classes don't remember @objc names for optional protocol methods

Issue Description:

Consider:

import Foundation

@objc
protocol Protocol {
    @objc
    optional func one() -> Int
    @objc
    optional func two() -> Int
}

public class Outer<M> {
    public class Inner: Protocol {
        public func two() -> Int {
            return 0
        }
    }
    public class Extended: Inner {
        public func one() -> Int {
            return 1
        }
        public override func two() -> Int {
            return 1
        }
    }
}

let object : Protocol = Outer<String>.Extended()
print( "one: ", object.one?() ) // one:  nil
print( "two: ", object.two?() ) // two:  Optional(1)

The fact that the Outer type is generic causes the Extended object's one function to become inaccessible. The object's two function remains available only because it overrides a function declared at the level where the Protocol is declared. If the Protocol declaration moves from the Inner to the Extended level, the one function will become available again.

Somehow, this peculiar behaviour is triggered explicitly by the fact that Outer is a generic type. Removing the generic qualifier from Outer restores sanity to the situation.

@belkadan
Copy link
Contributor

belkadan commented Jun 5, 2019

Thanks for the reduced test case! I think I know what's going on: the compiler is trying not to infer @objc for a generic method, but in this case the method isn't generic beyond the self type, which should be okay (as noted by Inner working fine).

@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

2 participants