diff --git a/toolchain/check/cpp/type_mapping.cpp b/toolchain/check/cpp/type_mapping.cpp index fd2c68841d95..74d02b0a7d73 100644 --- a/toolchain/check/cpp/type_mapping.cpp +++ b/toolchain/check/cpp/type_mapping.cpp @@ -266,6 +266,29 @@ static auto TryMapClassType(Context& context, SemIR::ClassType class_type) if (!tag_decl) { return clang::QualType(); } + + // If the decl is a `ClassTemplateSpecializationDecl` where some of the + // template args are dependent, it must be represented as a + // `TemplateSpecializationType` instead of a `TagType`. + if (auto* specialization_decl = + llvm::dyn_cast(tag_decl)) { + auto args = specialization_decl->getTemplateArgs().asArray(); + auto is_dependent = + llvm::any_of(args, [](const auto& arg) { return arg.isDependent(); }); + + if (is_dependent) { + llvm::SmallVector canonical_args( + llvm::map_range(args, [&ast_context](auto& arg) { + return ast_context.getCanonicalTemplateArgument(arg); + })); + + return ast_context.getTemplateSpecializationType( + clang::ElaboratedTypeKeyword::None, + clang::TemplateName(specialization_decl->getSpecializedTemplate()), + args, canonical_args); + } + } + return ast_context.getCanonicalTagType(tag_decl); } diff --git a/toolchain/check/testdata/interop/cpp/function/export/generic.carbon b/toolchain/check/testdata/interop/cpp/function/export/generic.carbon index 91ca9c9f3db4..5e9c6a476bfd 100644 --- a/toolchain/check/testdata/interop/cpp/function/export/generic.carbon +++ b/toolchain/check/testdata/interop/cpp/function/export/generic.carbon @@ -185,6 +185,23 @@ void CallUnsupportedF() { } '''; +// --- generic_class_param.carbon +library "[[@TEST_NAME]]"; +import Cpp; + +class A {} + +class C(T: type) {} + +fn F[T: type](unused c: C(T)) {} + +inline Cpp ''' +void G() { + Carbon::C c; + Carbon::F(c); +} +'''; + // CHECK:STDOUT: --- generic_facet.carbon = $generic_facet // CHECK:STDOUT: // CHECK:STDOUT: constants {