Skip to content

[cxx-interop] Disambiguate template instantiations with parameter packs #77450

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
Nov 11, 2024

Conversation

egorzhdan
Copy link
Contributor

This makes sure that different template instantiations of std::tuple get distinct Swift type names.

Similar to aa6804a.

This also refactors swift::importer::printClassTemplateSpecializationName to follow a proper visitor pattern for the C++ template arguments.

rdar://139435937

@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Nov 7, 2024
@egorzhdan
Copy link
Contributor Author

@swift-ci please smoke 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.

LGTM!

Comment on lines +187 to +206
void VisitPackTemplateArgument(const clang::TemplateArgument &arg,
llvm::raw_svector_ostream &buffer) {
VisitTemplateArgumentArray(arg.getPackAsArray(), buffer);
}

void VisitTemplateArgumentArray(ArrayRef<clang::TemplateArgument> args,
llvm::raw_svector_ostream &buffer) {
bool needsComma = false;
for (auto &arg : args) {
// Do not try to print empty packs.
if (arg.getKind() == clang::TemplateArgument::ArgKind::Pack &&
arg.getPackAsArray().empty())
continue;

if (needsComma)
buffer << ", ";
Visit(arg, buffer);
needsComma = true;
}
}
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 bit is the new functionality, the rest is just refactoring.

Comment on lines 208 to 210
// Unfortunately, clang::templateargumentvisitor::Base does not correctly
// perform static dispatch for methods that aren't overloaded, so we have to
// overload all of them here.
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 plan to submit a patch to upstream Clang to fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

It seems you're converting a Clang Type into something that looks like a string printout of a Swift Type. Don't you already have the Swift Type for the template argument somewhere so you can just print that out instead?

@egorzhdan
Copy link
Contributor Author

@slavapestov we don't always import the types of template arguments. They are only imported if used elsewhere in the header within fields, parameters, etc. The template parameter could also have a type that we don't import into Swift, or it can be a literal value or an expression that we wouldn't import into Swift.

This makes sure that different template instantiations of `std::tuple` get distinct Swift type names.

Similar to aa6804a.

This also refactors `swift::importer::printClassTemplateSpecializationName` to follow a proper visitor pattern for the C++ template arguments.

rdar://139435937
@egorzhdan
Copy link
Contributor Author

Please test with following PR:

swiftlang/llvm-project#9561

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

Please test with following PR:

swiftlang/llvm-project#9561

@swift-ci please smoke test

@egorzhdan
Copy link
Contributor Author

Please test with following PR:

swiftlang/llvm-project#9561

@swift-ci please clean smoke test

@egorzhdan
Copy link
Contributor Author

Please test with following PR:

swiftlang/llvm-project#9561

@swift-ci please smoke test

egorzhdan added a commit to swiftlang/llvm-project that referenced this pull request Nov 11, 2024
C++ class template instantiations previously did not get a valid generated Swift type name if they used variadic generics.

See swiftlang/swift#77450.

Since fully enabling the test requires the Swift PR to be merged, and the Swift PR relies on this test passing, this PR temporarily disables the failing asserts.

rdar://139435937
rdar://106459037
@egorzhdan
Copy link
Contributor Author

Please test with following PR:

swiftlang/llvm-project#9561

@swift-ci please smoke test

@egorzhdan egorzhdan merged commit fa8852b into main Nov 11, 2024
3 checks passed
@egorzhdan egorzhdan deleted the egorzhdan/class-template-packs branch November 11, 2024 17:48
egorzhdan added a commit to swiftlang/llvm-project that referenced this pull request Nov 13, 2024
C++ class template instantiations previously did not get a valid generated Swift type name if they used variadic generics.

See swiftlang/swift#77450.

rdar://139435937
rdar://106459037
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