Skip to content

[cxx-interop] Class template specializations each need their own metadata. #41569

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,14 @@ namespace {
// declaration's name as the ABI name.
} else if (auto clangDecl =
Mangle::ASTMangler::getClangDeclForMangling(Type)) {
abiName = clangDecl->getName();
// Class template specializations need to use their mangled name so
// that each specialization gets its own metadata. A class template
// specialization's Swift name will always be the mangled name, so just
// use that.
if (auto spec = dyn_cast<clang::ClassTemplateSpecializationDecl>(clangDecl))
abiName = Type->getName().str();
else
abiName = clangDecl->getName();

// Typedefs and compatibility aliases that have been promoted to
// their own nominal types need to be marked specially.
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/Cxx/templates/dependent-types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ DependentTypesTestSuite.test("Multiple dependent arguments (inferred type).") {
}

DependentTypesTestSuite.test("Multiple dependent arguments (not inferred).") {
let m = multipleDependentArgs(M<Int>(value: 42), M<CInt>(value: 0), T: Int.self, U: Int.self) as! M<Int>
let m = multipleDependentArgs(M<Int>(value: 42), M<CInt>(value: 0), T: Int.self, U: CInt.self) as! M<Int>
expectEqual(m.getValue(), 42)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef TEST_INTEROP_CXX_VALUE_WITNESS_TABLE_INPUTS_CLASS_TEMPLATE_METADATA_H
#define TEST_INTEROP_CXX_VALUE_WITNESS_TABLE_INPUTS_CLASS_TEMPLATE_METADATA_H

template<class T>
struct ClassTemplate {};

using Spec1 = ClassTemplate<int>;
using Spec2 = ClassTemplate<Spec1>;

#endif // TEST_INTEROP_CXX_VALUE_WITNESS_TABLE_INPUTS_CLASS_TEMPLATE_METADATA_H
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ module CopyConstructors {
header "copy-constructors.h"
requires cplusplus
}

module ClassTemplateMetadata {
header "class-template-metadata.h"
requires cplusplus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
//
// REQUIRES: executable_test

import ClassTemplateMetadata
import StdlibUnittest

var ClassTemplateMetadataTestSuite = TestSuite("Class Template Metadata")

func cmpMetadata<T, U>(_ _: T, _ _: U) -> Bool { "\(T.self)" == "\(U.self)" }

ClassTemplateMetadataTestSuite.test("Different specializations of a class template have different metadata.") {
let a = Spec1()
let b = Spec2()

expectFalse(cmpMetadata(a, b))
expectTrue(cmpMetadata(a, a))
}

runAllTests()