Skip to content

Compiler rejects protocol compositions involving protocols with primary associated types, but only for existentials #81295

@karwa

Description

@karwa

Description

I wanted to write the type any Foo<Int> & Bar, where Foo is a protocol with a primary associated type and Bar is another protocol. For some reason the compiler disallows this with any but allows it with some.

For example, this function is okay:

doSomething(_: some Foo<Int> & Bar) {}

But this isn't

doSomething(_: any Foo<Int> & Bar) {}

// Error: non-protocol, non-class type 'Foo<Int>' cannot be used within a protocol-constrained type

Similarly:

// Works
typealias Baz = Foo<Int> & Bar

// Neither of these work
let _: any Foo<Int> & Bar
let _: any (Foo<Int> & Bar)

// Works
let _: any Baz

Reproduction

protocol Foo<T> {
    associatedtype T
}

protocol Bar {}

func doSomething(_: some Foo<Int> & Bar) {}
func doSomething(_: any Foo<Int> & Bar) {} // Error: non-protocol, non-class type 'Foo<Int>' cannot be used within a protocol-constrained type

Expected behavior

Expect that any and some are supported equally when it comes to the constraints they are attached to, and more specifically that any Foo<Int> & Bar is allowed.

Environment

Swift version 6.2-dev (LLVM 978186dad468cc4, Swift 2d7b7a7)
Target: x86_64-unknown-linux-gnu

Additional information

No response

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 typesprotocol compositionsFeature → types: protocol composition typesswift 6.2triage neededThis issue needs more specific labelsunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions