Skip to content

[Swift 5.8] Unexpected error "Cannot inherit from protocol type with generic argument" #66740

Open
@ensan-hcl

Description

@ensan-hcl

Description
In Swift 5.8, when attempting to inherit from a protocol with primary associated types specified and providing a typealias for the associated type, the code fails to compile with the error "Cannot inherit from protocol type with generic argument 'P'."

Steps to reproduce

  1. Define a protocol P with a primary associated type T.
  2. Attempt to create a struct F that inherits from P<Int> and provide a typealias for the associated type.
protocol P<T> {
    associatedtype T
}

// Cannot inherit from protocol type with generic argument 'P<Int>'
struct F: P<Int> {}

Expected behavior
The code should compile without any errors, as mentioned in SE-0346

In the inheritance clause of a concrete type, for example:

struct Lines : Collection<String> { ... }

In this position it is sugar for specifying the associated type witness, similar to explicitly declaring a typealias:

struct Lines : Collection {
  typealias Element = String
}

Environment

  • Swift compiler version info:
    swift-driver version: 1.75.2 Apple Swift version 5.8.1 (swiftlang-5.8.0.124.5 clang-1403.0.22.11.100)
    Target: x86_64-apple-macosx13.0

  • Xcode version info:
    Xcode 14.3.1
    Build version 14E300c

  • Deployment target: N/A

Other Information

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.parameterized protocolsFeature → protocol: protocols with primary associated typesprotocolFeature → type declarations: Protocol declarationsswift 5.8type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions