[clang] [clang-tools-extra] [Clang][Sema] Don't set instantiated from function when rewriting operator<=> (PR #91339)
Krystian Stasiowski via cfe-commits
cfe-commits at lists.llvm.org
Tue May 7 17:08:13 PDT 2024
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 <sdkrystian at gmail.com>
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 <sdkrystian at gmail.com>
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<non_constexpr_type> obj; // cxx2a-note {{in instantiation of template class 'GH61238::my_struct<GH61238::non_constexpr_type>' requested here}}
}
+
+namespace Constrained {
+ template<typename T>
+ struct A {
+ std::strong_ordering operator<=>(const A&) const requires true = default;
+ };
+
+ bool f(A<int> a) {
+ return a != A<int>();
+ }
+}
>From a2597f5b4a4f1d8a5fb7e8a63251623258f46c42 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
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
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -2277,8 +2277,8 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(
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.
+ // this is a locally declared function), save the instantiation
+ // relationship for the purposes of constraint instantiation.
Function->setInstantiatedFromDecl(D);
}
}
>From caa379073f266c7f8bb8e6b10db8805f77233731 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Tue, 7 May 2024 11:00:56 -0400
Subject: [PATCH 4/6] [FOLD] update test
---
.../class.compare/class.compare.default/p4.cpp | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
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 a9b2fd2b2230f..c89718dd71bb0 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
@@ -18,14 +18,22 @@ namespace std {
namespace N {
struct A {
- friend constexpr std::strong_ordering operator<=>(const A&, const A&) = default;
+ friend constexpr std::strong_ordering operator<=>(const A&, const A&) = default; // expected-note 2{{declared here}}
};
- constexpr bool (*test_a_not_found)(const A&, const A&) = &operator==; // expected-error {{undeclared}}
+ constexpr std::strong_ordering (*test_a_threeway_not_found)(const A&, const A&) = &operator<=>; // expected-error {{undeclared}}
+
+ constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
+ constexpr std::strong_ordering (*test_a_threeway)(const A&, const A&) = &operator<=>;
+ static_assert((*test_a_threeway)(A(), A())); // expected-error {{static assertion expression is not an integral constant expression}}
+ // expected-note at -1 {{undefined function 'operator<=>' cannot be used in a constant expression}}
+
+ constexpr bool (*test_a_equal_not_found)(const A&, const A&) = &operator==; // expected-error {{undeclared}}
constexpr bool operator==(const A&, const A&) noexcept;
- constexpr bool (*test_a)(const A&, const A&) noexcept = &operator==;
- static_assert((*test_a)(A(), A()));
+ constexpr bool (*test_a_equal)(const A&, const A&) noexcept = &operator==;
+ static_assert((*test_a_equal)(A(), A())); // expected-error {{static assertion expression is not an integral constant expression}}
+ // expected-note at -1 {{undefined function 'operator==' cannot be used in a constant expression}}
}
struct B1 {
>From 7b690847ef3cdc751526963b4deda92f04d6098b Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Tue, 7 May 2024 11:15:00 -0400
Subject: [PATCH 5/6] [FOLD] fix test
---
clang/test/CXX/class/class.compare/class.compare.default/p4.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 c89718dd71bb0..53a8bfc9a4f4a 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
@@ -25,7 +25,7 @@ namespace N {
constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
constexpr std::strong_ordering (*test_a_threeway)(const A&, const A&) = &operator<=>;
- static_assert((*test_a_threeway)(A(), A())); // expected-error {{static assertion expression is not an integral constant expression}}
+ static_assert(!(*test_a_threeway)(A(), A())); // expected-error {{static assertion expression is not an integral constant expression}}
// expected-note at -1 {{undefined function 'operator<=>' cannot be used in a constant expression}}
constexpr bool (*test_a_equal_not_found)(const A&, const A&) = &operator==; // expected-error {{undeclared}}
>From 0f29348f1a9ea8a3b2f032709ccdd795dcf97141 Mon Sep 17 00:00:00 2001
From: Krystian Stasiowski <sdkrystian at gmail.com>
Date: Tue, 7 May 2024 13:35:51 -0400
Subject: [PATCH 6/6] [FOLD] fix clangd tests
---
clang-tools-extra/clangd/unittests/FindTargetTests.cpp | 5 +----
clang-tools-extra/clangd/unittests/HoverTests.cpp | 4 ++--
2 files changed, 3 insertions(+), 6 deletions(-)
diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
index 94437857cecca..0b2273f0a9a6e 100644
--- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
+++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp
@@ -642,10 +642,7 @@ TEST_F(TargetDeclTest, RewrittenBinaryOperator) {
bool x = (Foo(1) [[!=]] Foo(2));
)cpp";
EXPECT_DECLS("CXXRewrittenBinaryOperator",
- {"std::strong_ordering operator<=>(const Foo &) const = default",
- Rel::TemplatePattern},
- {"bool operator==(const Foo &) const noexcept = default",
- Rel::TemplateInstantiation});
+ {"bool operator==(const Foo &) const noexcept = default"});
}
TEST_F(TargetDeclTest, FunctionTemplate) {
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 28df24f34827c..d9e97e5215a26 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -3091,7 +3091,7 @@ TEST(Hover, All) {
HI.NamespaceScope = "";
HI.Definition =
"bool operator==(const Foo &) const noexcept = default";
- HI.Documentation = "Foo spaceship";
+ HI.Documentation = "";
}},
};
@@ -3894,7 +3894,7 @@ TEST(Hover, SpaceshipTemplateNoCrash) {
TU.ExtraArgs.push_back("-std=c++20");
auto AST = TU.build();
auto HI = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
- EXPECT_EQ(HI->Documentation, "Foo bar baz");
+ EXPECT_EQ(HI->Documentation, "");
}
TEST(Hover, ForwardStructNoCrash) {
More information about the cfe-commits
mailing list