[libcxx-commits] [libcxx] [libc++] Implement part of P2562R1: constexpr `ranges::stable_sort` (PR #128860)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 26 03:11:55 PST 2025


https://github.com/frederick-vs-ja created https://github.com/llvm/llvm-project/pull/128860

Drive-by: Unblocks test coverage for `ranges::stable_sort` with proxy iterators.

Fixes #119395.

>From 40f6ed843a7319f6244aa9fa5ac357cdde4e5b7e Mon Sep 17 00:00:00 2001
From: "A. Jiang" <de34 at live.cn>
Date: Wed, 26 Feb 2025 19:09:01 +0800
Subject: [PATCH] [libc++] P2562R1: constexpr `ranges::stable_sort`

---
 .../include/__algorithm/ranges_stable_sort.h  |  8 +++--
 libcxx/include/algorithm                      |  5 +--
 .../stable.sort/ranges.stable.sort.pass.cpp   | 36 ++++++++++---------
 .../ranges_robust_against_dangling.pass.cpp   |  4 +++
 ...es_robust_against_omitting_invoke.pass.cpp |  4 +++
 ...es_robust_against_proxy_iterators.pass.cpp |  4 +++
 6 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/libcxx/include/__algorithm/ranges_stable_sort.h b/libcxx/include/__algorithm/ranges_stable_sort.h
index 9c7df80ae9872..6e17d0d0c7ec4 100644
--- a/libcxx/include/__algorithm/ranges_stable_sort.h
+++ b/libcxx/include/__algorithm/ranges_stable_sort.h
@@ -41,7 +41,8 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 namespace ranges {
 struct __stable_sort {
   template <class _Iter, class _Sent, class _Comp, class _Proj>
-  _LIBCPP_HIDE_FROM_ABI static _Iter __stable_sort_fn_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
+  _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR_SINCE_CXX26 _Iter
+  __stable_sort_fn_impl(_Iter __first, _Sent __last, _Comp& __comp, _Proj& __proj) {
     auto __last_iter = ranges::next(__first, __last);
 
     auto&& __projected_comp = std::__make_projected(__comp, __proj);
@@ -52,13 +53,14 @@ struct __stable_sort {
 
   template <random_access_iterator _Iter, sentinel_for<_Iter> _Sent, class _Comp = ranges::less, class _Proj = identity>
     requires sortable<_Iter, _Comp, _Proj>
-  _LIBCPP_HIDE_FROM_ABI _Iter operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _Iter
+  operator()(_Iter __first, _Sent __last, _Comp __comp = {}, _Proj __proj = {}) const {
     return __stable_sort_fn_impl(std::move(__first), std::move(__last), __comp, __proj);
   }
 
   template <random_access_range _Range, class _Comp = ranges::less, class _Proj = identity>
     requires sortable<iterator_t<_Range>, _Comp, _Proj>
-  _LIBCPP_HIDE_FROM_ABI borrowed_iterator_t<_Range>
+  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 borrowed_iterator_t<_Range>
   operator()(_Range&& __r, _Comp __comp = {}, _Proj __proj = {}) const {
     return __stable_sort_fn_impl(ranges::begin(__r), ranges::end(__r), __comp, __proj);
   }
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 7b4cb8e496196..8ef2ba84e6cc5 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -422,11 +422,12 @@ namespace ranges {
   template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
           class Proj = identity>
     requires sortable<I, Comp, Proj>
-    I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {});                 // since C++20
+    constexpr I                                                                             // constexpr in C++26
+      ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {});                 // since C++20
 
   template<random_access_range R, class Comp = ranges::less, class Proj = identity>
     requires sortable<iterator_t<R>, Comp, Proj>
-    borrowed_iterator_t<R>
+    constexpr borrowed_iterator_t<R>                                                        // constexpr in C++26
       ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {});                           // since C++20
 
   template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
index c8f76b20c6825..5dedd54a4c009 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/stable.sort/ranges.stable.sort.pass.cpp
@@ -13,11 +13,12 @@
 // template<random_access_iterator I, sentinel_for<I> S, class Comp = ranges::less,
 //         class Proj = identity>
 //   requires sortable<I, Comp, Proj>
-//   I ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {});                 // since C++20
+//   constexpr I                                                                             // constexpr since C++26
+//     ranges::stable_sort(I first, S last, Comp comp = {}, Proj proj = {});                 // since C++20
 //
 // template<random_access_range R, class Comp = ranges::less, class Proj = identity>
 //   requires sortable<iterator_t<R>, Comp, Proj>
-//   borrowed_iterator_t<R>
+//   constexpr borrowed_iterator_t<R>                                                        // constexpr since C++26
 //     ranges::stable_sort(R&& r, Comp comp = {}, Proj proj = {});                           // since C++20
 
 #include <algorithm>
@@ -57,7 +58,7 @@ static_assert(!HasStableSortR<UncheckedRange<int*>, BadComparator>);
 static_assert(!HasStableSortR<UncheckedRange<const int*>>); // Doesn't satisfy `sortable`.
 
 template <class Iter, class Sent, std::size_t N>
-void test_one(std::array<int, N> input, std::array<int, N> expected) {
+TEST_CONSTEXPR_CXX26 void test_one(std::array<int, N> input, std::array<int, N> expected) {
   { // (iterator, sentinel) overload.
     auto sorted = input;
     auto b = Iter(sorted.data());
@@ -81,7 +82,7 @@ void test_one(std::array<int, N> input, std::array<int, N> expected) {
 }
 
 template <class Iter, class Sent>
-void test_iterators_2() {
+TEST_CONSTEXPR_CXX26 void test_iterators_2() {
   // Empty sequence.
   test_one<Iter, Sent, 0>({}, {});
   // 1-element sequence.
@@ -105,25 +106,25 @@ void test_iterators_2() {
 }
 
 template <class Iter>
-void test_iterators_1() {
+TEST_CONSTEXPR_CXX26 void test_iterators_1() {
   test_iterators_2<Iter, Iter>();
   test_iterators_2<Iter, sentinel_wrapper<Iter>>();
 }
 
-void test_iterators() {
+TEST_CONSTEXPR_CXX26 void test_iterators() {
   test_iterators_1<random_access_iterator<int*>>();
   test_iterators_1<contiguous_iterator<int*>>();
   test_iterators_1<int*>();
 }
 
-void test() {
+TEST_CONSTEXPR_CXX26 bool test() {
   test_iterators();
 
   struct OrderedValue {
     int value;
     double original_order;
     bool operator==(const OrderedValue&) const = default;
-    auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
+    TEST_CONSTEXPR_CXX26 auto operator<=>(const OrderedValue& rhs) const { return value <=> rhs.value; }
   };
 
   { // The sort is stable (equivalent elements remain in the same order).
@@ -214,24 +215,24 @@ void test() {
   { // `std::invoke` is used in the implementation.
     struct S {
       int i;
-      S(int i_) : i(i_) {}
+      TEST_CONSTEXPR_CXX26 S(int i_) : i(i_) {}
 
-      bool comparator(const S& rhs) const { return i < rhs.i; }
-      const S& projection() const { return *this; }
+      TEST_CONSTEXPR_CXX26 bool comparator(const S& rhs) const { return i < rhs.i; }
+      TEST_CONSTEXPR_CXX26 const S& projection() const { return *this; }
 
       bool operator==(const S&) const = default;
     };
 
     {
       std::array in = {S{2}, S{3}, S{1}};
-      auto last = std::ranges::stable_sort(in.begin(), in.end(), &S::comparator, &S::projection);
+      auto last     = std::ranges::stable_sort(in.begin(), in.end(), &S::comparator, &S::projection);
       assert((in == std::array{S{1}, S{2}, S{3}}));
       assert(last == in.end());
     }
 
     {
       std::array in = {S{2}, S{3}, S{1}};
-      auto last = std::ranges::stable_sort(in, &S::comparator, &S::projection);
+      auto last     = std::ranges::stable_sort(in, &S::comparator, &S::projection);
       assert((in == std::array{S{1}, S{2}, S{3}}));
       assert(last == in.end());
     }
@@ -242,8 +243,6 @@ void test() {
         std::ranges::stable_sort(std::array{1, 2, 3});
   }
 
-  // TODO: Enable the tests once the implementation switched to use iter_move/iter_swap
-  /*
   { // ProxyIterator
     {
       std::array in = {2, 1, 3};
@@ -260,12 +259,15 @@ void test() {
       assert((in == std::array{1, 2, 3}));
     }
   }
-  */
+
+  return true;
 }
 
 int main(int, char**) {
   test();
-  // Note: `stable_sort` is not `constexpr`.
+#if TEST_STD_VER >= 26
+  static_assert(test());
+#endif
 
   return 0;
 }
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index 0624a6c2d49c7..299478da22072 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -200,8 +200,12 @@ constexpr bool test_all() {
   if (!std::is_constant_evaluated())
     dangling_1st(std::ranges::stable_partition, in, unary_pred);
   dangling_1st(std::ranges::sort, in);
+#if TEST_STD_VER < 26
   if (!std::is_constant_evaluated())
+#endif
+  {
     dangling_1st(std::ranges::stable_sort, in);
+  }
   dangling_1st(std::ranges::partial_sort, in, mid);
   dangling_1st(std::ranges::nth_element, in, mid);
   if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
index acd7640b418eb..a01cdf1d3a179 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_omitting_invoke.pass.cpp
@@ -167,8 +167,12 @@ constexpr bool test_all() {
   if (!std::is_constant_evaluated())
     test(std::ranges::stable_partition, in, &Foo::unary_pred, &Bar::val);
   test(std::ranges::sort, in, &Foo::binary_pred, &Bar::val);
+#if TEST_STD_VER < 26
   if (!std::is_constant_evaluated())
+#endif
+  {
     test(std::ranges::stable_sort, in, &Foo::binary_pred, &Bar::val);
+  }
   test_mid(std::ranges::partial_sort, in, mid, &Foo::binary_pred, &Bar::val);
   test_mid(std::ranges::nth_element, in, mid, &Foo::binary_pred, &Bar::val);
   if (!std::is_constant_evaluated())
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index ca1433b778751..d85052c51973a 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -170,8 +170,12 @@ constexpr void run_tests() {
   if (!std::is_constant_evaluated())
     test(std::ranges::stable_partition, in, unary_pred);
   test(std::ranges::sort, in);
+#if TEST_STD_VER < 26
   if (!std::is_constant_evaluated())
+#endif
+  {
     test(std::ranges::stable_sort, in);
+  }
   test_mid(std::ranges::partial_sort, in, mid);
   test_mid(std::ranges::nth_element, in, mid);
   if (!std::is_constant_evaluated())



More information about the libcxx-commits mailing list