Skip to content

[cxx-interop] Allow C++ function templates to be instantiated with Swift closures #81016

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
Apr 30, 2025

Conversation

j-hui
Copy link
Contributor

@j-hui j-hui commented Apr 23, 2025

Swift started to explicitly forbid the instantiation of C++ function templates with arbitrary types in #77430, because many types cause the Swift compiler to crash. However, those checks prevented them from being instantiated with Swift closures (which were previously fine), causing a regression.

This patch relaxes the convertTemplateArgument() function to also allow converting Swift function types, and adds some tests to make sure doing so is fine. This patch also does some cleanup of an existing test checking the instantiation of various types.

rdar://148124104

@j-hui
Copy link
Contributor Author

j-hui commented Apr 23, 2025

@swift-ci please test

@@ -991,6 +1007,22 @@ clang::QualType ClangTypeConverter::convertTemplateArgument(Type type) {
return clang::QualType();
}

if (auto functionType = type->getAs<FunctionType>()) {
if (llvm::none_of(functionType->getParams(), [](auto &param) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have this restriction for FunctionType but not for SILFunctionType?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch! I'll add this check to SILFunctionType as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually decided to remove the check. All template instantiations involving FRTs are wonky because of nullable<->optional round-tripping, so I think we should keep that behavior at least consistent (even if the behavior is wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(specifically, the behavior is that the compiler crashes...)

@j-hui j-hui force-pushed the swift-function-as-template-arg branch from a81d356 to d3d08f9 Compare April 24, 2025 00:01
@j-hui
Copy link
Contributor Author

j-hui commented Apr 24, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented Apr 24, 2025

Hm. Apparently raw C function pointers don't work without the blocks runtime either. I'm going to move these to the // REQUIRES: objc_runtime test for now.

@j-hui j-hui force-pushed the swift-function-as-template-arg branch from d3d08f9 to 922cc65 Compare April 24, 2025 05:13
@j-hui
Copy link
Contributor Author

j-hui commented Apr 24, 2025

@swift-ci please test

Copy link
Contributor

@Xazax-hun Xazax-hun left a comment

Choose a reason for hiding this comment

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

Thanks!

@Xazax-hun
Copy link
Contributor

@swift-ci please test macOS

1 similar comment
@j-hui
Copy link
Contributor Author

j-hui commented Apr 24, 2025

@swift-ci please test macOS

@j-hui j-hui enabled auto-merge April 25, 2025 01:07
@j-hui
Copy link
Contributor Author

j-hui commented Apr 30, 2025

@swift-ci please test macos

j-hui added 2 commits April 29, 2025 23:17
…ift closures

Swift started to explicitly forbid the instantiation of C++ function
templates with arbitrary types in swiftlang#77430, because many types cause the
Swift compiler to crash. However, those checks prevented them from being
instantiated with Swift closures (which were previously fine), causing
a regression.

This patch relaxes the convertTemplateArgument() function to also allow
converting Swift function types, and adds some tests to make sure doing
so is fine.

This patch also does some cleanup of existing tests checking the
instantiation of various types, and adds testing for C function pointers
and Obj-C blocks.

rdar://148124104
This Boolean flag is used in ClangTypeConverter to indicate whether
a type is being converted in the context of a template type parameter.
This parameter can be made a template parameter because it is always
a compile-time constant.
@j-hui j-hui force-pushed the swift-function-as-template-arg branch from 922cc65 to fee1dd3 Compare April 30, 2025 06:17
@j-hui
Copy link
Contributor Author

j-hui commented Apr 30, 2025

@swift-ci please test

@j-hui
Copy link
Contributor Author

j-hui commented Apr 30, 2025

@swift-ci please test macos

1 similar comment
@j-hui
Copy link
Contributor Author

j-hui commented Apr 30, 2025

@swift-ci please test macos

@j-hui j-hui merged commit b2fe628 into swiftlang:main Apr 30, 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.

2 participants