diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -391,11 +391,18 @@ bool considerVisibility = shouldConsiderTemplateVisibility(fn, specInfo); - // Merge information from the template parameters. FunctionTemplateDecl *temp = specInfo->getTemplate(); - LinkageInfo tempLV = - getLVForTemplateParameterList(temp->getTemplateParameters(), computation); - LV.mergeMaybeWithVisibility(tempLV, considerVisibility); + + // Merge information from the template declaration. + LinkageInfo tempLV = getLVForDecl(temp, computation); + // The linkage of the specialization should be consistent with the + // template declaration. + LV.setLinkage(tempLV.getLinkage()); + + // Merge information from the template parameters. + LinkageInfo paramsLV = + getLVForTemplateParameterList(temp->getTemplateParameters(), computation); + LV.mergeMaybeWithVisibility(paramsLV, considerVisibility); // Merge information from the template arguments. const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; diff --git a/clang/unittests/AST/DeclTest.cpp b/clang/unittests/AST/DeclTest.cpp --- a/clang/unittests/AST/DeclTest.cpp +++ b/clang/unittests/AST/DeclTest.cpp @@ -171,3 +171,26 @@ selectFirst("f", match(functionDecl().bind("f"), Ctx)); EXPECT_TRUE(f->isInExportDeclContext()); } + +TEST(Decl, InConsistLinkageForTemplates) { + llvm::Annotations Code(R"( + export module m; + export template + void f() {} + + template <> + void f() {})"); + + auto AST = + tooling::buildASTFromCodeWithArgs(Code.code(), /*Args=*/{"-std=c++20"}); + ASTContext &Ctx = AST->getASTContext(); + + llvm::SmallVector Funcs = + match(functionDecl().bind("f"), Ctx); + + EXPECT_EQ(Funcs.size(), 2); + const FunctionDecl *TemplateF = Funcs[0].getNodeAs("f"); + const FunctionDecl *SpecializedF = Funcs[1].getNodeAs("f"); + EXPECT_EQ(TemplateF->getLinkageInternal(), + SpecializedF->getLinkageInternal()); +}