[clang] [clang-tools-extra] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> (PR #91339)
https://github.com/sdkrystian closed https://github.com/llvm/llvm-project/pull/91339 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-tools-extra] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> (PR #91339)
https://github.com/sdkrystian updated https://github.com/llvm/llvm-project/pull/91339 >From 715406a9188129dafef18674e4a5870e17dba8c5 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:07:26 -0400 Subject: [PATCH 1/6] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> --- .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 24 ++- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index d544cfac55ba3..37d5d3face088 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2269,16 +2269,18 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl( TemplateArgumentList::CreateCopy(SemaRef.Context, Innermost), /*InsertPos=*/nullptr); - } else if (isFriend && D->isThisDeclarationADefinition()) { -// Do not connect the friend to the template unless it's actually a -// definition. We don't want non-template functions to be marked as being -// template instantiations. -Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); - } else if (!isFriend) { -// If this is not a function template, and this is not a friend (that is, -// this is a locally declared function), save the instantiation relationship -// for the purposes of constraint instantiation. -Function->setInstantiatedFromDecl(D); + } else if (FunctionRewriteKind == RewriteKind::None) { +if (isFriend && D->isThisDeclarationADefinition()) { + // Do not connect the friend to the template unless it's actually a + // definition. We don't want non-template functions to be marked as being + // template instantiations. + Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); +} else if (!isFriend) { + // If this is not a function template, and this is not a friend (that is, + // this is a locally declared function), save the instantiation relationship + // for the purposes of constraint instantiation. + Function->setInstantiatedFromDecl(D); +} } if (isFriend) { @@ -2669,7 +2671,7 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( TemplateArgumentList::CreateCopy(SemaRef.Context, Innermost), /*InsertPos=*/nullptr); - } else if (!isFriend) { + } else if (!isFriend && FunctionRewriteKind == RewriteKind::None) { // Record that this is an instantiation of a member function. Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); } >From f2d28e49bd3fa4f6c83e3cc8e5856bb7a32f7d16 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:20:14 -0400 Subject: [PATCH 2/6] [FOLD] add test and release note --- clang/docs/ReleaseNotes.rst | 2 ++ .../class/class.compare/class.compare.default/p4.cpp | 11 +++ 2 files changed, 13 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index c4a9501ca15cf..c8ef2e8d614a7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -693,6 +693,8 @@ Bug Fixes to C++ Support and (#GH88832). - Clang now defers all substitution into the exception specification of a function template specialization until the noexcept-specifier is instantiated. +- Fix a crash when an implicitly declared ``operator==`` function with a trailing requires-clause has its + constraints compared to that of another declaration. Bug Fixes to AST Handling ^ diff --git a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp index 534c3b34d8832..a9b2fd2b2230f 100644 --- a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp +++ b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp @@ -161,3 +161,14 @@ struct non_constexpr_type { my_struct obj; // cxx2a-note {{in instantiation of template class 'GH61238::my_struct' requested here}} } + +namespace Constrained { + template + struct A { +std::strong_ordering operator<=>(const A&) const requires true = default; + }; + + bool f(A a) { +return a != A(); + } +} >From a2597f5b4a4f1d8a5fb7e8a63251623258f46c42 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:20:34 -0400 Subject: [PATCH 3/6] [FOLD] format --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 37d5d3face088..fde2d920c785e 100644 ---
[clang] [clang-tools-extra] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> (PR #91339)
https://github.com/sdkrystian updated https://github.com/llvm/llvm-project/pull/91339 >From b199345e93410ad4e7f7f4b37f09c7e66572d43a Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:07:26 -0400 Subject: [PATCH 1/6] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> --- .../lib/Sema/SemaTemplateInstantiateDecl.cpp | 24 ++- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index d544cfac55ba..37d5d3face08 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -2269,16 +2269,18 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl( TemplateArgumentList::CreateCopy(SemaRef.Context, Innermost), /*InsertPos=*/nullptr); - } else if (isFriend && D->isThisDeclarationADefinition()) { -// Do not connect the friend to the template unless it's actually a -// definition. We don't want non-template functions to be marked as being -// template instantiations. -Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); - } else if (!isFriend) { -// If this is not a function template, and this is not a friend (that is, -// this is a locally declared function), save the instantiation relationship -// for the purposes of constraint instantiation. -Function->setInstantiatedFromDecl(D); + } else if (FunctionRewriteKind == RewriteKind::None) { +if (isFriend && D->isThisDeclarationADefinition()) { + // Do not connect the friend to the template unless it's actually a + // definition. We don't want non-template functions to be marked as being + // template instantiations. + Function->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); +} else if (!isFriend) { + // If this is not a function template, and this is not a friend (that is, + // this is a locally declared function), save the instantiation relationship + // for the purposes of constraint instantiation. + Function->setInstantiatedFromDecl(D); +} } if (isFriend) { @@ -2669,7 +2671,7 @@ Decl *TemplateDeclInstantiator::VisitCXXMethodDecl( TemplateArgumentList::CreateCopy(SemaRef.Context, Innermost), /*InsertPos=*/nullptr); - } else if (!isFriend) { + } else if (!isFriend && FunctionRewriteKind == RewriteKind::None) { // Record that this is an instantiation of a member function. Method->setInstantiationOfMemberFunction(D, TSK_ImplicitInstantiation); } >From 53eef8ed177edef5c4c861472d0a5a7e4bedb072 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:20:14 -0400 Subject: [PATCH 2/6] [FOLD] add test and release note --- clang/docs/ReleaseNotes.rst | 3 +++ .../class/class.compare/class.compare.default/p4.cpp | 11 +++ 2 files changed, 14 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index a85095e424b6..87c4489114c8 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -683,6 +683,9 @@ Bug Fixes to C++ Support - Fix an assertion failure when parsing an invalid members of an anonymous class. (#GH85447) - Fixed a misuse of ``UnresolvedLookupExpr`` for ill-formed templated expressions. Fixes (#GH48673), (#GH63243) and (#GH88832). +- Fix a crash when an implicitly declared ``operator==`` function with a trailing requires-clause has its + constraints compared to that of another declaration. + Bug Fixes to AST Handling ^ diff --git a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp index 534c3b34d883..a9b2fd2b2230 100644 --- a/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp +++ b/clang/test/CXX/class/class.compare/class.compare.default/p4.cpp @@ -161,3 +161,14 @@ struct non_constexpr_type { my_struct obj; // cxx2a-note {{in instantiation of template class 'GH61238::my_struct' requested here}} } + +namespace Constrained { + template + struct A { +std::strong_ordering operator<=>(const A&) const requires true = default; + }; + + bool f(A a) { +return a != A(); + } +} >From 35140102cade777fffe0abc63c7170039e6b18f4 Mon Sep 17 00:00:00 2001 From: Krystian Stasiowski Date: Tue, 7 May 2024 10:20:34 -0400 Subject: [PATCH 3/6] [FOLD] format --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index