-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
SE-0309: Unlock existential types for all protocols #33767
SE-0309: Unlock existential types for all protocols #33767
Conversation
@swift-ci please test |
@theblixguy Do you mind if we combine this with swiftlang/swift-evolution#1170 as an evolution proposal? I reached out to Joe yesterday as soon as I realized it would be easier to lift the limitation at once, apart from handling the "known associated type" case. |
@AnthonyLatsis Yeah, sure, I’m open to collaboration! |
Not bad, only 9 tests had to be updated. I have added some additional type checker and execution tests. |
@swift-ci please smoke test |
I have added various test cases (and updated existing ones), but please let me know if there's anything I have missed. |
|
||
let p1: P1 = S1() | ||
_ = p1.returnSelf() // ok | ||
_ = p1.returnAssoc() // expected-error {{member 'returnAssoc' cannot be used on value of protocol type 'P1'; use a generic constraint instead}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be really great if we could change this to something more detailed like member 'returnAssoc' cannot be used on value of protocol type 'P1'; because it returns associated type 'Q', it may only be used on a concrete conforming type
, since if the proposal is accepted users will encounter it much more often
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On that note, it might be better to update the related educational note to explain why these members can't be used, rather than remove the educational note altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out there's already at least one use of Self
that's allowed, which is difficult to explain. I've left #33736 as a draft so that the educational note can evolve in parallel with these changes.
This will enable us to allow members containing references to Self or associated types only in covariant position to be used on an existential
AST: Refactor SelfReferenceKind in preparation for #33767
Could we get a toolchain built for this so we can play with it? (I wasn't sure if anyone can trigger the CI to build one or not) |
@Mordil Sure, we will build a toolchain as soon as we push our downstreams. |
@AnthonyLatsis ❤️ thank you! |
lib/AST/ASTContext.cpp
Outdated
} | ||
|
||
// Bound generic types are invariant. | ||
if (auto boundGenericType = type->getAs<BoundGenericType>()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m trying to figure out how it all fits together (out of sheer curiosity 😛) but I can’t seem to find the part that implements the special-case covariant Optional.Wrapped
, Array.Element
, and Dictionary.Value
type parameters. As far as I can understand, this function classifies these as invariant (just like any other bound generic type). Is the special-case covariance in this (or a future) PR? Thank you, and thank you too for the helpful inline documentation!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Special-case covariance is already implemented on the main
branch. This branch needs a rebase.
… educational note
…rtedProtocolType()
…colSelfReferences»
…xistential member access
@swift-ci please smoke test |
@swift-ci please smoke test macOS |
@swift-ci please test Windows |
@swift-ci please test Windows Platform |
@swift-ci please test Windows |
Is this on-track to make it into Swift 5.5? |
Checked in with @jckarter who was review manager for the evolution proposal -- let's go ahead and merge this 🎉 Awesome work @AnthonyLatsis, thanks! |
…ve-self-or-associated-type-diagnostic" The following regression test added for this feature is not passing: Swift(linux-x86_64) :: decl/protocol/protocols_with_self_or_assoc_reqs_executable.swift with a compiler crash happening during SILFunctionTransform "Devirtualizer". Reverting to unblock CI. This reverts commit f96057e, reversing changes made to 3fc18f3.
Revert "Merge pull request #33767 ..."
I reverted this PR only because one of the tests added for this feature was failing right after being merged. See PR #39067 for details. Something must have changed on |
Oh no! Thanks for catching it @kavon !
New PR is likely best. |
Drat! Looking into this... |
Not good... The crash is unrelated to this change, but it happens this bug was still introduced by me in #34140 (sorry!). The problem is that the SIL optimizer is failing to devirtualize a call to a protocol requirement like cc: @ktoso |
Sounds like reverting #34140 and getting you some help/guidance to get it right would be good. Joe is vacationing now but will be back soon, maybe @slavapestov can offer some guidance? And we could merge #39095 in the meantime 👍 |
@AnthonyLatsis in the mean time can we tweak the optimizer to not attempt to devirtualize such calls? Then you can land the real fix later at your leisure, without blocking anything else. |
@slavapestov Sure, thanks for the go-ahead. I initially thought the code there was too performance-sensitive for a defensive type traversal. |
Dear contributors, (@theblixguy) I cannot seem to figure out wether this change is still in the main branch or not. |
This specific change is merged, and will be included in the next Swift release, whenever that happens. There is a series of changes yet to come to fully implement the proposal, though (see this thread for details). |
Implementation for https://forums.swift.org/t/lifting-the-self-or-associated-type-constraint-on-existentials/18025
This PR removes the existing "Self or associated type" restriction. Luckily, the implementation involves only removing some code & we already have the right set of restrictions in place to disallow "invalid" code.