Skip to content

Sema: Fix a couple of issues related to variance of protocol 'Self' #41757

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

Merged
merged 2 commits into from
Mar 10, 2022

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Mar 9, 2022

The first one is a fix for a regression from #41677. The second one is rdar://problem/74944514, which is a follow-up to #41545.

I'm wondering if the new variance check in the conformance checker, findExistentialSelfReferences() and Sema's doesMemberHaveUnfulfillableConstraintsWithExistentialBase() can all be unified somehow.

… for generic context

We don't want to pass in the outer generic signature here.

The base type's constraint type is written in terms of archetypes,
and we run generic signature queries against it with types appearing
in the protocol member. Since the protocol member has Self at
depth 0, index 0, prepending the outer generic signature to the
opened existential signature would produce incorrect results.
@slavapestov slavapestov requested review from hamishknight, CodaFi and AnthonyLatsis and removed request for hamishknight March 9, 2022 23:16
Comment on lines +589 to +596
// Make sure this also works in a generic context!
struct G<X, Y, Z> {
func doIt() {
let exist: any UnfulfillableGenericRequirements

exist.method8(false)
// expected-error@-1 {{member 'method8' cannot be used on value of type 'any UnfulfillableGenericRequirements'; consider using a generic constraint instead}}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, if only these declarations could just be anonymous.

…elated case

There are three kinds of invariant 'Self' uses here:

- 'Self' appears as the left hand side of a same-type requirement between type parameters
- 'Self' appears as a structural component of the right hand side of a concrete type requirement
- 'Self' appears as a structural component of the right hand side of a superclass requirement

My previous fix only handled the first case. Generalize it to handle all three.

Fixes rdar://problem/74944514.
@slavapestov slavapestov force-pushed the protocol-self-reference-fixes branch from 318ea19 to 599bb79 Compare March 9, 2022 23:50
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

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.

I'm wondering if the new variance check in the conformance checker, findExistentialSelfReferences() and Sema's doesMemberHaveUnfulfillableConstraintsWithExistentialBase() can all be unified somehow.

Unifying the first and last sounds simple enough, I could see to that in a follow-up. I think the type signature check should have its own entry point: apparently there are places like this one where only generic requirements are of interest (and vice versa?).

@slavapestov slavapestov merged commit 1ff3fda into swiftlang:main Mar 10, 2022
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.

2 participants