Skip to content

[cxx-interop] Disambiguate instantiations with template template parameters#88577

Open
egorzhdan wants to merge 1 commit intoswiftlang:mainfrom
egorzhdan:egorzhdan/template-template
Open

[cxx-interop] Disambiguate instantiations with template template parameters#88577
egorzhdan wants to merge 1 commit intoswiftlang:mainfrom
egorzhdan:egorzhdan/template-template

Conversation

@egorzhdan
Copy link
Copy Markdown
Contributor

  • Explanation:
    template<template <typename> typename> was tripping up the template class name printer – different instantiations of a class template were getting assigned the same name.
  • Scope:
    This adjusts the instantiation name printer to emit an actual name of the template parameter instead of a placeholder (_).
  • Issues:
    rdar://169199221
  • Risk:
    Low, this is covered by tests.
  • Testing:
    Added compiler tests.

…meters

`template<template <typename> typename>` was tripping up the template class name printer – different instantiations of a class template were getting assigned the same name.

rdar://169199221
@egorzhdan egorzhdan requested a review from a team as a code owner April 21, 2026 10:31
@egorzhdan egorzhdan added the c++ interop Feature: Interoperability with C++ label Apr 21, 2026
@egorzhdan
Copy link
Copy Markdown
Contributor Author

@swift-ci please smoke test

Copy link
Copy Markdown
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.

This looks good, but I wonder if we should also cover other (non-integral) template arguments like:

struct S {
    constexpr S(int) {}
};

template <S s>
void f();

void g() {
    constexpr S s{5};
    f<s>();
}

Could be a separate PR though.

@egorzhdan
Copy link
Copy Markdown
Contributor Author

@Xazax-hun that particular example wouldn't trigger this code path in the compiler, since the template name printer is exclusive to types, it isn't used for functions. Supporting more kinds of template scaffolding would definitely be an improvement!

@egorzhdan
Copy link
Copy Markdown
Contributor Author

@swift-ci please test Windows

1 similar comment
@egorzhdan
Copy link
Copy Markdown
Contributor Author

@swift-ci please test Windows

Copy link
Copy Markdown
Contributor

@j-hui j-hui left a comment

Choose a reason for hiding this comment

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

Just for posteriority: do you have any record of what the crash looked like prior to your fix?

@j-hui
Copy link
Copy Markdown
Contributor

j-hui commented Apr 21, 2026

@Xazax-hun that particular example wouldn't trigger this code path in the compiler, since the template name printer is exclusive to types, it isn't used for functions. Supporting more kinds of template scaffolding would definitely be an improvement!

Perhaps something like this?

struct S { constexpr S(int) {} };
template <S s> struct T {};
using T5 = T<{5}>;

if (auto templateDecl = templateName.getAsTemplateDecl()) {
auto importedName = typePrinter.nameImporter->importName(
templateDecl, typePrinter.version);
importedName.getDeclName().print(buffer);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we care about qualified names, for example:

namespace foo {
template <typename> struct Traits {};
} // namespace foo

namespace bar {
template <typename> struct Traits {};
} // namespace bar

using HasTraits3 = HasTemplateTemplateParam<foo::Traits>;
using HasTraits4 = HasTemplateTemplateParam<bar::Traits>;

prints:

typealias HasTraits3 = HasTemplateTemplateParam<Traits>
typealias HasTraits4 = HasTemplateTemplateParam<Traits>

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