Skip to content

Commit 4556813

Browse files
authored
[Sema] Use lexical DC for friend functions when getting constraint instantiation args (#77552)
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
1 parent 39bb790 commit 4556813

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ Bug Fixes in This Version
723723
- Clang now emits correct source location for code-coverage regions in `if constexpr`
724724
and `if consteval` branches.
725725
Fixes (`#54419 <https://github.com/llvm/llvm-project/issues/54419>`_)
726+
- Fix assertion failure when declaring a template friend function with
727+
a constrained parameter in a template class that declares a class method
728+
or lambda at different depth.
729+
Fixes (`#75426 <https://github.com/llvm/llvm-project/issues/75426>`_)
726730
- Fix an issue where clang cannot find conversion function with template
727731
parameter when instantiation of template class.
728732
Fixes (`#77583 <https://github.com/llvm/llvm-project/issues/77583>`_)

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ Response HandleFunction(const FunctionDecl *Function,
223223
(!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) {
224224
return Response::ChangeDecl(Function->getLexicalDeclContext());
225225
}
226+
227+
if (ForConstraintInstantiation && Function->getFriendObjectKind())
228+
return Response::ChangeDecl(Function->getLexicalDeclContext());
226229
return Response::UseNextDecl(Function);
227230
}
228231

clang/test/SemaTemplate/GH75426.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
2+
// expected-no-diagnostics
3+
4+
template<typename T> concept C = true;
5+
6+
struct A {
7+
template<C T> void f();
8+
};
9+
10+
auto L = []<C T>{};
11+
12+
template<typename X>
13+
class Friends {
14+
template<C T> friend void A::f();
15+
template<C T> friend void decltype(L)::operator()();
16+
};

0 commit comments

Comments
 (0)