[libcxx-commits] [libcxx] [libc++][chono] Use hidden friends for leap_second comparison. (PR #104713)

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Aug 18 08:17:39 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

<details>
<summary>Changes</summary>

The function

    template<class Duration>
      requires three_way_comparable_with<sys_seconds, sys_time<Duration>>
      constexpr auto operator<=>(const leap_second& x, const sys_time<Duration>& y) noexcept;

Has a recursive constrained. This caused an infinite loop in GCC and is now hit by https://github.com/llvm/llvm-project/pull/102857.

A fix would be to make this function a hidden friend, this solution is propsed in LWG4139.

For consistency all comparisons are made hidden friends. Since the issue causes compilation failures no additional test are needed.

Fixes: https://github.com/llvm/llvm-project/issues/104700

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


2 Files Affected:

- (modified) libcxx/docs/Status/Cxx2cIssues.csv (+1) 
- (modified) libcxx/include/__chrono/leap_second.h (+68-63) 


``````````diff
diff --git a/libcxx/docs/Status/Cxx2cIssues.csv b/libcxx/docs/Status/Cxx2cIssues.csv
index 60c6dc532dc719..3e49738443db5b 100644
--- a/libcxx/docs/Status/Cxx2cIssues.csv
+++ b/libcxx/docs/Status/Cxx2cIssues.csv
@@ -77,4 +77,5 @@
 "`LWG4106 <https://wg21.link/LWG4106>`__","``basic_format_args`` should not be default-constructible","2024-06 (St. Louis)","|Complete|","19.0","|format|"
 "","","","","",""
 "`LWG3343 <https://wg21.link/LWG3343>`__","Ordering of calls to ``unlock()`` and ``notify_all()`` in Effects element of ``notify_all_at_thread_exit()`` should be reversed","Not Adopted Yet","|Complete|","16.0",""
+"`LWG3343 <https://wg21.link/LWG3343>`__","ยง[time.zone.leap] recursive constraint in <=>","Not Adopted Yet","|Complete|","20.0",""
 "","","","","",""
diff --git a/libcxx/include/__chrono/leap_second.h b/libcxx/include/__chrono/leap_second.h
index 1a0e7f3107de81..d79111ed8eecfc 100644
--- a/libcxx/include/__chrono/leap_second.h
+++ b/libcxx/include/__chrono/leap_second.h
@@ -50,70 +50,75 @@ class leap_second {
 private:
   sys_seconds __date_;
   seconds __value_;
-};
 
-_LIBCPP_HIDE_FROM_ABI inline constexpr bool operator==(const leap_second& __x, const leap_second& __y) {
-  return __x.date() == __y.date();
-}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr strong_ordering operator<=>(const leap_second& __x, const leap_second& __y) {
-  return __x.date() <=> __y.date();
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator==(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return __x.date() == __y;
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return __x.date() < __y;
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator<(const sys_time<_Duration>& __x, const leap_second& __y) {
-  return __x < __y.date();
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return __y < __x;
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator>(const sys_time<_Duration>& __x, const leap_second& __y) {
-  return __y < __x;
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return !(__y < __x);
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator<=(const sys_time<_Duration>& __x, const leap_second& __y) {
-  return !(__y < __x);
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return !(__x < __y);
-}
-
-template <class _Duration>
-_LIBCPP_HIDE_FROM_ABI constexpr bool operator>=(const sys_time<_Duration>& __x, const leap_second& __y) {
-  return !(__x < __y);
-}
-
-#    ifndef _LIBCPP_COMPILER_GCC
-// This requirement cause a compilation loop in GCC-13 and running out of memory.
-// TODO TZDB Test whether GCC-14 fixes this.
-template <class _Duration>
-  requires three_way_comparable_with<sys_seconds, sys_time<_Duration>>
-_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(const leap_second& __x, const sys_time<_Duration>& __y) {
-  return __x.date() <=> __y;
-}
-#    endif
+  // The function
+  //   template<class Duration>
+  //    requires three_way_comparable_with<sys_seconds, sys_time<Duration>>
+  //    constexpr auto operator<=>(const leap_second& x, const sys_time<Duration>& y) noexcept;
+  //
+  // Has constraints that are recursive (LWG4139). The proposed resolution is
+  // to make the funcion a hidden friend. For consistency make this change for
+  // all comparison functions.
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const leap_second& __y) {
+    return __x.date() == __y.date();
+  }
+
+  _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering operator<=>(const leap_second& __x, const leap_second& __y) {
+    return __x.date() <=> __y.date();
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return __x.date() == __y;
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return __x.date() < __y;
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const sys_time<_Duration>& __x, const leap_second& __y) {
+    return __x < __y.date();
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return __y < __x;
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const sys_time<_Duration>& __x, const leap_second& __y) {
+    return __y < __x;
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return !(__y < __x);
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const sys_time<_Duration>& __x, const leap_second& __y) {
+    return !(__y < __x);
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return !(__x < __y);
+  }
+
+  template <class _Duration>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const sys_time<_Duration>& __x, const leap_second& __y) {
+    return !(__x < __y);
+  }
+
+  template <class _Duration>
+    requires three_way_comparable_with<sys_seconds, sys_time<_Duration>>
+  _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const leap_second& __x, const sys_time<_Duration>& __y) {
+    return __x.date() <=> __y;
+  }
+};
 
 } // namespace chrono
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/104713


More information about the libcxx-commits mailing list