https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/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. >From 4267f06f47c9e0bad8b859f2e3fa17d952a5d153 Mon Sep 17 00:00:00 2001 From: Oleksandr Tarasiuk <[email protected]> Date: Mon, 17 Aug 2026 15:51:53 +0300 Subject: [PATCH] [Clang] Prevent an assertion failure when instantiating an invalid friend function template --- clang/lib/Sema/SemaDeclCXX.cpp | 3 +-- clang/test/CXX/temp/temp.decls/temp.friend/p6.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 8d5ee07c5ad49..d0f63b5df9a01 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -18788,8 +18788,7 @@ NamedDecl *Sema::ActOnFriendFunctionDecl(Scope *S, Declarator &D, FriendDecl *Friend = FriendDecl::Create( Context, CurContext, D.getIdentifierLoc(), ND, DS.getFriendSpecLoc()); Friend->setAccess(AS_public); - if (!isa<FunctionTemplateDecl>(ND)) - Friend->setInvalidDecl(); + Friend->setInvalidDecl(); CurContext->addDecl(Friend); return ND; } 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
