[libcxx-commits] [libcxx] Fixed shared_ptr comparisons with nullptr_t when spaceship is unavailable. (PR #76781)
James Touton via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jan 3 01:02:26 PST 2024
https://github.com/Bekenn updated https://github.com/llvm/llvm-project/pull/76781
>From f605a7a566cfadf8490e000792b32acff342f8a4 Mon Sep 17 00:00:00 2001
From: James Touton <bekenn at gmail.com>
Date: Tue, 2 Jan 2024 21:30:05 -0800
Subject: [PATCH 1/2] Fixed shared_ptr comparisons with nullptr_t when
spaceship is unavailable.
---
libcxx/include/__memory/shared_ptr.h | 4 +--
.../cmp_nullptr.pass.cpp | 31 +++++++++++++++++
.../unique.ptr.special/cmp_nullptr.pass.cpp | 34 +++++++++++++++++++
3 files changed, 67 insertions(+), 2 deletions(-)
diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h
index 9aa938b2203121..9a73d439306d9e 100644
--- a/libcxx/include/__memory/shared_ptr.h
+++ b/libcxx/include/__memory/shared_ptr.h
@@ -1166,12 +1166,12 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(nullptr_t, const shared_ptr<_Tp>& _
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI bool operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT {
- return less<_Tp*>()(__x.get(), nullptr);
+ return less<typename shared_ptr<_Tp>::element_type*>()(__x.get(), nullptr);
}
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI bool operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT {
- return less<_Tp*>()(nullptr, __x.get());
+ return less<typename shared_ptr<_Tp>::element_type*>()(nullptr, __x.get());
}
template <class _Tp>
diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
index 3bc8ef3799ab66..5a131714fce4c4 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
@@ -87,5 +87,36 @@ int main(int, char**)
assert((nullptr <=> p2) == std::strong_ordering::equivalent);
#endif
+ const std::shared_ptr<int[]> p3(new int[1]);
+ assert(!(p3 == nullptr));
+ assert(!(nullptr == p3));
+ assert(!(p3 < nullptr));
+ assert((nullptr < p3));
+ assert(!(p3 <= nullptr));
+ assert((nullptr <= p3));
+ assert((p3 > nullptr));
+ assert(!(nullptr > p3));
+ assert((p3 >= nullptr));
+ assert(!(nullptr >= p3));
+#if TEST_STD_VER > 17
+ assert((nullptr <=> p3) == std::strong_ordering::less);
+ assert((p3 <=> nullptr) == std::strong_ordering::greater);
+#endif
+
+ const std::shared_ptr<int[]> p4;
+ assert((p4 == nullptr));
+ assert((nullptr == p4));
+ assert(!(p4 < nullptr));
+ assert(!(nullptr < p4));
+ assert((p4 <= nullptr));
+ assert((nullptr <= p4));
+ assert(!(p4 > nullptr));
+ assert(!(nullptr > p4));
+ assert((p4 >= nullptr));
+ assert((nullptr >= p4));
+#if TEST_STD_VER > 17
+ assert((nullptr <=> p4) == std::strong_ordering::equivalent);
+#endif
+
return 0;
}
diff --git a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
index ddd02a455c5883..9ee88e22884488 100644
--- a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
@@ -92,6 +92,40 @@ TEST_CONSTEXPR_CXX23 bool test() {
assert((nullptr <=> p2) == std::strong_ordering::equivalent);
#endif
+ const std::unique_ptr<int[]> p3(new int[1]);
+ assert(!(p3 == nullptr));
+ assert(!(nullptr == p3));
+ // A pointer to allocated storage and a nullptr can't be compared at compile-time
+ if (!TEST_IS_CONSTANT_EVALUATED) {
+ assert(!(p3 < nullptr));
+ assert((nullptr < p3));
+ assert(!(p3 <= nullptr));
+ assert((nullptr <= p3));
+ assert((p3 > nullptr));
+ assert(!(nullptr > p3));
+ assert((p3 >= nullptr));
+ assert(!(nullptr >= p3));
+#if TEST_STD_VER > 17
+ assert((nullptr <=> p3) == std::strong_ordering::less);
+ assert((p3 <=> nullptr) == std::strong_ordering::greater);
+#endif
+ }
+
+ const std::unique_ptr<int[]> p4;
+ assert((p4 == nullptr));
+ assert((nullptr == p4));
+ assert(!(p4 < nullptr));
+ assert(!(nullptr < p4));
+ assert((p4 <= nullptr));
+ assert((nullptr <= p4));
+ assert(!(p4 > nullptr));
+ assert(!(nullptr > p4));
+ assert((p4 >= nullptr));
+ assert((nullptr >= p4));
+#if TEST_STD_VER > 17
+ assert((nullptr <=> p4) == std::strong_ordering::equivalent);
+#endif
+
return true;
}
>From 315a90f65735d0eff0a743b1d662249693f18c0e Mon Sep 17 00:00:00 2001
From: James Touton <bekenn at gmail.com>
Date: Wed, 3 Jan 2024 00:22:01 -0800
Subject: [PATCH 2/2] Don't test shared_ptr<T[]> in language modes earlier than
C++17.
---
.../util.smartptr.shared.cmp/cmp_nullptr.pass.cpp | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
index 5a131714fce4c4..3281986a42c86b 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
@@ -87,6 +87,7 @@ int main(int, char**)
assert((nullptr <=> p2) == std::strong_ordering::equivalent);
#endif
+#if TEST_STD_VER > 14
const std::shared_ptr<int[]> p3(new int[1]);
assert(!(p3 == nullptr));
assert(!(nullptr == p3));
@@ -98,10 +99,10 @@ int main(int, char**)
assert(!(nullptr > p3));
assert((p3 >= nullptr));
assert(!(nullptr >= p3));
-#if TEST_STD_VER > 17
+# if TEST_STD_VER > 17
assert((nullptr <=> p3) == std::strong_ordering::less);
assert((p3 <=> nullptr) == std::strong_ordering::greater);
-#endif
+# endif
const std::shared_ptr<int[]> p4;
assert((p4 == nullptr));
@@ -114,8 +115,9 @@ int main(int, char**)
assert(!(nullptr > p4));
assert((p4 >= nullptr));
assert((nullptr >= p4));
-#if TEST_STD_VER > 17
+# if TEST_STD_VER > 17
assert((nullptr <=> p4) == std::strong_ordering::equivalent);
+# endif
#endif
return 0;
More information about the libcxx-commits
mailing list