Skip to content

Commit 5534f38

Browse files
[test] instantiating template multiple times with same types (#756)
1 parent 6623df3 commit 5534f38

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

unittests/CppInterOp/FunctionReflectionTest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,56 @@ TYPED_TEST(CppInterOpTest, FunctionReflectionTestInstantiateVariadicFunction) {
973973
"fixedParam, MyClass args, double args)");
974974
}
975975

976+
TYPED_TEST(CppInterOpTest, FunctionReflectionTestBestOverloadFunctionMatch0) {
977+
// make sure templates are not instantiated multiple times
978+
std::vector<Decl*> Decls;
979+
std::string code = R"(
980+
template <typename T1, typename T2 = int>
981+
void Tfn(T1& t1, T2& t2) {}
982+
)";
983+
GetAllTopLevelDecls(code, Decls);
984+
EXPECT_EQ(Decls.size(), 1);
985+
986+
std::vector<Cpp::TCppFunction_t> candidates;
987+
candidates.reserve(Decls.size());
988+
for (auto* i : Decls)
989+
candidates.push_back(i);
990+
991+
ASTContext& C = Interp->getCI()->getASTContext();
992+
993+
std::vector<Cpp::TemplateArgInfo> args0 = {
994+
C.getLValueReferenceType(C.DoubleTy).getAsOpaquePtr(),
995+
C.getLValueReferenceType(C.IntTy).getAsOpaquePtr(),
996+
};
997+
998+
std::vector<Cpp::TemplateArgInfo> explicit_args0;
999+
std::vector<Cpp::TemplateArgInfo> explicit_args1 = {
1000+
C.DoubleTy.getAsOpaquePtr()};
1001+
std::vector<Cpp::TemplateArgInfo> explicit_args2 = {
1002+
C.DoubleTy.getAsOpaquePtr(),
1003+
C.IntTy.getAsOpaquePtr(),
1004+
};
1005+
1006+
Cpp::TCppScope_t fn0 =
1007+
Cpp::BestOverloadFunctionMatch(candidates, explicit_args0, args0);
1008+
EXPECT_TRUE(fn0);
1009+
1010+
Cpp::TCppScope_t fn =
1011+
Cpp::BestOverloadFunctionMatch(candidates, explicit_args1, args0);
1012+
EXPECT_EQ(fn, fn0);
1013+
1014+
fn = Cpp::BestOverloadFunctionMatch(candidates, explicit_args2, args0);
1015+
EXPECT_EQ(fn, fn0);
1016+
1017+
fn = Cpp::InstantiateTemplate(Decls[0], explicit_args1.data(),
1018+
explicit_args1.size());
1019+
EXPECT_EQ(fn, fn0);
1020+
1021+
fn = Cpp::InstantiateTemplate(Decls[0], explicit_args2.data(),
1022+
explicit_args2.size());
1023+
EXPECT_EQ(fn, fn0);
1024+
}
1025+
9761026
TYPED_TEST(CppInterOpTest, FunctionReflectionTestBestOverloadFunctionMatch1) {
9771027
std::vector<Decl*> Decls;
9781028
std::string code = R"(

0 commit comments

Comments
 (0)