Skip to content

🍒 [6.2] [cxx-interop] Import public constructor of C++ foreign reference types as Swift initializers #80715

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 10, 2025

Conversation

fahadnayyar
Copy link
Contributor

@fahadnayyar fahadnayyar commented Apr 10, 2025

Explanation: In this change we are enabling Swift to construct C++ foreign reference types directly by calling an initializer, without needing to create wrapper functions in C++ that would construct the object and return it. The Swift compiler would automatically allocate using new operator and call the C++ constructor of the type via a compiler synthesized static factory.

Issue: rdar://131557219
Risk: Low. This doesn't impact existing code but provides a new functionality. The risk of this patch is that the automatic initializers are synthesized for all foreign references types regardless of whether they are used or not. We take enough precaution before we start the synthesis of any AST node to ensure that the synthesized code will compile correctly.
Testing: Added tests to test suite and manually tested by using this on some adopter projects
Original PRs: #79986, #80449, #80659
Reviewer: @Xazax-hun @j-hui

…pes as Swift Initializer (#80449)

Extends PR #79986 by adding support for calling parameterized C++ initializers from Swift. This patch synthesizes static factory methods corresponding to C++ parameterized constructors, allowing Swift to call them as Swift initializers (e.g., init(_:), init(_:_:), etc.). This patch also aded tests and logic to make sure that we emit no additional diagnostics when a C++ foreign ref type is just referred from Swift and its initializer is not explicitly called.

rdar://148285251
…ssCXXForeignReferenceTypeInitializers to be an opt-out flag

Turning the feature "ctor of C++ foreign reference types is callable as Swift Initializers" on by default.

rdar://148285972
@fahadnayyar
Copy link
Contributor Author

@swift-ci please test

@fahadnayyar fahadnayyar marked this pull request as ready for review April 10, 2025 13:42
@fahadnayyar fahadnayyar requested a review from a team as a code owner April 10, 2025 13:42
@egorzhdan

This comment was marked as resolved.

@fahadnayyar fahadnayyar added c++ interop Feature: Interoperability with C++ 🍒 release cherry pick Flag: Release branch cherry picks labels Apr 10, 2025
@fahadnayyar
Copy link
Contributor Author

@swift-ci please test macOS

@fahadnayyar fahadnayyar merged commit c37700b into release/6.2 Apr 10, 2025
5 checks passed
@fahadnayyar fahadnayyar deleted the fahadnayyar/6.2/cxx-frt-ctor branch April 10, 2025 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++ 🍒 release cherry pick Flag: Release branch cherry picks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants