diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index c7c2aecc8b179..a8ce1c469b632 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6240,6 +6240,9 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) { // FIXME Check for ODR error if the two definitions have // different initializers? return Importer.MapImported(D, FoundDef); + if (FoundTemplate->getDeclContext()->isRecord() && + D->getDeclContext()->isRecord()) + return Importer.MapImported(D, FoundTemplate); FoundByLookup = FoundTemplate; break; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index c90b5aaeb6243..ac27d932ce919 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -4988,6 +4988,37 @@ TEST_P(ASTImporterOptionSpecificTestBase, } } +TEST_P(ImportFriendClasses, RecordVarTemplateDecl) { + Decl *ToTU = getToTuDecl( + R"( + template + class A { + public: + template + static constexpr bool X = true; + }; + )", + Lang_CXX14); + + auto *ToTUX = FirstDeclMatcher().match( + ToTU, varTemplateDecl(hasName("X"))); + Decl *FromTU = getTuDecl( + R"( + template + class A { + public: + template + static constexpr bool X = true; + }; + )", + Lang_CXX14, "input1.cc"); + auto *FromX = FirstDeclMatcher().match( + FromTU, varTemplateDecl(hasName("X"))); + auto *ToX = Import(FromX, Lang_CXX11); + EXPECT_TRUE(ToX); + EXPECT_EQ(ToTUX, ToX); +} + TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) { constexpr auto Code = R"(