Skip to content

🍒[cxx-interop] Fix exporting cdecl Swift functions to Obj-C++ #74615

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 1 commit into from
Jun 24, 2024

Conversation

Xazax-hun
Copy link
Contributor

@Xazax-hun Xazax-hun commented Jun 21, 2024

Explanation:
We export cdecl function declarations twice: once for Objective-C and once for C++. When the code is compiled in Objective-C++ both of the declarations are visible to the compiler. The generated header did not compile, because only one of the declarations were noexcept. There are multiple possible ways to fix this issue, one of them would make only the C++ declarations visible in Objective-C++ mode. However, I decided to make the Objective-C functions SWIFT_NOEXCEPT. This approach resolves the inconsistency that broke the code when compiled in Objective-C++ mode. Moreover, Swift guarantees that those cdecl declarations cannot raise errors, so in case we only generate the Obj-C headers and consume them from Objective-C++, those are the correct declarations. Keeping both declarations will also help us identify further inconsistencies in the future.
Scope: C++ Interop
Risk: Low, however, when the Obj-C header is consumed by C++ or Obj-C++, the newly added noexcept is observable, code with SFINAE might pick a different overload. This observable change should be benign in most of the cases.
Testing: Added a regression test.
Issue: rdar://129550313
Reviewer: @egorzhdan, @beccadax
Original PR: #74516

[cxx-interop] Fix exporting cdecl Swift functions to Obj-C++
@Xazax-hun Xazax-hun requested a review from a team as a code owner June 21, 2024 09:25
@Xazax-hun Xazax-hun added the c++ interop Feature: Interoperability with C++ label Jun 21, 2024
@Xazax-hun Xazax-hun requested a review from egorzhdan June 21, 2024 09:26
@Xazax-hun
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

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

I'm fine with this.

@Xazax-hun Xazax-hun enabled auto-merge June 24, 2024 18:27
@Xazax-hun Xazax-hun merged commit 5172b72 into release/6.0 Jun 24, 2024
5 checks passed
@Xazax-hun Xazax-hun deleted the gaborh/objcpp-fix-cherry-pick branch June 24, 2024 22:59
drodriguez added a commit that referenced this pull request Jun 28, 2024
Explanation:
In #74516 the `SWIFT_NOEXCEPT` specifier is added to the imported Swift
functions in C++ mode, but it is added after the function attributes. It seems
that the tests only do `-fsyntax-only`, which seems not to catch an error like
"expected function body after function declarator" when the header is
used without that flag.

Flip the attributes and the specifier around in the printer, and flip
them in all the tests.

The tests were using `%check-in-clang`, but it only checks importing as
an objective-c-header. Add a parallel `%check-in-clang-cxx` to test also
in C++. It uses C++17 because of some details in the imported headers and
disables a warning about variadic macros that was promoted to an error
and was blocking passing the tests. The clang-importer-sdk gets the
minimal set of files to compile the two modified tests as C++. The files
are mostly empty, except `cstddef` that imports the equivalent C header.
Some modifications were needed in `ctypes.h` because the header was
using features only available in C and not C++.

Scope: C++ Interop
Risk: Low besides the risks pointed out in #74615.
Testing: The tests have been modified. Locally using the header without -fsyntax-only also avoids the error.
Reviewer: @Xazax-hun
Original PR: #74780
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++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants