Skip to content

AST: Use weighted reduction order for opaque return types #81171

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 5 commits into from
Apr 30, 2025

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Apr 29, 2025

This fixes a bug that has been there ever since https://github.com/swiftlang/swift-evolution/blob/main/proposals/0346-light-weight-same-type-syntax.md was first implemented.

If the opaque generic signature has a same-type requirement between an outer type parameter and an opaque type parameter, the former should always precede the latter in the type parameter order, even if it is longer. Achieve this by giving the innermost generic parameters a non-zero weight in the opaque generic signature.

Now, when we map a type parameter into an opaque generic environment, we correctly decide if it is represented by a type parameter of the outer generic signature, in which case we apply the outer substitution map instead of instantiating an archetype.

The included test case demonstrates the issue; we declare an opaque return type some P<T.A.A>, so the opaque generic signature has a requirement T.A.A == <<some P>>.A. Previously, the reduced type of <<some P>>.A was <<some P>>.A, so it remained opaque; now we apply outer substitutions to T.A.A.

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

This is currently always 0, but can be set to 1.
If the opaque generic signature has a same-type requirement between
an outer type parameter and an opaque type parameter, the former
should always precede the latter in the type parameter order, even
if it is longer. Achieve this by giving the innermost generic
parameters a non-zero weight in the opaque generic signature.

Now, when we map a type parameter into an opaque generic environment,
we correctly decide if it is represented by a type parameter of the
outer generic signature, in which case we apply the outer substitution
map instead of instantiating an archetype.

The included test case demonstrates the issue; we declare an opaque
return type `some P<T.A.A>`, so the opaque generic signature has a
requirement `T.A.A == <<some P>>.A`. Previously, the reduced type of
`<<some P>>.A` was `<<some P>>.A`, so it remained opaque; now we
apply outer substitutions to `T.A.A`.

- Fixes swiftlang#81036.
- Fixes rdar://problem/149871931.
@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test macOS

@slavapestov slavapestov enabled auto-merge April 30, 2025 02:08
@slavapestov
Copy link
Contributor Author

swiftlang/llvm-project#10587
@swift-ci Please smoke test macOS

@slavapestov slavapestov merged commit 7b5b5e9 into swiftlang:main Apr 30, 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.

Swift 5.7: incorrect compiler error with opaque result types and primary associated types
1 participant