Skip to content

Clean up GenericFunctionType substitution #80301

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 8 commits into from
Mar 27, 2025

Conversation

slavapestov
Copy link
Contributor

@slavapestov slavapestov commented Mar 26, 2025

GenericFunctionType::substGenericArgs() didn't support parameter packs. This can be fixed by replacing the implementation with a much simpler one: we form a new FunctionType by dropping the generic signature, and then we call subst().

Now, if subst() was called directly on a GenericFunctionType, we would essentially do the same thing as above if the replacement types were not type parameters, and output a FunctionType. If the substitution replaced type parameters with type parameters though, we would attempt to build a new generic signature, and output a new GenericFunctionType. The way this was done wasn't really meaningful, except when the substitution map was the identity substitution map, in which case it did manage to rebuild the original signature.

Since this is an ongoing source of confusion, clean it up by changing subst() to simply assert if the input is a GenericFunctionType. Instead, callers must explicitly call substGenericArgs().

A GenericFunctionType is really not like the other interface types at all; it is a closed term that can only contain type parameters of its own generic signature, while other interface types can contain free occurrences of arbitrary type parameters. Since Swift doesn't allow higher-rank polymorphism, GenericFunctionType is not the type of a first-class value, so most callers of subst() don't expect to see a GenericFunctionType. In the few cases that do, we should be explicit about what we're doing.

@slavapestov slavapestov force-pushed the subst-generic-function-type branch from b0af0a6 to 1859533 Compare March 27, 2025 00:07
@slavapestov
Copy link
Contributor Author

@swift-ci Please smoke test

@slavapestov
Copy link
Contributor Author

@swift-ci Please test source compatibility

@slavapestov slavapestov merged commit 09a4e16 into swiftlang:main Mar 27, 2025
4 of 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.

1 participant