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

2024-01-10 Thread via cfe-commits

antangelo wrote:

Thank you for the review! I have commit access so I'll be able to merge once 
the CI runs pass after fixing conflicts.

https://github.com/llvm/llvm-project/pull/77552
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2024-01-10 Thread via cfe-commits

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 
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 `_)
+- 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 `_)
 
 
 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 00..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 concept C = true;
+
+struct A {
+template void f();
+};
+
+auto L = []{};
+
+template
+class Friends {
+template friend void A::f();
+template friend void decltype(L)::operator()();
+};

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits