Skip to content

[Clang][Sema] fix a bug on constraint check with template friend function #90646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ Bug Fixes to C++ Support
- Fix a bug on template partial specialization with issue on deduction of nontype template parameter
whose type is `decltype(auto)`. Fixes (#GH68885).
- Clang now correctly treats the noexcept-specifier of a friend function to be a complete-class context.
- Fix a bug on constraint check with template friend function. Fixes (#GH90349).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Sema/SemaTemplateInstantiate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,20 @@ Response HandleFunction(Sema &SemaRef, const FunctionDecl *Function,
if (Function->getPrimaryTemplate()->isMemberSpecialization())
return Response::Done();

if (Function->getFriendObjectKind())
if (const ClassTemplateSpecializationDecl *TD =
dyn_cast<ClassTemplateSpecializationDecl>(
Function->getLexicalDeclContext())) {
const CXXRecordDecl *TemplatePattern =
TD->getTemplateInstantiationPattern();
const FunctionDecl *FunctionPattern =
Function->getTemplateInstantiationPattern();
if (TemplatePattern && FunctionPattern &&
TemplatePattern->getTemplateDepth() ==
FunctionPattern->getTemplateDepth())
return Response::UseNextDecl(Function);
}

// If this function is a generic lambda specialization, we are done.
if (!ForConstraintInstantiation &&
isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) {
Expand Down
67 changes: 67 additions & 0 deletions clang/test/SemaCXX/PR90349.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s

// expected-no-diagnostics

namespace std {
template<class T>
concept floating_point = __is_same(T,double) || __is_same(T,float);

template<class T>
concept integral = __is_same(T,int);

}

template<std::integral T, std::floating_point Float>
class Blob;

template<std::floating_point Float, std::integral T>
Blob<T, Float> MakeBlob();

template<std::integral T, std::floating_point Float>
class Blob {
private:
Blob() {}

friend Blob<T, Float> MakeBlob<Float, T>();
};

template<std::floating_point Float, std::integral T>
Blob<T, Float> MakeBlob()
{
return Blob<T, Float>();
}

template<std::floating_point Float, std::integral T>
Blob<T, Float> FindBlobs()
{
return MakeBlob<Float, T>();
}

int main(int argc, const char * argv[]) {
FindBlobs<double, int>();
return 0;
}

template<typename T, typename U>
concept D = sizeof(T) == sizeof(U);

template<typename T>
struct A
{
template<typename U, typename V> requires D<U, V>
static void f();
};

template<typename T, typename U>
struct B
{
template<typename V>
struct C
{
friend void A<char>::f<T, U>();
};
};

template struct B<int, int>::C<short>;

extern template void A<char>::f<int, int>(); // crash here
Loading