Skip to content

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

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 9, 2023

Conversation

egorzhdan
Copy link
Contributor

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

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Sep 19, 2023
@egorzhdan
Copy link
Contributor Author

This unblocks #68240.

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

@swift-ci please test

@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from 856216d to 6cbdcaf Compare September 20, 2023 12:49
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

1 similar comment
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from 6cbdcaf to dae04e2 Compare October 5, 2023 16:02
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from dae04e2 to c83df4f Compare October 7, 2023 16:24
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch 2 times, most recently from bcaa474 to 3fa512d Compare October 7, 2023 17:26
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from 3fa512d to eee38f0 Compare October 7, 2023 17:57
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from eee38f0 to 6f146a0 Compare October 8, 2023 13:29
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from 6f146a0 to f2e4832 Compare October 8, 2023 21:29
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

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
@egorzhdan egorzhdan force-pushed the egorzhdan/template-type-names branch from f2e4832 to 041005a Compare October 9, 2023 13:57
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke test

@@ -2217,7 +2217,7 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
version, givenName);
if (!isa<clang::ClassTemplatePartialSpecializationDecl>(D)) {
auto getSwiftBuiltinTypeName =
[&](const clang::BuiltinType *builtin) -> std::optional<StringRef> {
[&](const clang::BuiltinType *builtin) -> std::optional<std::string> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This dangling pointer issue got exposed by this patch

@egorzhdan egorzhdan merged commit 4f861e4 into main Oct 9, 2023
@egorzhdan egorzhdan deleted the egorzhdan/template-type-names branch October 9, 2023 17:07
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.

2 participants