Author: Oleksandr Tarasiuk
Date: 2026-08-17T22:48:24+03:00
New Revision: 55194486eda68bf6090d757ad57109f4b8e880ee

URL: 
https://github.com/llvm/llvm-project/commit/55194486eda68bf6090d757ad57109f4b8e880ee
DIFF: 
https://github.com/llvm/llvm-project/commit/55194486eda68bf6090d757ad57109f4b8e880ee.diff

LOG: [Clang] Prevent an assertion failure when instantiating an invalid friend 
function template (#216727)к

Fixes #216694

---

This patch addresses an assertion failure that occurs when instantiating
an invalid friend function template by marking its `FriendDecl` wrapper
as invalid.

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
    clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp 
b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 0356d2b567e54..ff29ae27a3b66 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2818,7 +2818,8 @@ 
TemplateDeclInstantiator::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
   if (!isFriend) {
     Owner->addDecl(InstTemplate);
   } else if (InstTemplate->getDeclContext()->isRecord() &&
-             !getPreviousDeclForInstantiation(D)) {
+             !getPreviousDeclForInstantiation(D) &&
+             isa<CXXMethodDecl>(InstTemplate->getTemplatedDecl())) {
     SemaRef.CheckFriendAccess(InstTemplate);
   }
 

diff  --git a/clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp 
b/clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp
index f60f3dd42445c..673fce425572b 100644
--- a/clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp
@@ -25,3 +25,12 @@ void t3() {
     // expected-error@-1 {{templates cannot be declared inside of a local 
class}}
   };
 }
+
+template <class T> void t4() {
+  struct S {
+    template <class U> friend void f(T);
+    // expected-error@-1 {{templates can only be declared in namespace or 
class scope}}
+  };
+}
+
+template void t4<int>();


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to