Skip to content

🍒[cxx-interop] Use more correct type names in C++ template parameters #69328

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
Oct 26, 2023

Conversation

egorzhdan
Copy link
Contributor

@egorzhdan egorzhdan commented Oct 23, 2023

Explanation:
When importing a C++ class template instantiation, Swift translates the template parameter type names from C++ into their Swift equivalent.
For instance, basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>> gets imported as basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>: wchar_t is imported as CWideChar, which is a typealias for Scalar on most platforms including Darwin. Notice that Swift goes through the CWideChar typealias on the specific platform. Another instantiation basic_string<uint32_t, char_traits<uint32_t>, allocator<uint32_t>> also gets imported as basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>: uint32_t is also imported as Scalar. This is problematic because we have two distinct C++ types that have the same name in Swift.
This change makes sure Swift doesn't go through typealiases when emitting names of template parameters, so wchar_t would now get printed as CWideChar, int would get printed as CInt, etc.

Scope: All of the code changes are on a C++ interop only code path.
Risk: Low, only takes effect when C++ interop is enabled.
Testing: Covered by LIT tests.
Original PR: #68620

rdar://115673622

(cherry picked from commit 041005a)

When importing a C++ class template instantiation, Swift translates the template parameter type names from C++ into their Swift equivalent.

For instance, `basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>` gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `wchar_t` is imported as `CWideChar`, which is a typealias for `Scalar` on most platforms including Darwin. Notice that Swift goes through the `CWideChar` typealias on the specific platform. Another instantiation `basic_string<uint32_t, char_traits<uint32_t>, allocator<uint32_t>>` also gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `uint32_t` is also imported as `Scalar`. This is problematic because we have two distinct C++ types that have the same name in Swift.

This change makes sure Swift doesn't go through typealiases when emitting names of template parameters, so `wchar_t` would now get printed as `CWideChar`, `int` would get printed as `CInt`, etc.

This also encourages clients to use the correct type (`CInt`, `CWideChar`, etc) instead of relying on platform-specific typealiases.

rdar://115673622

(cherry picked from commit 041005a)
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Oct 23, 2023
@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan egorzhdan marked this pull request as ready for review October 24, 2023 12:52
@egorzhdan egorzhdan requested a review from a team as a code owner October 24, 2023 12:52
@egorzhdan
Copy link
Contributor Author

@swift-ci please test source compatibility

@egorzhdan egorzhdan requested a review from hyp October 24, 2023 12:52
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

This makes a lot of sense, thank you!

@egorzhdan egorzhdan merged commit 46f5b89 into release/5.10 Oct 26, 2023
@egorzhdan egorzhdan deleted the egorzhdan/5.10-template-type-names branch October 26, 2023 20:46
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.

3 participants