Navigation Menu

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-10053] Protocols extending class types are not recognized as class types #52456

Closed
swift-ci opened this issue Mar 6, 2019 · 1 comment
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Mar 6, 2019

Previous ID SR-10053
Radar rdar://problem/48702830
Original Reporter jbfit (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Swift compiler 4.2-RELEASE

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

md5: 7098f9a0a7e244cda268053f1aa1d117

duplicates:

  • SR-55 non-@objc protocol existentials do not conform to their own protocol type

Issue Description:

class Alpha<T> where T: AnyObject {
    private let value: T
    init(_ value: T) {
        self.value = value
    }
}

protocol Beta: AnyObject {}

class Gamma: Beta {}

print(Alpha<Beta>(Gamma()))

Expected:

  • The above code should compile: The requirement on generic `T` is that is that it extends from `AnyObject`. Since protocol `Beta` also extends from `AnyObject` it holds that any class `Foo` that implements protocol `Beta` will also extend from `AnyObject`

Actual:

  • Code does not compile. Error message is error: "'Alpha' requires that 'Beta' be a class type"

Note: `class` and `AnyObject` are type-aliased to the same thing, so changing to `Beta: class` produces the same error

Also, this works as expected:

class Alpha<T> where T: AnyObject {
    private let value: T
    init(_ value: T) {
        self.value = value
    }
}
class Beta {}
class Gamma: Beta {}
print(Alpha<AnyObject>(Gamma())) 
@belkadan
Copy link
Contributor

This is unfortunately correct behavior, because a protocol value isn't just a reference to the object; it also tracks information about how the type conforms to the protocol. See SR-55 and its dups for more discussion.

@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 diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

2 participants