Skip to content

[SR-10053] Protocols extending class types are not recognized as class types #52456

@swift-ci

Description

@swift-ci
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())) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions