Skip to content

[TypeChecker] SE-0352: Require coercion if result type contains existential(s) that would loose generic requirements #42559

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 10 commits into from
May 27, 2022

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Apr 22, 2022

Implements SE-0352 revision - require explicit as coercion when existential
erasure loses information.

For example:

protocol P {
  associatedtype A
}

protocol Q {
  associatedtype B: P where B.A == Int
}

func getB<T: Q>(_: T) -> T.B { ... }

func test(v: any Q) {
  let _ = getB(v) // <- produces `any P` which loses A == Int
}

@xedin xedin requested a review from DougGregor April 22, 2022 01:36
@xedin
Copy link
Contributor Author

xedin commented Apr 22, 2022

@DougGregor Please take a look to make sure that this is going in the right direction, I still need to implement this diagnostic for Self erasure cases but it shouldn't be too bad.

@xedin xedin force-pushed the se-0352-as-any-coercion branch from 45150b9 to a95c827 Compare April 22, 2022 01:38
@xedin xedin marked this pull request as ready for review April 22, 2022 21:58
@xedin xedin force-pushed the se-0352-as-any-coercion branch from b57fd74 to 29ae2eb Compare April 22, 2022 21:58
@xedin xedin requested a review from hborla April 22, 2022 21:58
@xedin xedin force-pushed the se-0352-as-any-coercion branch from 29ae2eb to f309e4f Compare April 27, 2022 22:07
@xedin
Copy link
Contributor Author

xedin commented Apr 28, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Apr 28, 2022

@swift-ci please test source compatibility

@xedin
Copy link
Contributor Author

xedin commented May 25, 2022

@swift-ci please smoke test

xedin added 8 commits May 25, 2022 16:07
…ments

Diagnose situations where inferring existential type for result of
a call would result in loss of generic requirements.

```swift
protocol P {
  associatedtype A
}

protocol Q {
 associatedtype B: P where B.A == Int
}

func getB<T: Q>(_: T) -> T.B { ... }

func test(v: any Q) {
  let _ = getB(v) // <- produces `any P` which looses A == Int
}
```
…rcion

If there is going to be any loss of generic requirements in the result,
a result of a call has to use explicit coercion.
For types like `<Base>.B.C` we need to check that neither
`B` nor `C` have any additional requirements because they
cannot be expressed in the existential type.
…would loose information

Accessing members on the protocol could result in existential opening and subsequence
result erasure, which requires explicit coercion if there is any loss of generic requirements.
…d of declaration

Since the inference is a call site specific it makes sense to attach
a candidate note to the call itself instead of the declaration used.
Instead of checking for presence of `where` clause, let's use
requirement signature and check whether any of the requires are
anchored on a particular associated type.
@xedin xedin force-pushed the se-0352-as-any-coercion branch from f309e4f to ec0b836 Compare May 26, 2022 19:09
@xedin
Copy link
Contributor Author

xedin commented May 26, 2022

@swift-ci please test

xedin added 2 commits May 26, 2022 15:34
…ppressing opening at use site

If erased result is passed as an argument to a call that requires
implicit opening, the fix-it should use parens to avoid suppressing
the opening at that argument position.
@xedin xedin force-pushed the se-0352-as-any-coercion branch from d8e40a1 to 8eba890 Compare May 26, 2022 22:35
@xedin
Copy link
Contributor Author

xedin commented May 26, 2022

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented May 27, 2022

@swift-ci please test macOS platform

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