Skip to content
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

Merged
merged 9 commits into from
Aug 26, 2021
Merged

SE-0309: Unlock existential types for all protocols #33767

merged 9 commits into from
Aug 26, 2021

Conversation

theblixguy
Copy link
Collaborator

@theblixguy theblixguy commented Sep 2, 2020

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.

@theblixguy
Copy link
Collaborator Author

@swift-ci please test

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Sep 2, 2020

@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.

@theblixguy
Copy link
Collaborator Author

@AnthonyLatsis Yeah, sure, I’m open to collaboration!

@theblixguy
Copy link
Collaborator Author

Not bad, only 9 tests had to be updated. I have added some additional type checker and execution tests.

@theblixguy
Copy link
Collaborator Author

@swift-ci please smoke test

@theblixguy theblixguy changed the title [DNM] [WIP] Lift the 'Self or associated type' restriction [DNM] Lift the 'Self or associated type' restriction Sep 2, 2020
@swiftlang swiftlang deleted a comment from swift-ci Sep 2, 2020
@swiftlang swiftlang deleted a comment from swift-ci Sep 2, 2020
@theblixguy theblixguy added the swift evolution pending discussion Flag → feature: A feature that has a Swift evolution proposal currently in review label Sep 2, 2020
@theblixguy
Copy link
Collaborator Author

theblixguy commented Sep 2, 2020

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}}
Copy link
Contributor

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

Copy link
Member

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.

Copy link
Collaborator

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.

lib/Sema/CSApply.cpp Outdated Show resolved Hide resolved
lib/AST/Decl.cpp Outdated Show resolved Hide resolved
@shahmishal
Copy link
Member

Please update the base branch to main by Oct 5th otherwise the pull request will be closed automatically.

  • How to change the base branch: (Link)
  • More detail about the branch update: (Link)

@AnthonyLatsis AnthonyLatsis changed the base branch from master to main October 1, 2020 06:24
AnthonyLatsis added a commit to AnthonyLatsis/swift that referenced this pull request Oct 12, 2020
This will enable us to allow members containing references to Self or associated types
only in covariant position to be used on an existential
AnthonyLatsis added a commit that referenced this pull request Oct 13, 2020
AST: Refactor SelfReferenceKind in preparation for #33767
@Mordil
Copy link

Mordil commented Apr 14, 2021

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)

@AnthonyLatsis
Copy link
Collaborator

@Mordil Sure, we will build a toolchain as soon as we push our downstreams.

@Mordil
Copy link

Mordil commented Apr 14, 2021

@AnthonyLatsis ❤️ thank you!

}

// Bound generic types are invariant.
if (auto boundGenericType = type->getAs<BoundGenericType>()) {
Copy link

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!

Copy link
Collaborator

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.

@AnthonyLatsis AnthonyLatsis changed the title [DNM] Lift the 'Self or associated type' restriction SE-0309: Unlock existential types for all protocols Apr 25, 2021
@jckarter jckarter added the swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process label May 4, 2021
@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please smoke test macOS

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please test Windows

@ktoso
Copy link
Contributor

ktoso commented Aug 19, 2021

@swift-ci please test Windows Platform

@AnthonyLatsis
Copy link
Collaborator

@swift-ci please test Windows

@Saklad5
Copy link

Saklad5 commented Aug 23, 2021

Is this on-track to make it into Swift 5.5?

@ktoso
Copy link
Contributor

ktoso commented Aug 26, 2021

Checked in with @jckarter who was review manager for the evolution proposal -- let's go ahead and merge this 🎉

Awesome work @AnthonyLatsis, thanks!

@ktoso ktoso merged commit f96057e into swiftlang:main Aug 26, 2021
kavon added a commit to kavon/swift that referenced this pull request Aug 26, 2021
…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.
kavon added a commit that referenced this pull request Aug 26, 2021
@kavon
Copy link
Member

kavon commented Aug 26, 2021

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 main the 10 days since the tests passed, or maybe the smoke test didn't catch it (try a full test?). Please try merging it again after fixing that test 😃 . I'm not sure if that requires making a new PR or not?

@ktoso
Copy link
Contributor

ktoso commented Aug 27, 2021

Oh no! Thanks for catching it @kavon !

I'm not sure if that requires making a new PR or not?

New PR is likely best.

@AnthonyLatsis
Copy link
Collaborator

Drat! Looking into this...

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Aug 30, 2021

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 foo() -> [Self], with covariant Self nested inside a known-covariant stdlib type such as an array or dictionary, because they involve a consecutive intrinsic collection cast apply that needs to be found and replaced too, or a thunk partial_apply when stuff like [Self] appears in other covariant positions. I've tried looking into fixing this, but it's my first time digging into the optimizer, and I really need a bit of guidance. For now, I've made the test work with @_optimize(none)#39095. @jckarter At this conjecture, should we be reverting #34140 first?

cc: @ktoso

@ktoso
Copy link
Contributor

ktoso commented Sep 2, 2021

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 👍

@slavapestov
Copy link
Contributor

@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.

@AnthonyLatsis
Copy link
Collaborator

@slavapestov Sure, thanks for the go-ahead. I initially thought the code there was too performance-sensitive for a defensive type traversal.

@theblixguy theblixguy deleted the chore/remove-self-or-associated-type-diagnostic branch September 10, 2021 12:13
@ZsoltMolnarMBH
Copy link

Dear contributors, (@theblixguy)

I cannot seem to figure out wether this change is still in the main branch or not.
What is the status of this feature?
When should we expect to put this to use?

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented Oct 15, 2021

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
swift evolution approved Flag → feature: A feature that was approved through the Swift evolution process
Projects
None yet
Development

Successfully merging this pull request may close these issues.