Skip to content
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

[libc++][chrono] Adds year_month_day_last&::operator<=>. #98169

Merged

Conversation

mordante
Copy link
Member

@mordante mordante commented Jul 9, 2024

41f7bb9 claimed it implemented this change but the code was not adjusted. The other spaceship operators in the calendar code have been validated too.

Implements parts of

  • P1614R2 The Mothership has Landed

@mordante mordante requested a review from a team as a code owner July 9, 2024 15:24
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jul 9, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 9, 2024

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

41f7bb9 claimed it implemented this change but the code was not adjusted. The other spaceship operators in the calendar code have been validated too.

Implements parts of

  • P1614R2 The Mothership has Landed

Full diff: https://github.com/llvm/llvm-project/pull/98169.diff

2 Files Affected:

  • (modified) libcxx/include/__chrono/year_month_day.h (+5-27)
  • (modified) libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp (+60-67)
diff --git a/libcxx/include/__chrono/year_month_day.h b/libcxx/include/__chrono/year_month_day.h
index 75884f3654d87..b06c0be03e0de 100644
--- a/libcxx/include/__chrono/year_month_day.h
+++ b/libcxx/include/__chrono/year_month_day.h
@@ -239,33 +239,11 @@ operator==(const year_month_day_last& __lhs, const year_month_day_last& __rhs) n
   return __lhs.year() == __rhs.year() && __lhs.month_day_last() == __rhs.month_day_last();
 }
 
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool
-operator!=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
-  return !(__lhs == __rhs);
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool
-operator<(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
-  if (__lhs.year() < __rhs.year())
-    return true;
-  if (__lhs.year() > __rhs.year())
-    return false;
-  return __lhs.month_day_last() < __rhs.month_day_last();
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool
-operator>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
-  return __rhs < __lhs;
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool
-operator<=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
-  return !(__rhs < __lhs);
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool
-operator>=(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
-  return !(__lhs < __rhs);
+_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering
+operator<=>(const year_month_day_last& __lhs, const year_month_day_last& __rhs) noexcept {
+  if (auto __c = __lhs.year() <=> __rhs.year(); __c != 0)
+    return __c;
+  return __lhs.month_day_last() <=> __rhs.month_day_last();
 }
 
 _LIBCPP_HIDE_FROM_ABI inline constexpr year_month_day_last operator/(const year_month& __lhs, last_spec) noexcept {
diff --git a/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp b/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp
index 45d7e51fbe718..e28b6d8609bc4 100644
--- a/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp
+++ b/libcxx/test/std/time/time.cal/time.cal.ymdlast/time.cal.ymdlast.nonmembers/comparisons.pass.cpp
@@ -5,19 +5,14 @@
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
-// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
 
 // <chrono>
 // class year_month_day_last;
 
 // constexpr bool operator==(const year_month_day_last& x, const year_month_day_last& y) noexcept;
-//   Returns: x.year() == y.year() && x.month_day_last() == y.month_day_last().
-//
-// constexpr bool operator< (const year_month_day_last& x, const year_month_day_last& y) noexcept;
-//   Returns:
-//      If x.year() < y.year(), returns true.
-//      Otherwise, if x.year() > y.year(), returns false.
-//      Otherwise, returns x.month_day_last() < y.month_day_last()
+// constexpr bool operator<=>(const year_month_day_last& x, const year_month_day_last& y) noexcept;
 
 #include <chrono>
 #include <type_traits>
@@ -26,63 +21,61 @@
 #include "test_macros.h"
 #include "test_comparisons.h"
 
-int main(int, char**)
-{
-    using year                = std::chrono::year;
-    using month               = std::chrono::month;
-    using month_day_last      = std::chrono::month_day_last;
-    using year_month_day_last = std::chrono::year_month_day_last;
-
-    AssertComparisonsAreNoexcept<year_month_day_last>();
-    AssertComparisonsReturnBool<year_month_day_last>();
-
-    constexpr month January = std::chrono::January;
-    constexpr month February = std::chrono::February;
-
-    static_assert( testComparisons(
-        year_month_day_last{year{1234}, month_day_last{January}},
-        year_month_day_last{year{1234}, month_day_last{January}},
-        true, false), "");
-
-    // different month
-    static_assert( testComparisons(
-        year_month_day_last{year{1234}, month_day_last{January}},
-        year_month_day_last{year{1234}, month_day_last{February}},
-        false, true), "");
-
-    // different year
-    static_assert( testComparisons(
-        year_month_day_last{year{1234}, month_day_last{January}},
-        year_month_day_last{year{1235}, month_day_last{January}},
-        false, true), "");
-
-    // different month
-    static_assert( testComparisons(
-        year_month_day_last{year{1234}, month_day_last{January}},
-        year_month_day_last{year{1234}, month_day_last{February}},
-        false, true), "");
-
-    // different year and month
-    static_assert( testComparisons(
-        year_month_day_last{year{1234}, month_day_last{February}},
-        year_month_day_last{year{1235}, month_day_last{January}},
-        false, true), "");
-
-    // same year, different months
-    for (unsigned i = 1; i < 12; ++i)
-        for (unsigned j = 1; j < 12; ++j)
-            assert((testComparisons(
-                year_month_day_last{year{1234}, month_day_last{month{i}}},
-                year_month_day_last{year{1234}, month_day_last{month{j}}},
-                i == j, i < j )));
-
-    // same month, different years
-    for (int i = 1000; i < 2000; ++i)
-        for (int j = 1000; j < 2000; ++j)
-        assert((testComparisons(
-            year_month_day_last{year{i}, month_day_last{January}},
-            year_month_day_last{year{j}, month_day_last{January}},
-            i == j, i < j )));
-
-    return 0;
+constexpr bool test() {
+  using year                = std::chrono::year;
+  using month               = std::chrono::month;
+  using month_day_last      = std::chrono::month_day_last;
+  using year_month_day_last = std::chrono::year_month_day_last;
+
+  constexpr month January  = std::chrono::January;
+  constexpr month February = std::chrono::February;
+
+  assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
+                   year_month_day_last{year{1234}, month_day_last{January}},
+                   std::strong_ordering::equal));
+
+  //  different month
+  assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
+                   year_month_day_last{year{1234}, month_day_last{February}},
+                   std::strong_ordering::less));
+
+  //  different year
+  assert(testOrder(year_month_day_last{year{1234}, month_day_last{January}},
+                   year_month_day_last{year{1235}, month_day_last{January}},
+                   std::strong_ordering::less));
+
+  //  different year and month
+  assert(testOrder(year_month_day_last{year{1234}, month_day_last{February}},
+                   year_month_day_last{year{1235}, month_day_last{January}},
+                   std::strong_ordering::less));
+
+  //  same year, different months
+  for (unsigned i = 1; i < 12; ++i)
+    for (unsigned j = 1; j < 12; ++j)
+      assert((testOrder(year_month_day_last{year{1234}, month_day_last{month{i}}},
+                        year_month_day_last{year{1234}, month_day_last{month{j}}},
+                        i == j  ? std::strong_ordering::equal
+                        : i < j ? std::strong_ordering::less
+                                : std::strong_ordering::greater)));
+
+  //  same month, different years
+  for (int i = 1000; i < 20; ++i)
+    for (int j = 1000; j < 20; ++j)
+      assert((testOrder(year_month_day_last{year{i}, month_day_last{January}},
+                        year_month_day_last{year{j}, month_day_last{January}},
+                        i == j  ? std::strong_ordering::equal
+                        : i < j ? std::strong_ordering::less
+                                : std::strong_ordering::greater)));
+  return true;
+}
+
+int main(int, char**) {
+  using year_month_day_last = std::chrono::year_month_day_last;
+  AssertOrderAreNoexcept<year_month_day_last>();
+  AssertOrderReturn<std::strong_ordering, year_month_day_last>();
+
+  test();
+  static_assert(test());
+
+  return 0;
 }

41f7bb9 claimed it implemented this
change but the code was not adjusted. The other spaceship operators in the
calendar code have been validated too.

Implements parts of
- P1614R2 The Mothership has Landed
@mordante mordante force-pushed the review/finishes_chrono_calendar_spaceship branch from cd21c27 to 6d624bf Compare July 10, 2024 08:02
@mordante mordante added this to the LLVM 19.X Release milestone Jul 10, 2024
@mordante mordante merged commit a05724a into llvm:main Jul 16, 2024
57 checks passed
@mordante mordante deleted the review/finishes_chrono_calendar_spaceship branch July 16, 2024 17:01
sayhaan pushed a commit to sayhaan/llvm-project that referenced this pull request Jul 16, 2024
Summary:
41f7bb9 claimed it implemented this
change but the code was not adjusted. The other spaceship operators in
the calendar code have been validated too.

Implements parts of
- P1614R2 The Mothership has Landed

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D59822456
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
41f7bb9 claimed it implemented this
change but the code was not adjusted. The other spaceship operators in
the calendar code have been validated too.

Implements parts of
- P1614R2 The Mothership has Landed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants