Skip to content

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

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

Open
karwa opened this issue May 5, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@karwa
Copy link
Contributor

karwa commented May 5, 2025

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

@karwa karwa added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 5, 2025
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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant