[clang] [Sema] Use lexical DC for friend functions when getting constraint instantiation args (PR #77552)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 9 19:33:07 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (antangelo)

<details>
<summary>Changes</summary>

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.

Fixes #<!-- -->75426

---
Full diff: https://github.com/llvm/llvm-project/pull/77552.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.rst (+4) 
- (modified) clang/lib/Sema/SemaTemplateInstantiate.cpp (+3) 
- (added) clang/test/SemaTemplate/GH75426.cpp (+16) 


``````````diff
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()();
+};

``````````

</details>


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


More information about the cfe-commits mailing list