Skip to content

[6.2] Allow existential parameterized compositions: any P<A> & Q #81750

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

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented May 23, 2025

6.2 cherry-pick of: #76705

  • Description: While technically a new feature, in reality we didn't always reject compositions involving parameterized protocol types, and users discovered they could work around the restriction with a type alias. I've been fixing the missing support needed to make this work gradually over time, so now the only missing piece was ASTDemangler support for such types. Implement the ASTDemangler support and remove the Sema diagnostic that bans this construction.

  • Scope of the issue: This has been coming up more and more with people wanting to write stuff like any Sequence<Int> & Sendable, etc.

  • Tested: New tests added.

  • Risk: Low.

  • Radar: rdar://96960993

  • Issue: Enable constrained existential types in protocol compositions #63877

  • Reviewed by: @AnthonyLatsis

@slavapestov slavapestov requested a review from a team as a code owner May 23, 2025 18:25
@slavapestov slavapestov changed the title Existential parameterized composition 6.2 Allow existential parameterized compositions: any P<A> & Q May 23, 2025
@slavapestov slavapestov changed the title Allow existential parameterized compositions: any P<A> & Q [6.2] Allow existential parameterized compositions: any P<A> & Q May 23, 2025
@slavapestov
Copy link
Contributor Author

@swift-ci Please test

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

Comment on lines +802 to +804
auto found = primaryAssociatedTypes.find(assocTy->getName());
if (found == primaryAssociatedTypes.end())
return protoTy;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how this return appears to be correct regardless of args.empty(). Might be worth a test:

protocol P<A> { 
  associatedtype A 
}
protocol Q<A, B> {
  associatedtype A
  associatedtype B
}

func test(_: any P & Q<Int, Float>) {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't even think of that. I'll add the test case on main in a separate PR.

@slavapestov slavapestov merged commit c690603 into swiftlang:release/6.2 May 27, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants