Skip to content

Commit

Permalink
[Sema] Use lexical DC for friend functions when getting constraint in…
Browse files Browse the repository at this point in the history
…stantiation 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
  • Loading branch information
antangelo committed Jan 12, 2024
1 parent 39bb790 commit 4556813
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,10 @@ Bug Fixes in This Version
- Clang now emits correct source location for code-coverage regions in `if constexpr`
and `if consteval` branches.
Fixes (`#54419 <https://github.com/llvm/llvm-project/issues/54419>`_)
- 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>`_)
- Fix an issue where clang cannot find conversion function with template
parameter when instantiation of template class.
Fixes (`#77583 <https://github.com/llvm/llvm-project/issues/77583>`_)
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
16 changes: 16 additions & 0 deletions clang/test/SemaTemplate/GH75426.cpp
Original file line number Diff line number Diff line change
@@ -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()();
};

0 comments on commit 4556813

Please sign in to comment.