-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Fixed shared_ptr comparisons with nullptr_t when spaceship is unavailable. #76781
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libcxx Author: James Touton (Bekenn) Changes@ldionne This was causing compilation errors when attempting to compare a Full diff: https://github.com/llvm/llvm-project/pull/76781.diff 3 Files Affected:
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;
}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
d673084
to
315a90f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your patch!
A few minor comments.
@@ -87,5 +87,38 @@ int main(int, char**) | |||
assert((nullptr <=> p2) == std::strong_ordering::equivalent); | |||
#endif | |||
|
|||
#if TEST_STD_VER > 14 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nowadays we typically use this style.
#if TEST_STD_VER > 14 | |
#if TEST_STD_VER >= 17 |
Please apply to similar cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated.
...ties/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code LGTM. However the CI fails, I expect a rebase will fix the CI.
After rebasing I'll approve the CI run that need approval.
d3c9062
to
66744b0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM! Do you need help landing this patch?
I don't currently have write access, though I have contributed elsewhere in llvm-project before, and have some other stuff I'm working on. |
…able. (llvm#76781) This was causing compilation errors when attempting to compare a `shared_ptr<T[]>` with `nullptr`, as `get()` returns `T*` rather than `T (*)[]`. `unique_ptr` did not have this issue, but I've added tests to make sure.
@ldionne
This was causing compilation errors when attempting to compare a
shared_ptr<T[]>
withnullptr
, asget()
returnsT*
rather thanT (*)[]
.unique_ptr
did not have this issue, but I've added tests to make sure.