[lld] [clang] [libc] [lldb] [libunwind] [llvm] [compiler-rt] [libcxxabi] [flang] [libcxx] [clang-tools-extra] [Sema] Use lexical DC for friend functions when getting constraint instantiation args (PR #77552)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 11 15:52:39 PST 2024


https://github.com/antangelo updated https://github.com/llvm/llvm-project/pull/77552

>From f9e35231207090afcda91d3cd3551d7d1639598b Mon Sep 17 00:00:00 2001
From: Antonio Abbatangelo <contact at antangelo.com>
Date: Tue, 9 Jan 2024 20:20:30 -0500
Subject: [PATCH] [Sema] Use lexical DC for friend functions when getting
 constraint instantiation args

Fixes a crash where the template argument depth computed in the semantic
context for a friend FunctionDecl with a constrained parameter is
compared against arguments in the lexical context for the purpose
of checking if the constraint depends on enclosing template parameters.

Since getTemplateInstantiationArgs in this case follows the semantic
DC for friend FunctionDecls, the resulting depth is incorrect and
trips an assertion.
---
 clang/docs/ReleaseNotes.rst                |  4 ++++
 clang/lib/Sema/SemaTemplateInstantiate.cpp |  3 +++
 clang/test/SemaTemplate/GH75426.cpp        | 16 ++++++++++++++++
 3 files changed, 23 insertions(+)
 create mode 100644 clang/test/SemaTemplate/GH75426.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 46f4b82b89e488..a3035fd07282d0 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -705,6 +705,10 @@ Bug Fixes in This Version
 - Fix assertion crash due to failed scope restoring caused by too-early VarDecl
   invalidation by invalid initializer Expr.
   Fixes (`#30908 <https://github.com/llvm/llvm-project/issues/30908>`_)
+- Fix assertion failure when declaring a template friend function with
+  a constrained parameter in a template class that declares a class method
+  or lambda at different depth.
+  Fixes (`#75426 <https://github.com/llvm/llvm-project/issues/75426>`_)
 
 
 Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 7f20413c104e97..fc80515b45e35b 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -223,6 +223,9 @@ Response HandleFunction(const FunctionDecl *Function,
       (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
     return Response::ChangeDecl(Function->getLexicalDeclContext());
   }
+
+  if (ForConstraintInstantiation && Function->getFriendObjectKind())
+    return Response::ChangeDecl(Function->getLexicalDeclContext());
   return Response::UseNextDecl(Function);
 }
 
diff --git a/clang/test/SemaTemplate/GH75426.cpp b/clang/test/SemaTemplate/GH75426.cpp
new file mode 100644
index 00000000000000..faf70699f9c5f0
--- /dev/null
+++ b/clang/test/SemaTemplate/GH75426.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+template<typename T> concept C = true;
+
+struct A {
+    template<C T> void f();
+};
+
+auto L = []<C T>{};
+
+template<typename X>
+class Friends {
+    template<C T> friend void A::f();
+    template<C T> friend void decltype(L)::operator()();
+};



More information about the cfe-commits mailing list