[clang-tools-extra] [llvm] [clang] [libcxx] [libcxx] adds ranges::fold_left_with_iter and ranges::fold_left (PR #75259)

Christopher Di Bella via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 18 17:32:35 PST 2023


https://github.com/cjdb updated https://github.com/llvm/llvm-project/pull/75259

>From e36a06d3b88c1f2d2169080fc242ee3203a6942d Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 6 Dec 2023 22:41:33 +0000
Subject: [PATCH 01/17] [libcxx] extends test range types to take a value-type

This increases the types' versatility so that they're not restricted
just to `int*`.
---
 .../range.ref.view/borrowing.compile.pass.cpp |  2 +-
 libcxx/test/support/test_range.h              | 54 ++++++++++---------
 2 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp b/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
index 4405742eb023a1..2887ef10b09786 100644
--- a/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
+++ b/libcxx/test/std/ranges/range.adaptors/range.all/range.ref.view/borrowing.compile.pass.cpp
@@ -15,6 +15,6 @@
 
 #include "test_range.h"
 
-static_assert(std::ranges::borrowed_range<std::ranges::ref_view<BorrowedRange>>);
+static_assert(std::ranges::borrowed_range<std::ranges::ref_view<BorrowedRange<>>>);
 static_assert(std::ranges::borrowed_range<std::ranges::ref_view<BorrowedView>>);
 static_assert(std::ranges::borrowed_range<std::ranges::ref_view<NonBorrowedView>>);
diff --git a/libcxx/test/support/test_range.h b/libcxx/test/support/test_range.h
index eea8ce16ce7fa3..22bed476666db2 100644
--- a/libcxx/test/support/test_range.h
+++ b/libcxx/test/support/test_range.h
@@ -22,55 +22,57 @@ struct sentinel {
   bool operator==(std::input_or_output_iterator auto const&) const;
 };
 
-template <template <class...> class I>
-requires std::input_or_output_iterator<I<int*> >
+template <template <class...> class I, class T = int>
+  requires std::input_or_output_iterator<I<T*> >
 struct test_range {
-  I<int*> begin();
-  I<int const*> begin() const;
+  I<T*> begin();
+  I<T const*> begin() const;
   sentinel end();
   sentinel end() const;
 };
 
-template <template <class...> class I>
-requires std::input_or_output_iterator<I<int*> >
+template <template <class...> class I, class T = int>
+  requires std::input_or_output_iterator<I<T*> >
 struct test_non_const_range {
-  I<int*> begin();
+  I<T*> begin();
   sentinel end();
 };
 
-template <template <class...> class I>
-requires std::input_or_output_iterator<I<int*> >
+template <template <class...> class I, class T = int>
+  requires std::input_or_output_iterator<I<T*> >
 struct test_common_range {
-  I<int*> begin();
-  I<int const*> begin() const;
-  I<int*> end();
-  I<int const*> end() const;
+  I<T*> begin();
+  I<T const*> begin() const;
+  I<T*> end();
+  I<T const*> end() const;
 };
 
-template <template <class...> class I>
-requires std::input_or_output_iterator<I<int*> >
+template <template <class...> class I, class T = int>
+  requires std::input_or_output_iterator<I<T*> >
 struct test_non_const_common_range {
-  I<int*> begin();
-  I<int*> end();
+  I<T*> begin();
+  I<T*> end();
 };
 
-template <template <class...> class I>
-requires std::input_or_output_iterator<I<int*> >
+template <template <class...> class I, class T = int>
+  requires std::input_or_output_iterator<I<T*> >
 struct test_view : std::ranges::view_base {
-  I<int*> begin();
-  I<int const*> begin() const;
+  I<T*> begin();
+  I<T const*> begin() const;
   sentinel end();
   sentinel end() const;
 };
 
+template <class T = int>
 struct BorrowedRange {
-  int *begin() const;
-  int *end() const;
+  T* begin() const;
+  T* end() const;
   BorrowedRange(BorrowedRange&&) = delete;
 };
-template<> inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange> = true;
-static_assert(!std::ranges::view<BorrowedRange>);
-static_assert(std::ranges::borrowed_range<BorrowedRange>);
+template <class T>
+inline constexpr bool std::ranges::enable_borrowed_range<BorrowedRange<T>> = true;
+static_assert(!std::ranges::view<BorrowedRange<>>);
+static_assert(std::ranges::borrowed_range<BorrowedRange<>>);
 
 using BorrowedView = std::ranges::empty_view<int>;
 static_assert(std::ranges::view<BorrowedView>);

>From b373740807a7426c3089d8e864f7f71b1c398010 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 13 Dec 2023 00:15:37 +0000
Subject: [PATCH 02/17] [libcxx] adds `ranges::fold_left_with_iter` and
 `ranges::fold_left`

---
 libcxx/include/CMakeLists.txt                 |   1 +
 libcxx/include/__algorithm/fold.h             | 118 ++++++++++++
 libcxx/include/algorithm                      |   1 +
 libcxx/include/module.modulemap.in            |   1 +
 .../ranges.nodiscard_extensions.verify.cpp    |  13 ++
 .../fold_left/requirements.compile.pass.cpp   |  99 ++++++++++
 .../fold_left/return_types.compile.pass.cpp   | 171 +++++++++++++++++
 .../alg.fold/fold_left/valid.pass.cpp         |  93 ++++++++++
 .../requirements.compile.pass.cpp             | 107 +++++++++++
 .../return_types.compile.pass.cpp             | 174 ++++++++++++++++++
 .../fold_left_with_iter/valid.pass.cpp        | 116 ++++++++++++
 .../alg.nonmodifying/alg.fold/gaussian_sum.h  |  19 ++
 .../alg.nonmodifying/alg.fold/requirements.h  |  82 +++++++++
 .../input_iterator.compile.pass.cpp           |   1 +
 .../niebloid.compile.pass.cpp                 |   4 +
 15 files changed, 1000 insertions(+)
 create mode 100644 libcxx/include/__algorithm/fold.h
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d8faf6467b79ae..95702288a3c2f3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -22,6 +22,7 @@ set(files
   __algorithm/find_first_of.h
   __algorithm/find_if.h
   __algorithm/find_if_not.h
+  __algorithm/fold.h
   __algorithm/for_each.h
   __algorithm/for_each_n.h
   __algorithm/for_each_segment.h
diff --git a/libcxx/include/__algorithm/fold.h b/libcxx/include/__algorithm/fold.h
new file mode 100644
index 00000000000000..ad4e9820b479f6
--- /dev/null
+++ b/libcxx/include/__algorithm/fold.h
@@ -0,0 +1,118 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___ALGORITHM_FOLD_H
+#define _LIBCPP___ALGORITHM_FOLD_H
+
+#include <__concepts/assignable.h>
+#include <__concepts/convertible_to.h>
+#include <__concepts/invocable.h>
+#include <__concepts/movable.h>
+#include <__config>
+#include <__functional/invoke.h>
+#include <__functional/reference_wrapper.h>
+#include <__iterator/concepts.h>
+#include <__iterator/iterator_traits.h>
+#include <__iterator/next.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/dangling.h>
+#include <__type_traits/decay.h>
+#include <__type_traits/invoke.h>
+#include <__utility/forward.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 23
+
+namespace ranges {
+template <class _Ip, class _Tp>
+struct in_value_result {
+  _Ip in;
+  _Tp result;
+};
+
+template <class _Ip, class _Tp>
+using fold_left_with_iter_result = in_value_result<_Ip, _Tp>;
+
+template <class _Fp, class _Tp, class _Ip, class _Rp, class _Up = decay_t<_Rp>>
+concept __indirectly_binary_left_foldable_impl =
+    convertible_to<_Rp, _Up> &&                    //
+    movable<_Tp> &&                                //
+    movable<_Up> &&                                //
+    convertible_to<_Tp, _Up> &&                    //
+    invocable<_Fp&, _Up, iter_reference_t<_Ip>> && //
+    assignable_from<_Up&, invoke_result_t<_Fp&, _Up, iter_reference_t<_Ip>>>;
+
+template <class _Fp, class _Tp, class _Ip>
+concept __indirectly_binary_left_foldable =
+    copy_constructible<_Fp> &&                     //
+    invocable<_Fp&, _Tp, iter_reference_t<_Ip>> && //
+    __indirectly_binary_left_foldable_impl<_Fp, _Tp, _Ip, invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
+
+struct __fold_left_with_iter {
+  template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto
+  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) const {
+    using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
+
+    if (__first == __last) {
+      return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), _Up(std::move(__init))};
+    }
+
+    _Up __result = std::invoke(__f, std::move(__init), *__first);
+    for (++__first; __first != __last; ++__first) {
+      __result = std::invoke(__f, std::move(__result), *__first);
+    }
+
+    return fold_left_with_iter_result<_Ip, _Up>{std::move(__first), std::move(__result)};
+  }
+
+  template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) const {
+    auto __result = (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
+
+    using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;
+    return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{
+        std::move(__result.in), std::move(__result.result)};
+  }
+};
+
+inline namespace __cpo {
+inline constexpr auto fold_left_with_iter = __fold_left_with_iter();
+} // namespace __cpo
+
+struct __fold_left {
+  template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto
+  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) const {
+    return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).result;
+  }
+
+  template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) const {
+    return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).result;
+  }
+};
+
+inline namespace __cpo {
+inline constexpr auto fold_left = __fold_left();
+} // namespace __cpo
+} // namespace ranges
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___ALGORITHM_FOLD_H
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 627e7d20213fe8..3fd9ba10f6310b 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -1778,6 +1778,7 @@ template <class BidirectionalIterator, class Compare>
 #include <__algorithm/find_first_of.h>
 #include <__algorithm/find_if.h>
 #include <__algorithm/find_if_not.h>
+#include <__algorithm/fold.h>
 #include <__algorithm/for_each.h>
 #include <__algorithm/for_each_n.h>
 #include <__algorithm/generate.h>
diff --git a/libcxx/include/module.modulemap.in b/libcxx/include/module.modulemap.in
index 90ee7fbb2157c2..359de41fdd128d 100644
--- a/libcxx/include/module.modulemap.in
+++ b/libcxx/include/module.modulemap.in
@@ -665,6 +665,7 @@ module std_private_algorithm_find_end                                    [system
 module std_private_algorithm_find_first_of                               [system] { header "__algorithm/find_first_of.h" }
 module std_private_algorithm_find_if                                     [system] { header "__algorithm/find_if.h" }
 module std_private_algorithm_find_if_not                                 [system] { header "__algorithm/find_if_not.h" }
+module std_private_algorithm_fold                                        [system] { header "__algorithm/fold.h" }
 module std_private_algorithm_for_each                                    [system] { header "__algorithm/for_each.h" }
 module std_private_algorithm_for_each_n                                  [system] { header "__algorithm/for_each_n.h" }
 module std_private_algorithm_for_each_segment                            [system] { header "__algorithm/for_each_segment.h" }
diff --git a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
index d11dcfcd0d2e41..f0a0e4889a7603 100644
--- a/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/ranges.nodiscard_extensions.verify.cpp
@@ -12,6 +12,8 @@
 
 #include <algorithm>
 
+#include "test_macros.h"
+
 void test() {
   int range[1];
   int* iter = range;
@@ -87,4 +89,15 @@ void test() {
   std::ranges::unique(iter, iter); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::ranges::upper_bound(range, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::ranges::upper_bound(iter, iter, 1); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
+
+#if TEST_STD_VER >= 23
+  std::ranges::fold_left(range, 0, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::fold_left(iter, iter, 0, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::fold_left_with_iter(range, 0, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::fold_left_with_iter(iter, iter, 0, std::plus());
+  // expected-warning at -1{{ignoring return value of function declared with 'nodiscard' attribute}}
+#endif
 }
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
new file mode 100644
index 00000000000000..0b1c1e4eae1ea5
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
@@ -0,0 +1,99 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// Covers indirectly_readable<I> too
+template <std::input_or_output_iterator T>
+  requires(!std::input_iterator<T>)
+void requires_input_iterator() {
+  static_assert(!requires(T t) { std::ranges::fold_left(t, std::unreachable_sentinel, 0, std::plus()); });
+}
+
+template <std::equality_comparable T>
+  requires(!std::sentinel_for<int*, T>)
+void requires_sentinel() {
+  static_assert(!requires(T first, T last) { std::ranges::fold_left(first, last, 0, std::plus()); });
+}
+
+template <class F>
+  requires(!std::copy_constructible<F>)
+void requires_copy_constructible_F() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, std::move(f)); });
+}
+
+template <class F>
+  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
+void requires_raw_invocable() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, f); });
+}
+
+template <class F>
+  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
+                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
+void requires_decaying_invoke_result() {
+  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left(first, last, init, f); });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_init() {
+  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
+    std::ranges::fold_left(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_decayed() {
+  static_assert(!requires(T* first, T* last) { std::ranges::fold_left(first, last, 0, std::minus()); });
+}
+
+template <class T>
+  requires(!std::convertible_to<T, int>)
+void requires_init_is_convertible_to_decayed() {
+  static_assert(!requires(int* first, int* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+template <class T>
+  requires(!std::invocable<std::plus<>&, T, T&>)
+void requires_invocable_with_decayed() {
+  static_assert(!requires(T* first, T* last, int init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+template <class T>
+  requires(!std::assignable_from<T&, T volatile&>)
+void requires_assignable_from_invoke_result() {
+  static_assert(!requires(T* first, T* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+void test() {
+  requires_input_iterator<bad_iterator_category>();
+  requires_sentinel<cpp17_input_iterator<int*>>();
+  requires_copy_constructible_F<non_copy_constructible_callable>();
+  requires_raw_invocable<not_invocable>();
+  requires_decaying_invoke_result<non_decayable_result>();
+  requires_movable_init<non_movable>();
+  requires_movable_decayed<copyable_non_movable>();
+  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
+  requires_invocable_with_decayed<not_invocable_with_decayed>();
+  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
new file mode 100644
index 00000000000000..911ce1615a1c75
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
@@ -0,0 +1,171 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left`'s return type is correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "test_range.h"
+
+template <class Result, class T>
+concept is_T = std::same_as<Result, T>;
+
+using std::ranges::fold_left;
+[[maybe_unused]] auto f = [](int x, double y) { return x * y; };
+
+struct Int {
+  int value;
+};
+
+struct Long {
+  int value;
+
+  Long plus(Int) const;
+};
+
+namespace sentinel_based_ranges {
+template <class T>
+using cpp17_input_range = test_range<cpp17_input_iterator, T>;
+
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
+});
+
+template <class T>
+using cpp20_input_range = test_range<cpp20_input_iterator, T>;
+
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
+});
+
+template <class T>
+using forward_range = test_range<forward_iterator, T>;
+
+static_assert(requires(forward_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(forward_range<int> r) {
+  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(forward_range<int> r) {
+  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
+});
+
+template <class T>
+using bidirectional_range = test_range<bidirectional_iterator, T>;
+
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left(r, 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
+});
+
+template <class T>
+using random_access_range = test_range<random_access_iterator, T>;
+
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(r, 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
+});
+
+template <class T>
+using contiguous_range = test_range<contiguous_iterator, T>;
+
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0.0f, std::plus()) } -> std::same_as<float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(r, 0.0f, std::plus()) } -> std::same_as<float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(std::move(r), 0.0f, std::plus()) } -> std::same_as<float>;
+});
+} // namespace sentinel_based_ranges
+
+namespace common_ranges {
+template <class T>
+using forward_range = test_common_range<forward_iterator, T>;
+
+static_assert(requires(forward_range<Int> r) {
+  { fold_left(r.begin(), r.end(), Long(0), &Long::plus) } -> std::same_as<Long>;
+});
+static_assert(requires(forward_range<Int> r) {
+  { fold_left(r, Long(0), &Long::plus) } -> std::same_as<Long>;
+});
+static_assert(requires(forward_range<Int> r) {
+  { fold_left(std::move(r), Long(0), &Long::plus) } -> std::same_as<Long>;
+});
+
+template <class T>
+using bidirectional_range = test_common_range<bidirectional_iterator, T>;
+
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
+});
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
+});
+
+template <class T>
+using random_access_range = test_common_range<random_access_iterator, T>;
+
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(r, 0, f) } -> std::same_as<double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
+});
+
+template <class T>
+using contiguous_range = test_common_range<contiguous_iterator, T>;
+
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(r.begin(), r.end(), 0.0f, std::plus()) } -> std::same_as<float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(r, 0.0f, std::plus()) } -> std::same_as<float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left(std::move(r), 0.0f, std::plus()) } -> std::same_as<float>;
+});
+} // namespace common_ranges
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
new file mode 100644
index 00000000000000..ef0fbb4b2a6d39
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
@@ -0,0 +1,93 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<input_iterator I, sentinel_for<I> S, class T,
+//          indirectly-binary-left-foldable<T, I> F>
+//   constexpr see below ranges::fold_left(I first, S last, T init, F f);
+//
+// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+//   constexpr see below ranges::fold_left(R&& r, T init, F f);
+
+#include <algorithm>
+#include <cassert>
+#include <vector>
+#include <functional>
+
+#include "test_range.h"
+#include "../gaussian_sum.h"
+
+constexpr bool test() {
+  {
+    auto data         = std::vector<int>{1, 2, 3, 4};
+    auto const result = std::ranges::fold_left(data.begin(), data.begin(), 0, std::plus());
+
+    assert(result == 0);
+
+    auto range = std::span(data.data(), 0);
+    assert(std::ranges::fold_left(range, 0, std::plus()) == result);
+  }
+  {
+    auto data           = std::vector<int>{1, 2, 3, 4};
+    constexpr auto init = 100.1;
+    auto const result   = std::ranges::fold_left(data.begin(), data.begin(), init, std::plus());
+
+    assert(result == init);
+
+    auto range = std::span(data.data(), 0);
+    assert(std::ranges::fold_left(range, init, std::plus()) == result);
+  }
+  {
+    auto data         = std::vector{1, 3, 5, 7, 9};
+    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0, std::plus());
+
+    assert(result == gaussian_sum(data)); // sum of n ascending odd numbers = n^2
+    assert(std::ranges::fold_left(data, 0, std::plus()) == result);
+  }
+  {
+    auto data         = std::vector{2, 4, 6, 8, 10, 12};
+    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0L, std::plus<long>());
+
+    assert(result == gaussian_sum(data));
+    assert(std::ranges::fold_left(data, 0, std::plus()) == result);
+  }
+  {
+    auto data         = std::vector{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
+    auto plus         = [](int const x, double const y) { return x + y; };
+    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0.0, plus);
+
+    assert(result == -21.6); // int(  0.0) + -1.1 =   0 + -1.1 =  -1.1
+                             // int(- 1.1) + -2.2 = - 1 + -2.2 =  -3.2
+                             // int(- 3.2) + -3.3 = - 3 + -3.3 =  -6.3
+                             // int(- 6.3) + -4.4 = - 6 + -4.4 = -10.4
+                             // int(-10.4) + -5.5 = -10 + -5.5 = -15.5
+                             // int(-15.5) + -6.6 = -15 + -6.6 = -21.6.
+
+    assert(std::ranges::fold_left(data, 0, plus) == result);
+  }
+  {
+    auto data           = std::vector<double>{1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1};
+    auto plus           = [](double const x, double const y) { return static_cast<short>(x + y); };
+    constexpr auto init = 10.0;
+    auto const result   = std::ranges::fold_left(data.begin(), data.end(), init, plus);
+
+    assert(result == static_cast<short>(gaussian_sum(data) + init));
+    assert(std::ranges::fold_left(data, init, plus) == result);
+  }
+
+  return true;
+}
+
+int main() {
+  test();
+  static_assert(test());
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
new file mode 100644
index 00000000000000..c0f647b5fc88f1
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
@@ -0,0 +1,107 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// Covers indirectly_readable<I> too
+template <std::input_or_output_iterator T>
+  requires(!std::input_iterator<T>)
+void requires_input_iterator() {
+  static_assert(!requires(T t) { std::ranges::fold_left_with_iter(t, std::unreachable_sentinel, 0, std::plus()); });
+}
+
+template <std::equality_comparable T>
+  requires(!std::sentinel_for<int*, T>)
+void requires_sentinel() {
+  static_assert(!requires(T first, T last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
+}
+
+template <class F>
+  requires(!std::copy_constructible<F>)
+void requires_copy_constructible_F() {
+  static_assert(!requires(int* first, int* last, F f) {
+    std::ranges::fold_left_with_iter(first, last, 0, std::move(f));
+  });
+}
+
+template <class F>
+  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
+void requires_raw_invocable() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left_with_iter(first, last, 0, f); });
+}
+
+template <class F>
+  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
+                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
+void requires_decaying_invoke_result() {
+  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left_with_iter(first, last, init, f); });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_init() {
+  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_decayed() {
+  static_assert(!requires(T* first, T* last) { std::ranges::fold_left_with_iter(first, last, 0, std::minus()); });
+}
+
+template <class T>
+  requires(!std::convertible_to<T, int>)
+void requires_init_is_convertible_to_decayed() {
+  static_assert(!requires(int* first, int* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::invocable<std::plus<>&, T, T&>)
+void requires_invocable_with_decayed() {
+  static_assert(!requires(T* first, T* last, int init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::assignable_from<T&, T volatile&>)
+void requires_assignable_from_invoke_result() {
+  static_assert(!requires(T* first, T* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+void test() {
+  requires_input_iterator<bad_iterator_category>();
+  requires_sentinel<cpp17_input_iterator<int*>>();
+  requires_copy_constructible_F<non_copy_constructible_callable>();
+  requires_raw_invocable<not_invocable>();
+  requires_decaying_invoke_result<non_decayable_result>();
+  requires_movable_init<non_movable>();
+  requires_movable_decayed<copyable_non_movable>();
+  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
+  requires_invocable_with_decayed<not_invocable_with_decayed>();
+  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
new file mode 100644
index 00000000000000..b930002336294d
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
@@ -0,0 +1,174 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left_with_iter`'s return type is correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "test_range.h"
+
+template <class Result, class Range, class T>
+concept is_in_value_result = std::same_as<Result, std::ranges::in_value_result<std::ranges::iterator_t<Range>, T>>;
+
+template <class Result, class T>
+concept is_dangling_with = std::same_as<Result, std::ranges::in_value_result<std::ranges::dangling, T>>;
+
+using std::ranges::fold_left_with_iter;
+[[maybe_unused]] auto f = [](int x, double y) { return x * y; };
+
+struct Int {
+  int value;
+};
+
+struct Long {
+  int value;
+
+  Long plus(Int) const;
+};
+
+namespace sentinel_based_ranges {
+template <class T>
+using cpp17_input_range = test_range<cpp17_input_iterator, T>;
+
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<cpp17_input_range<int>, int>;
+});
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<cpp17_input_range<int>, int>;
+});
+static_assert(requires(cpp17_input_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
+});
+
+template <class T>
+using cpp20_input_range = test_range<cpp20_input_iterator, T>;
+
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<cpp20_input_range<int>, int>;
+});
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<cpp20_input_range<int>, int>;
+});
+static_assert(requires(cpp20_input_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
+});
+
+template <class T>
+using forward_range = test_range<forward_iterator, T>;
+
+static_assert(requires(forward_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<forward_range<int>, int>;
+});
+static_assert(requires(forward_range<int> r) {
+  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<forward_range<int>, int>;
+});
+static_assert(requires(forward_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
+});
+
+template <class T>
+using bidirectional_range = test_range<bidirectional_iterator, T>;
+
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<bidirectional_range<short>, double>;
+});
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<bidirectional_range<short>, double>;
+});
+static_assert(requires(bidirectional_range<short> r) {
+  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
+});
+
+template <class T>
+using random_access_range = test_range<random_access_iterator, T>;
+
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<random_access_range<int>, double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<random_access_range<int>, double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
+});
+
+template <class T>
+using contiguous_range = test_range<contiguous_iterator, T>;
+
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(r, 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0.0f, std::plus()) } -> is_dangling_with<float>;
+});
+} // namespace sentinel_based_ranges
+
+namespace common_ranges {
+template <class T>
+using forward_range = test_common_range<forward_iterator, T>;
+
+static_assert(requires(forward_range<Int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), Long(0), &Long::plus) } -> is_in_value_result<forward_range<Int>, Long>;
+});
+static_assert(requires(forward_range<Int> r) {
+  { fold_left_with_iter(r, Long(0), &Long::plus) } -> is_in_value_result<forward_range<Int>, Long>;
+});
+static_assert(requires(forward_range<Int> r) {
+  { fold_left_with_iter(std::move(r), Long(0), &Long::plus) } -> is_dangling_with<Long>;
+});
+
+template <class T>
+using bidirectional_range = test_common_range<bidirectional_iterator, T>;
+
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<bidirectional_range<int>, int>;
+});
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<bidirectional_range<int>, int>;
+});
+static_assert(requires(bidirectional_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
+});
+
+template <class T>
+using random_access_range = test_common_range<random_access_iterator, T>;
+
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<random_access_range<int>, double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<random_access_range<int>, double>;
+});
+static_assert(requires(random_access_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
+});
+
+template <class T>
+using contiguous_range = test_common_range<contiguous_iterator, T>;
+
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(r.begin(), r.end(), 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(r, 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
+});
+static_assert(requires(contiguous_range<int> r) {
+  { fold_left_with_iter(std::move(r), 0.0f, std::plus()) } -> is_dangling_with<float>;
+});
+} // namespace common_ranges
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
new file mode 100644
index 00000000000000..d8fb6525119e0c
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
@@ -0,0 +1,116 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<input_iterator I, sentinel_for<I> S, class T,
+//          indirectly-binary-left-foldable<T, I> F>
+//   constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
+//
+// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+//   constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);
+
+#include <algorithm>
+#include <cassert>
+#include <vector>
+#include <functional>
+
+#include "test_range.h"
+#include "../gaussian_sum.h"
+
+constexpr bool test() {
+  {
+    auto data   = std::vector<int>{1, 2, 3, 4};
+    auto result = std::ranges::fold_left_with_iter(data.begin(), data.begin(), 0, std::plus());
+
+    assert(result.in == data.begin());
+    assert(result.result == 0);
+
+    auto range   = std::span(data.data(), 0);
+    auto rresult = std::ranges::fold_left_with_iter(range, 0, std::plus());
+
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+  {
+    auto data           = std::vector<int>{1, 2, 3, 4};
+    constexpr auto init = 100.1;
+    auto result         = std::ranges::fold_left_with_iter(data.begin(), data.begin(), init, std::plus());
+
+    assert(result.in == data.begin());
+    assert(result.result == init);
+
+    auto range   = std::span(data.data(), 0);
+    auto rresult = std::ranges::fold_left_with_iter(range, init, std::plus());
+
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+  {
+    auto data   = std::vector{1, 3, 5, 7, 9};
+    auto result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0, std::plus());
+
+    assert(result.in == data.end());
+    assert(result.result == gaussian_sum(data)); // sum of n ascending odd numbers = n^2
+
+    auto rresult = std::ranges::fold_left_with_iter(data, 0, std::plus());
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+  {
+    auto data         = std::vector{2, 4, 6, 8, 10, 12};
+    auto const result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0L, std::plus<long>());
+
+    assert(result.in == data.end());
+    assert(result.result == gaussian_sum(data));
+
+    auto rresult = std::ranges::fold_left_with_iter(data, 0, std::plus());
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+  {
+    auto data   = std::vector{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
+    auto plus   = [](int const x, double const y) { return x + y; };
+    auto result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0.0, plus);
+
+    assert(result.in == data.end());
+    assert(result.result == -21.6); // int(  0.0) + -1.1 =   0 + -1.1 =  -1.1
+                                    // int(- 1.1) + -2.2 = - 1 + -2.2 =  -3.2
+                                    // int(- 3.2) + -3.3 = - 3 + -3.3 =  -6.3
+                                    // int(- 6.3) + -4.4 = - 6 + -4.4 = -10.4
+                                    // int(-10.4) + -5.5 = -10 + -5.5 = -15.5
+                                    // int(-15.5) + -6.6 = -15 + -6.6 = -21.6.
+
+    auto rresult = std::ranges::fold_left_with_iter(data, 0, plus);
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+  {
+    auto data           = std::vector<double>{1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1};
+    auto plus           = [](double const x, double const y) { return static_cast<short>(x + y); };
+    constexpr auto init = 10.0;
+    auto result         = std::ranges::fold_left_with_iter(data.begin(), data.end(), init, plus);
+
+    assert(result.in == data.end());
+    assert(result.result == static_cast<short>(gaussian_sum(data) + init));
+
+    auto rresult = std::ranges::fold_left_with_iter(data, init, plus);
+    assert(rresult.in == result.in);
+    assert(rresult.result == result.result);
+  }
+
+  return true;
+}
+
+int main() {
+  test();
+  static_assert(test());
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
new file mode 100644
index 00000000000000..060beaa6703b1a
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
@@ -0,0 +1,19 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
+#define LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
+
+#include <vector>
+
+template <class T>
+constexpr auto gaussian_sum(std::vector<T> const& input) {
+  return (static_cast<double>(input.size()) / 2) * (input.front() + input.back());
+}
+
+#endif // LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
new file mode 100644
index 00000000000000..0104c71b11447e
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
@@ -0,0 +1,82 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBCXX_TEST_FOLD_REQUIREMENTS_H
+#define LIBCXX_TEST_FOLD_REQUIREMENTS_H
+
+#include <cstddef>
+
+// FIXME(cjdb): deduplicate
+struct bad_iterator_category {
+  using value_type        = int;
+  using difference_type   = std::ptrdiff_t;
+  using iterator_category = void;
+
+  value_type operator*() const;
+
+  bad_iterator_category& operator++();
+  void operator++(int);
+};
+
+struct non_movable {
+  non_movable(non_movable&&) = delete;
+};
+
+struct copyable_non_movable {
+  copyable_non_movable(int);
+  copyable_non_movable(copyable_non_movable&&) = delete;
+  copyable_non_movable(copyable_non_movable const&);
+
+  friend int operator+(copyable_non_movable const&, non_movable const&);
+  friend int operator+(non_movable const&, copyable_non_movable const&);
+
+  friend copyable_non_movable const& operator-(int, copyable_non_movable const&);
+  friend copyable_non_movable const& operator-(copyable_non_movable const&, int);
+  friend copyable_non_movable const& operator-(copyable_non_movable const&, copyable_non_movable const&);
+};
+
+struct non_copy_constructible_callable {
+  non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
+  non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;
+
+  int operator()(int, int) const;
+};
+
+struct not_invocable {
+  int operator()(int, int&&);
+};
+
+struct S {};
+
+struct non_decayable_result {
+  S volatile& operator()(S, S) const;
+};
+
+struct not_convertible_to_int {
+  friend int operator+(not_convertible_to_int, not_convertible_to_int);
+  friend int operator+(not_convertible_to_int, int);
+  friend int operator+(int, not_convertible_to_int);
+};
+
+struct not_invocable_with_decayed {
+  not_invocable_with_decayed(int);
+  friend not_invocable_with_decayed& operator+(int, not_invocable_with_decayed&);
+  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed&, int);
+  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed volatile&, not_invocable_with_decayed&);
+};
+
+struct not_assignable_to_decayed {
+  not_assignable_to_decayed();
+  not_assignable_to_decayed(not_assignable_to_decayed&);
+  not_assignable_to_decayed(not_assignable_to_decayed const&);
+  not_assignable_to_decayed(not_assignable_to_decayed volatile&);
+  not_assignable_to_decayed(not_assignable_to_decayed const volatile&);
+  friend not_assignable_to_decayed volatile& operator+(not_assignable_to_decayed, not_assignable_to_decayed);
+};
+
+#endif // LIBCXX_TEST_FOLD_REQUIREMENTS
diff --git a/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.input/input_iterator.compile.pass.cpp b/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.input/input_iterator.compile.pass.cpp
index 217c68fea7bc92..0c72c70a72d27f 100644
--- a/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.input/input_iterator.compile.pass.cpp
+++ b/libcxx/test/std/iterators/iterator.requirements/iterator.concepts/iterator.concept.input/input_iterator.compile.pass.cpp
@@ -81,6 +81,7 @@ struct not_indirectly_readable {
 };
 static_assert(!std::indirectly_readable<not_indirectly_readable> && !std::input_iterator<not_indirectly_readable>);
 
+// FIXME(cjdb): deduplicate
 struct bad_iterator_category {
   using value_type = int;
   using difference_type = std::ptrdiff_t;
diff --git a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
index 683f88c19f6784..eeaaa6784353d6 100644
--- a/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
+++ b/libcxx/test/std/library/description/conventions/customization.point.object/niebloid.compile.pass.cpp
@@ -83,6 +83,10 @@ static_assert(test(std::ranges::find_end, a, a));
 static_assert(test(std::ranges::find_first_of, a, a));
 static_assert(test(std::ranges::find_if, a, odd));
 static_assert(test(std::ranges::find_if_not, a, odd));
+#if TEST_STD_VER >= 23
+static_assert(test(std::ranges::fold_left, a, 0, std::plus()));
+static_assert(test(std::ranges::fold_left_with_iter, a, 0, std::plus()));
+#endif
 static_assert(test(std::ranges::for_each, a, odd));
 static_assert(test(std::ranges::for_each_n, a, 10, odd));
 static_assert(test(std::ranges::generate, a, gen));

>From e776610f0b0772bb690e07d07f8fc31832fcb5c7 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 13 Dec 2023 21:22:45 +0000
Subject: [PATCH 03/17] changes to using static opertaor()

---
 libcxx/include/__algorithm/fold.h | 14 +++++++-------
 libcxx/modules/std/algorithm.inc  |  8 +++++++-
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/libcxx/include/__algorithm/fold.h b/libcxx/include/__algorithm/fold.h
index ad4e9820b479f6..7364b676b787c4 100644
--- a/libcxx/include/__algorithm/fold.h
+++ b/libcxx/include/__algorithm/fold.h
@@ -63,8 +63,8 @@ concept __indirectly_binary_left_foldable =
 
 struct __fold_left_with_iter {
   template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto
-  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) const {
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
+  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
     using _Up = decay_t<invoke_result_t<_Fp&, _Tp, iter_reference_t<_Ip>>>;
 
     if (__first == __last) {
@@ -80,8 +80,8 @@ struct __fold_left_with_iter {
   }
 
   template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) const {
-    auto __result = (*this)(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
+    auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
 
     using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;
     return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{
@@ -95,13 +95,13 @@ inline constexpr auto fold_left_with_iter = __fold_left_with_iter();
 
 struct __fold_left {
   template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto
-  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) const {
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
+  operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
     return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).result;
   }
 
   template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
-  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) const {
+  _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
     return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).result;
   }
 };
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index b7900d15c10c2b..dfae1dcaa45de3 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -16,7 +16,7 @@ export namespace std {
     using std::ranges::in_in_result;
     using std::ranges::in_out_out_result;
     using std::ranges::in_out_result;
-    // using std::ranges::in_value_result;
+    using std::ranges::in_value_result;
     using std::ranges::min_max_result;
     // using std::ranges::out_value_result;
   } // namespace ranges
@@ -48,6 +48,12 @@ export namespace std {
   } // namespace ranges
 #endif
 
+  // [alg.fold], fold
+  namespace ranges {
+    using std::ranges::fold_left;
+    using std::ranges::fold_left_with_iter;
+  } // namespace ranges
+
   // [alg.foreach], for each
   using std::for_each;
 

>From 15b686b110db92015f28837133797771641a0d6f Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Thu, 14 Dec 2023 19:14:23 +0000
Subject: [PATCH 04/17] adds missing export

---
 libcxx/modules/std/algorithm.inc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index dfae1dcaa45de3..d58cec27d04acb 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -52,6 +52,7 @@ export namespace std {
   namespace ranges {
     using std::ranges::fold_left;
     using std::ranges::fold_left_with_iter;
+    using std::ranges::fold_left_with_iter_result;
   } // namespace ranges
 
   // [alg.foreach], for each

>From bec1572ab327212aaf57af020678403cf7b1a321 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Thu, 14 Dec 2023 21:07:17 +0000
Subject: [PATCH 05/17] changes requirements test from asserts to verify

---
 .../requirements.compile.pass.cpp             | 107 ---------
 .../requirements.verify.cpp                   | 203 ++++++++++++++++++
 2 files changed, 203 insertions(+), 107 deletions(-)
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
deleted file mode 100644
index c0f647b5fc88f1..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
+++ /dev/null
@@ -1,107 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// Covers indirectly_readable<I> too
-template <std::input_or_output_iterator T>
-  requires(!std::input_iterator<T>)
-void requires_input_iterator() {
-  static_assert(!requires(T t) { std::ranges::fold_left_with_iter(t, std::unreachable_sentinel, 0, std::plus()); });
-}
-
-template <std::equality_comparable T>
-  requires(!std::sentinel_for<int*, T>)
-void requires_sentinel() {
-  static_assert(!requires(T first, T last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
-}
-
-template <class F>
-  requires(!std::copy_constructible<F>)
-void requires_copy_constructible_F() {
-  static_assert(!requires(int* first, int* last, F f) {
-    std::ranges::fold_left_with_iter(first, last, 0, std::move(f));
-  });
-}
-
-template <class F>
-  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
-void requires_raw_invocable() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left_with_iter(first, last, 0, f); });
-}
-
-template <class F>
-  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
-                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
-void requires_decaying_invoke_result() {
-  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left_with_iter(first, last, init, f); });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_init() {
-  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_decayed() {
-  static_assert(!requires(T* first, T* last) { std::ranges::fold_left_with_iter(first, last, 0, std::minus()); });
-}
-
-template <class T>
-  requires(!std::convertible_to<T, int>)
-void requires_init_is_convertible_to_decayed() {
-  static_assert(!requires(int* first, int* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::invocable<std::plus<>&, T, T&>)
-void requires_invocable_with_decayed() {
-  static_assert(!requires(T* first, T* last, int init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::assignable_from<T&, T volatile&>)
-void requires_assignable_from_invoke_result() {
-  static_assert(!requires(T* first, T* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-void test() {
-  requires_input_iterator<bad_iterator_category>();
-  requires_sentinel<cpp17_input_iterator<int*>>();
-  requires_copy_constructible_F<non_copy_constructible_callable>();
-  requires_raw_invocable<not_invocable>();
-  requires_decaying_invoke_result<non_decayable_result>();
-  requires_movable_init<non_movable>();
-  requires_movable_decayed<copyable_non_movable>();
-  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
-  requires_invocable_with_decayed<not_invocable_with_decayed>();
-  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp
new file mode 100644
index 00000000000000..1260094c8074e4
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp
@@ -0,0 +1,203 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_range.h"
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// expected-error@*:* 19 {{no matching function for call to object of type 'const __fold_left_with_iter'}}
+
+void test_iterator() {
+  // expected-note@*:*  10 {{candidate template ignored: constraints not satisfied}}
+  // expected-note@*:*  10 {{candidate function template not viable: requires 3 arguments, but 4 were provided}}
+
+  std::ranges::fold_left_with_iter(bad_iterator_category(), std::unreachable_sentinel, 0, std::plus());
+  // expected-note@*:* {{because 'bad_iterator_category' does not satisfy 'input_iterator'}}
+  // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
+  // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
+
+  {
+    int* first;
+    int* last;
+    std::ranges::fold_left_with_iter(
+        cpp17_input_iterator<int*>(first), cpp17_input_iterator<int*>(last), 0, std::plus());
+    // expected-note@*:* {{because 'sentinel_for<cpp17_input_iterator<int *>, cpp17_input_iterator<int *> >' evaluated to false}}
+    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'semiregular'}}
+    // expected-note@*:* {{'cpp17_input_iterator<int *>' does not satisfy 'default_initializable'}}
+    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'constructible_from'}}
+    // expected-note@*:* {{because 'is_constructible_v<cpp17_input_iterator<int *> >' evaluated to false}}
+
+    std::ranges::fold_left_with_iter(first, last, 0, non_copy_constructible_callable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, int *>' evaluated to false}}
+    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+
+    std::ranges::fold_left_with_iter(first, last, 0, not_invocable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, int *>' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<int *> >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    S* first = nullptr;
+    S* last  = nullptr;
+    std::ranges::fold_left_with_iter(first, last, S(), non_decayable_result());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, S *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, S *, invoke_result_t<non_decayable_result &, S, iter_reference_t<S *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
+  }
+  {
+    copyable_non_movable* first;
+    copyable_non_movable* last;
+    std::ranges::fold_left_with_iter(first, last, non_movable(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, copyable_non_movable *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, copyable_non_movable *, invoke_result_t<plus<void> &, non_movable, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
+  }
+  {
+    copyable_non_movable* first;
+    copyable_non_movable* last;
+    std::ranges::fold_left_with_iter(first, last, 0, std::minus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, copyable_non_movable *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, copyable_non_movable *, invoke_result_t<minus<void> &, int, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+  }
+  {
+    int* first = nullptr;
+    int* last  = nullptr;
+    std::ranges::fold_left_with_iter(first, last, not_convertible_to_int(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, int *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, int *, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<int *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
+  }
+  {
+    not_invocable_with_decayed* first;
+    not_invocable_with_decayed* last;
+    std::ranges::fold_left_with_iter(first, last, 0, std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, not_invocable_with_decayed *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, not_invocable_with_decayed *, invoke_result_t<plus<void> &, int, iter_reference_t<not_invocable_with_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<not_invocable_with_decayed *> >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    not_assignable_to_decayed* first;
+    not_assignable_to_decayed* last;
+    std::ranges::fold_left_with_iter(first, last, not_assignable_to_decayed(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
+  }
+}
+
+void test_fold_range() {
+  // expected-note@*:*  9 {{candidate template ignored: constraints not satisfied}}
+  // expected-note@*:*  9 {{candidate function template not viable: requires 4 arguments, but 3 were provided}}
+
+  {
+    struct bad_range {
+      bad_iterator_category begin();
+      std::unreachable_sentinel_t end();
+    };
+
+    bad_range r;
+    std::ranges::fold_left_with_iter(r, 0, std::plus());
+    // expected-note@*:* {{because 'bad_range &' does not satisfy 'input_range'}}
+    // expected-note@*:* {{because 'iterator_t<bad_range &>' (aka 'bad_iterator_category') does not satisfy 'input_iterator'}}
+    // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
+    // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, int> r;
+
+    std::ranges::fold_left_with_iter(r, 0, non_copy_constructible_callable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+
+    std::ranges::fold_left_with_iter(r, 0, not_invocable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<cpp20_input_iterator<int *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    test_range<cpp20_input_iterator, S> r;
+
+    std::ranges::fold_left_with_iter(r, S(), non_decayable_result());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, iterator_t<test_range<cpp20_input_iterator, S> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, cpp20_input_iterator<S *>, invoke_result_t<non_decayable_result &, S, iter_reference_t<cpp20_input_iterator<S *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, copyable_non_movable> r;
+    std::ranges::fold_left_with_iter(r, non_movable(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<plus<void> &, non_movable, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, copyable_non_movable> r;
+
+    std::ranges::fold_left_with_iter(r, 0, std::minus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<minus<void> &, int, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, int> r;
+    std::ranges::fold_left_with_iter(r, not_convertible_to_int(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, cpp20_input_iterator<int *>, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<cpp20_input_iterator<int *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, not_invocable_with_decayed> r;
+    std::ranges::fold_left_with_iter(r, 0, std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, iterator_t<test_range<cpp20_input_iterator, not_invocable_with_decayed> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, cpp20_input_iterator<not_invocable_with_decayed *>, invoke_result_t<plus<void> &, int, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    test_range<cpp20_input_iterator, not_assignable_to_decayed> r;
+    std::ranges::fold_left_with_iter(r, not_assignable_to_decayed(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, iterator_t<test_range<cpp20_input_iterator, not_assignable_to_decayed> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, cpp20_input_iterator<not_assignable_to_decayed *>, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
+  }
+}

>From 6a57523fa1d33f2e8d221858c38af8765396c1aa Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Thu, 14 Dec 2023 22:40:52 +0000
Subject: [PATCH 06/17] adds missing file

---
 .../std/algorithms/alg.nonmodifying/alg.fold/requirements.h     | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
index 0104c71b11447e..918f84348084c9 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
@@ -24,6 +24,7 @@ struct bad_iterator_category {
 };
 
 struct non_movable {
+  non_movable()              = default;
   non_movable(non_movable&&) = delete;
 };
 
@@ -41,6 +42,7 @@ struct copyable_non_movable {
 };
 
 struct non_copy_constructible_callable {
+  non_copy_constructible_callable()                                       = default;
   non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
   non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;
 

>From 12f36c42514b24a39c02c25bf33c94cf8bef9bc9 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Thu, 14 Dec 2023 23:26:48 +0000
Subject: [PATCH 07/17] repeats for `fold_left`

---
 .../fold_left/requirements.compile.pass.cpp   |  99 ---------
 .../fold_left/requirements.verify.cpp         | 202 ++++++++++++++++++
 2 files changed, 202 insertions(+), 99 deletions(-)
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
deleted file mode 100644
index 0b1c1e4eae1ea5..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// Covers indirectly_readable<I> too
-template <std::input_or_output_iterator T>
-  requires(!std::input_iterator<T>)
-void requires_input_iterator() {
-  static_assert(!requires(T t) { std::ranges::fold_left(t, std::unreachable_sentinel, 0, std::plus()); });
-}
-
-template <std::equality_comparable T>
-  requires(!std::sentinel_for<int*, T>)
-void requires_sentinel() {
-  static_assert(!requires(T first, T last) { std::ranges::fold_left(first, last, 0, std::plus()); });
-}
-
-template <class F>
-  requires(!std::copy_constructible<F>)
-void requires_copy_constructible_F() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, std::move(f)); });
-}
-
-template <class F>
-  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
-void requires_raw_invocable() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, f); });
-}
-
-template <class F>
-  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
-                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
-void requires_decaying_invoke_result() {
-  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left(first, last, init, f); });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_init() {
-  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
-    std::ranges::fold_left(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_decayed() {
-  static_assert(!requires(T* first, T* last) { std::ranges::fold_left(first, last, 0, std::minus()); });
-}
-
-template <class T>
-  requires(!std::convertible_to<T, int>)
-void requires_init_is_convertible_to_decayed() {
-  static_assert(!requires(int* first, int* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-template <class T>
-  requires(!std::invocable<std::plus<>&, T, T&>)
-void requires_invocable_with_decayed() {
-  static_assert(!requires(T* first, T* last, int init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-template <class T>
-  requires(!std::assignable_from<T&, T volatile&>)
-void requires_assignable_from_invoke_result() {
-  static_assert(!requires(T* first, T* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-void test() {
-  requires_input_iterator<bad_iterator_category>();
-  requires_sentinel<cpp17_input_iterator<int*>>();
-  requires_copy_constructible_F<non_copy_constructible_callable>();
-  requires_raw_invocable<not_invocable>();
-  requires_decaying_invoke_result<non_decayable_result>();
-  requires_movable_init<non_movable>();
-  requires_movable_decayed<copyable_non_movable>();
-  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
-  requires_invocable_with_decayed<not_invocable_with_decayed>();
-  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp
new file mode 100644
index 00000000000000..fd1c2529200945
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp
@@ -0,0 +1,202 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_range.h"
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// expected-error@*:* 19 {{no matching function for call to object of type 'const __fold_left'}}
+
+void test_iterator() {
+  // expected-note@*:*  10 {{candidate template ignored: constraints not satisfied}}
+  // expected-note@*:*  10 {{candidate function template not viable: requires 3 arguments, but 4 were provided}}
+
+  std::ranges::fold_left(bad_iterator_category(), std::unreachable_sentinel, 0, std::plus());
+  // expected-note@*:* {{because 'bad_iterator_category' does not satisfy 'input_iterator'}}
+  // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
+  // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
+
+  {
+    int* first;
+    int* last;
+    std::ranges::fold_left(cpp17_input_iterator<int*>(first), cpp17_input_iterator<int*>(last), 0, std::plus());
+    // expected-note@*:* {{because 'sentinel_for<cpp17_input_iterator<int *>, cpp17_input_iterator<int *> >' evaluated to false}}
+    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'semiregular'}}
+    // expected-note@*:* {{'cpp17_input_iterator<int *>' does not satisfy 'default_initializable'}}
+    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'constructible_from'}}
+    // expected-note@*:* {{because 'is_constructible_v<cpp17_input_iterator<int *> >' evaluated to false}}
+
+    std::ranges::fold_left(first, last, 0, non_copy_constructible_callable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, int *>' evaluated to false}}
+    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+
+    std::ranges::fold_left(first, last, 0, not_invocable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, int *>' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<int *> >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    S* first = nullptr;
+    S* last  = nullptr;
+    std::ranges::fold_left(first, last, S(), non_decayable_result());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, S *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, S *, invoke_result_t<non_decayable_result &, S, iter_reference_t<S *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
+  }
+  {
+    copyable_non_movable* first;
+    copyable_non_movable* last;
+    std::ranges::fold_left(first, last, non_movable(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, copyable_non_movable *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, copyable_non_movable *, invoke_result_t<plus<void> &, non_movable, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
+  }
+  {
+    copyable_non_movable* first;
+    copyable_non_movable* last;
+    std::ranges::fold_left(first, last, 0, std::minus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, copyable_non_movable *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, copyable_non_movable *, invoke_result_t<minus<void> &, int, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+  }
+  {
+    int* first = nullptr;
+    int* last  = nullptr;
+    std::ranges::fold_left(first, last, not_convertible_to_int(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, int *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, int *, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<int *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
+  }
+  {
+    not_invocable_with_decayed* first;
+    not_invocable_with_decayed* last;
+    std::ranges::fold_left(first, last, 0, std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, not_invocable_with_decayed *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, not_invocable_with_decayed *, invoke_result_t<plus<void> &, int, iter_reference_t<not_invocable_with_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<not_invocable_with_decayed *> >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    not_assignable_to_decayed* first;
+    not_assignable_to_decayed* last;
+    std::ranges::fold_left(first, last, not_assignable_to_decayed(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *>' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
+  }
+}
+
+void test_fold_range() {
+  // expected-note@*:*  9 {{candidate template ignored: constraints not satisfied}}
+  // expected-note@*:*  9 {{candidate function template not viable: requires 4 arguments, but 3 were provided}}
+
+  {
+    struct bad_range {
+      bad_iterator_category begin();
+      std::unreachable_sentinel_t end();
+    };
+
+    bad_range r;
+    std::ranges::fold_left(r, 0, std::plus());
+    // expected-note@*:* {{because 'bad_range &' does not satisfy 'input_range'}}
+    // expected-note@*:* {{because 'iterator_t<bad_range &>' (aka 'bad_iterator_category') does not satisfy 'input_iterator'}}
+    // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
+    // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, int> r;
+
+    std::ranges::fold_left(r, 0, non_copy_constructible_callable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
+
+    std::ranges::fold_left(r, 0, not_invocable());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<cpp20_input_iterator<int *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    test_range<cpp20_input_iterator, S> r;
+
+    std::ranges::fold_left(r, S(), non_decayable_result());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, iterator_t<test_range<cpp20_input_iterator, S> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, cpp20_input_iterator<S *>, invoke_result_t<non_decayable_result &, S, iter_reference_t<cpp20_input_iterator<S *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, copyable_non_movable> r;
+    std::ranges::fold_left(r, non_movable(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<plus<void> &, non_movable, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, copyable_non_movable> r;
+
+    std::ranges::fold_left(r, 0, std::minus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<minus<void> &, int, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
+    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
+    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, int> r;
+    std::ranges::fold_left(r, not_convertible_to_int(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, cpp20_input_iterator<int *>, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<cpp20_input_iterator<int *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
+    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
+  }
+  {
+    test_range<cpp20_input_iterator, not_invocable_with_decayed> r;
+    std::ranges::fold_left(r, 0, std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, iterator_t<test_range<cpp20_input_iterator, not_invocable_with_decayed> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, cpp20_input_iterator<not_invocable_with_decayed *>, invoke_result_t<plus<void> &, int, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > >' evaluated to false}}
+    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
+  }
+  {
+    test_range<cpp20_input_iterator, not_assignable_to_decayed> r;
+    std::ranges::fold_left(r, not_assignable_to_decayed(), std::plus());
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, iterator_t<test_range<cpp20_input_iterator, not_assignable_to_decayed> &> >' evaluated to false}}
+    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, cpp20_input_iterator<not_assignable_to_decayed *>, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
+    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
+  }
+}

>From 07ddbb40a5a7f6042c6aadef5dfcecee0863cda8 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 15 Dec 2023 19:42:17 +0000
Subject: [PATCH 08/17] Revert "changes requirements test from asserts to
 verify"

This reverts commit bec1572ab327212aaf57af020678403cf7b1a321.
---
 .../requirements.compile.pass.cpp             | 107 +++++++++
 .../requirements.verify.cpp                   | 203 ------------------
 2 files changed, 107 insertions(+), 203 deletions(-)
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
new file mode 100644
index 00000000000000..c0f647b5fc88f1
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
@@ -0,0 +1,107 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// Covers indirectly_readable<I> too
+template <std::input_or_output_iterator T>
+  requires(!std::input_iterator<T>)
+void requires_input_iterator() {
+  static_assert(!requires(T t) { std::ranges::fold_left_with_iter(t, std::unreachable_sentinel, 0, std::plus()); });
+}
+
+template <std::equality_comparable T>
+  requires(!std::sentinel_for<int*, T>)
+void requires_sentinel() {
+  static_assert(!requires(T first, T last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
+}
+
+template <class F>
+  requires(!std::copy_constructible<F>)
+void requires_copy_constructible_F() {
+  static_assert(!requires(int* first, int* last, F f) {
+    std::ranges::fold_left_with_iter(first, last, 0, std::move(f));
+  });
+}
+
+template <class F>
+  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
+void requires_raw_invocable() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left_with_iter(first, last, 0, f); });
+}
+
+template <class F>
+  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
+                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
+void requires_decaying_invoke_result() {
+  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left_with_iter(first, last, init, f); });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_init() {
+  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_decayed() {
+  static_assert(!requires(T* first, T* last) { std::ranges::fold_left_with_iter(first, last, 0, std::minus()); });
+}
+
+template <class T>
+  requires(!std::convertible_to<T, int>)
+void requires_init_is_convertible_to_decayed() {
+  static_assert(!requires(int* first, int* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::invocable<std::plus<>&, T, T&>)
+void requires_invocable_with_decayed() {
+  static_assert(!requires(T* first, T* last, int init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::assignable_from<T&, T volatile&>)
+void requires_assignable_from_invoke_result() {
+  static_assert(!requires(T* first, T* last, T init) {
+    std::ranges::fold_left_with_iter(first, last, init, std::plus());
+  });
+}
+
+void test() {
+  requires_input_iterator<bad_iterator_category>();
+  requires_sentinel<cpp17_input_iterator<int*>>();
+  requires_copy_constructible_F<non_copy_constructible_callable>();
+  requires_raw_invocable<not_invocable>();
+  requires_decaying_invoke_result<non_decayable_result>();
+  requires_movable_init<non_movable>();
+  requires_movable_decayed<copyable_non_movable>();
+  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
+  requires_invocable_with_decayed<not_invocable_with_decayed>();
+  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp
deleted file mode 100644
index 1260094c8074e4..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.verify.cpp
+++ /dev/null
@@ -1,203 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_range.h"
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// expected-error@*:* 19 {{no matching function for call to object of type 'const __fold_left_with_iter'}}
-
-void test_iterator() {
-  // expected-note@*:*  10 {{candidate template ignored: constraints not satisfied}}
-  // expected-note@*:*  10 {{candidate function template not viable: requires 3 arguments, but 4 were provided}}
-
-  std::ranges::fold_left_with_iter(bad_iterator_category(), std::unreachable_sentinel, 0, std::plus());
-  // expected-note@*:* {{because 'bad_iterator_category' does not satisfy 'input_iterator'}}
-  // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
-  // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
-
-  {
-    int* first;
-    int* last;
-    std::ranges::fold_left_with_iter(
-        cpp17_input_iterator<int*>(first), cpp17_input_iterator<int*>(last), 0, std::plus());
-    // expected-note@*:* {{because 'sentinel_for<cpp17_input_iterator<int *>, cpp17_input_iterator<int *> >' evaluated to false}}
-    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'semiregular'}}
-    // expected-note@*:* {{'cpp17_input_iterator<int *>' does not satisfy 'default_initializable'}}
-    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'constructible_from'}}
-    // expected-note@*:* {{because 'is_constructible_v<cpp17_input_iterator<int *> >' evaluated to false}}
-
-    std::ranges::fold_left_with_iter(first, last, 0, non_copy_constructible_callable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, int *>' evaluated to false}}
-    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-
-    std::ranges::fold_left_with_iter(first, last, 0, not_invocable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, int *>' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<int *> >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    S* first = nullptr;
-    S* last  = nullptr;
-    std::ranges::fold_left_with_iter(first, last, S(), non_decayable_result());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, S *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, S *, invoke_result_t<non_decayable_result &, S, iter_reference_t<S *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
-  }
-  {
-    copyable_non_movable* first;
-    copyable_non_movable* last;
-    std::ranges::fold_left_with_iter(first, last, non_movable(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, copyable_non_movable *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, copyable_non_movable *, invoke_result_t<plus<void> &, non_movable, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
-  }
-  {
-    copyable_non_movable* first;
-    copyable_non_movable* last;
-    std::ranges::fold_left_with_iter(first, last, 0, std::minus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, copyable_non_movable *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, copyable_non_movable *, invoke_result_t<minus<void> &, int, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-  }
-  {
-    int* first = nullptr;
-    int* last  = nullptr;
-    std::ranges::fold_left_with_iter(first, last, not_convertible_to_int(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, int *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, int *, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<int *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
-  }
-  {
-    not_invocable_with_decayed* first;
-    not_invocable_with_decayed* last;
-    std::ranges::fold_left_with_iter(first, last, 0, std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, not_invocable_with_decayed *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, not_invocable_with_decayed *, invoke_result_t<plus<void> &, int, iter_reference_t<not_invocable_with_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<not_invocable_with_decayed *> >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    not_assignable_to_decayed* first;
-    not_assignable_to_decayed* last;
-    std::ranges::fold_left_with_iter(first, last, not_assignable_to_decayed(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
-  }
-}
-
-void test_fold_range() {
-  // expected-note@*:*  9 {{candidate template ignored: constraints not satisfied}}
-  // expected-note@*:*  9 {{candidate function template not viable: requires 4 arguments, but 3 were provided}}
-
-  {
-    struct bad_range {
-      bad_iterator_category begin();
-      std::unreachable_sentinel_t end();
-    };
-
-    bad_range r;
-    std::ranges::fold_left_with_iter(r, 0, std::plus());
-    // expected-note@*:* {{because 'bad_range &' does not satisfy 'input_range'}}
-    // expected-note@*:* {{because 'iterator_t<bad_range &>' (aka 'bad_iterator_category') does not satisfy 'input_iterator'}}
-    // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
-    // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, int> r;
-
-    std::ranges::fold_left_with_iter(r, 0, non_copy_constructible_callable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-
-    std::ranges::fold_left_with_iter(r, 0, not_invocable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<cpp20_input_iterator<int *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    test_range<cpp20_input_iterator, S> r;
-
-    std::ranges::fold_left_with_iter(r, S(), non_decayable_result());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, iterator_t<test_range<cpp20_input_iterator, S> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, cpp20_input_iterator<S *>, invoke_result_t<non_decayable_result &, S, iter_reference_t<cpp20_input_iterator<S *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, copyable_non_movable> r;
-    std::ranges::fold_left_with_iter(r, non_movable(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<plus<void> &, non_movable, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, copyable_non_movable> r;
-
-    std::ranges::fold_left_with_iter(r, 0, std::minus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<minus<void> &, int, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, int> r;
-    std::ranges::fold_left_with_iter(r, not_convertible_to_int(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, cpp20_input_iterator<int *>, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<cpp20_input_iterator<int *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, not_invocable_with_decayed> r;
-    std::ranges::fold_left_with_iter(r, 0, std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, iterator_t<test_range<cpp20_input_iterator, not_invocable_with_decayed> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, cpp20_input_iterator<not_invocable_with_decayed *>, invoke_result_t<plus<void> &, int, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    test_range<cpp20_input_iterator, not_assignable_to_decayed> r;
-    std::ranges::fold_left_with_iter(r, not_assignable_to_decayed(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, iterator_t<test_range<cpp20_input_iterator, not_assignable_to_decayed> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, cpp20_input_iterator<not_assignable_to_decayed *>, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
-  }
-}

>From 5376becd8ecb3d701ed42b1148f6bb9dd717fd7e Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 15 Dec 2023 19:42:23 +0000
Subject: [PATCH 09/17] Revert "adds missing file"

This reverts commit 6a57523fa1d33f2e8d221858c38af8765396c1aa.
---
 .../std/algorithms/alg.nonmodifying/alg.fold/requirements.h     | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
index 918f84348084c9..0104c71b11447e 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
@@ -24,7 +24,6 @@ struct bad_iterator_category {
 };
 
 struct non_movable {
-  non_movable()              = default;
   non_movable(non_movable&&) = delete;
 };
 
@@ -42,7 +41,6 @@ struct copyable_non_movable {
 };
 
 struct non_copy_constructible_callable {
-  non_copy_constructible_callable()                                       = default;
   non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
   non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;
 

>From c806db0215d36d5206cdd9bac04a9c183f3de1e2 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 15 Dec 2023 19:42:25 +0000
Subject: [PATCH 10/17] Revert "repeats for `fold_left`"

This reverts commit 12f36c42514b24a39c02c25bf33c94cf8bef9bc9.
---
 .../fold_left/requirements.compile.pass.cpp   |  99 +++++++++
 .../fold_left/requirements.verify.cpp         | 202 ------------------
 2 files changed, 99 insertions(+), 202 deletions(-)
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
new file mode 100644
index 00000000000000..0b1c1e4eae1ea5
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
@@ -0,0 +1,99 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left`'s requirements are correct.
+
+#include <algorithm>
+#include <concepts>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_iterators.h"
+#include "../requirements.h"
+
+// Covers indirectly_readable<I> too
+template <std::input_or_output_iterator T>
+  requires(!std::input_iterator<T>)
+void requires_input_iterator() {
+  static_assert(!requires(T t) { std::ranges::fold_left(t, std::unreachable_sentinel, 0, std::plus()); });
+}
+
+template <std::equality_comparable T>
+  requires(!std::sentinel_for<int*, T>)
+void requires_sentinel() {
+  static_assert(!requires(T first, T last) { std::ranges::fold_left(first, last, 0, std::plus()); });
+}
+
+template <class F>
+  requires(!std::copy_constructible<F>)
+void requires_copy_constructible_F() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, std::move(f)); });
+}
+
+template <class F>
+  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
+void requires_raw_invocable() {
+  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, f); });
+}
+
+template <class F>
+  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
+                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
+void requires_decaying_invoke_result() {
+  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left(first, last, init, f); });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_init() {
+  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
+    std::ranges::fold_left(first, last, init, std::plus());
+  });
+}
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_decayed() {
+  static_assert(!requires(T* first, T* last) { std::ranges::fold_left(first, last, 0, std::minus()); });
+}
+
+template <class T>
+  requires(!std::convertible_to<T, int>)
+void requires_init_is_convertible_to_decayed() {
+  static_assert(!requires(int* first, int* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+template <class T>
+  requires(!std::invocable<std::plus<>&, T, T&>)
+void requires_invocable_with_decayed() {
+  static_assert(!requires(T* first, T* last, int init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+template <class T>
+  requires(!std::assignable_from<T&, T volatile&>)
+void requires_assignable_from_invoke_result() {
+  static_assert(!requires(T* first, T* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
+}
+
+void test() {
+  requires_input_iterator<bad_iterator_category>();
+  requires_sentinel<cpp17_input_iterator<int*>>();
+  requires_copy_constructible_F<non_copy_constructible_callable>();
+  requires_raw_invocable<not_invocable>();
+  requires_decaying_invoke_result<non_decayable_result>();
+  requires_movable_init<non_movable>();
+  requires_movable_decayed<copyable_non_movable>();
+  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
+  requires_invocable_with_decayed<not_invocable_with_decayed>();
+  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp
deleted file mode 100644
index fd1c2529200945..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.verify.cpp
+++ /dev/null
@@ -1,202 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_range.h"
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// expected-error@*:* 19 {{no matching function for call to object of type 'const __fold_left'}}
-
-void test_iterator() {
-  // expected-note@*:*  10 {{candidate template ignored: constraints not satisfied}}
-  // expected-note@*:*  10 {{candidate function template not viable: requires 3 arguments, but 4 were provided}}
-
-  std::ranges::fold_left(bad_iterator_category(), std::unreachable_sentinel, 0, std::plus());
-  // expected-note@*:* {{because 'bad_iterator_category' does not satisfy 'input_iterator'}}
-  // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
-  // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
-
-  {
-    int* first;
-    int* last;
-    std::ranges::fold_left(cpp17_input_iterator<int*>(first), cpp17_input_iterator<int*>(last), 0, std::plus());
-    // expected-note@*:* {{because 'sentinel_for<cpp17_input_iterator<int *>, cpp17_input_iterator<int *> >' evaluated to false}}
-    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'semiregular'}}
-    // expected-note@*:* {{'cpp17_input_iterator<int *>' does not satisfy 'default_initializable'}}
-    // expected-note@*:* {{because 'cpp17_input_iterator<int *>' does not satisfy 'constructible_from'}}
-    // expected-note@*:* {{because 'is_constructible_v<cpp17_input_iterator<int *> >' evaluated to false}}
-
-    std::ranges::fold_left(first, last, 0, non_copy_constructible_callable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, int *>' evaluated to false}}
-    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-
-    std::ranges::fold_left(first, last, 0, not_invocable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, int *>' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<int *> >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    S* first = nullptr;
-    S* last  = nullptr;
-    std::ranges::fold_left(first, last, S(), non_decayable_result());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, S *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, S *, invoke_result_t<non_decayable_result &, S, iter_reference_t<S *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
-  }
-  {
-    copyable_non_movable* first;
-    copyable_non_movable* last;
-    std::ranges::fold_left(first, last, non_movable(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, copyable_non_movable *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, copyable_non_movable *, invoke_result_t<plus<void> &, non_movable, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
-  }
-  {
-    copyable_non_movable* first;
-    copyable_non_movable* last;
-    std::ranges::fold_left(first, last, 0, std::minus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, copyable_non_movable *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, copyable_non_movable *, invoke_result_t<minus<void> &, int, iter_reference_t<copyable_non_movable *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-  }
-  {
-    int* first = nullptr;
-    int* last  = nullptr;
-    std::ranges::fold_left(first, last, not_convertible_to_int(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, int *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, int *, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<int *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
-  }
-  {
-    not_invocable_with_decayed* first;
-    not_invocable_with_decayed* last;
-    std::ranges::fold_left(first, last, 0, std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, not_invocable_with_decayed *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, not_invocable_with_decayed *, invoke_result_t<plus<void> &, int, iter_reference_t<not_invocable_with_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<not_invocable_with_decayed *> >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    not_assignable_to_decayed* first;
-    not_assignable_to_decayed* last;
-    std::ranges::fold_left(first, last, not_assignable_to_decayed(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *>' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, not_assignable_to_decayed *, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<not_assignable_to_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
-  }
-}
-
-void test_fold_range() {
-  // expected-note@*:*  9 {{candidate template ignored: constraints not satisfied}}
-  // expected-note@*:*  9 {{candidate function template not viable: requires 4 arguments, but 3 were provided}}
-
-  {
-    struct bad_range {
-      bad_iterator_category begin();
-      std::unreachable_sentinel_t end();
-    };
-
-    bad_range r;
-    std::ranges::fold_left(r, 0, std::plus());
-    // expected-note@*:* {{because 'bad_range &' does not satisfy 'input_range'}}
-    // expected-note@*:* {{because 'iterator_t<bad_range &>' (aka 'bad_iterator_category') does not satisfy 'input_iterator'}}
-    // expected-note@*:* {{because 'derived_from<_ITER_CONCEPT<bad_iterator_category>, input_iterator_tag>' evaluated to false}}
-    // expected-note@*:* {{because 'is_base_of_v<std::input_iterator_tag, void>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, int> r;
-
-    std::ranges::fold_left(r, 0, non_copy_constructible_callable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_copy_constructible_callable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because 'non_copy_constructible_callable' does not satisfy 'copy_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_copy_constructible_callable, non_copy_constructible_callable &>' evaluated to false}}
-
-    std::ranges::fold_left(r, 0, not_invocable());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<not_invocable, int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<not_invocable &, int, iter_reference_t<cpp20_input_iterator<int *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    test_range<cpp20_input_iterator, S> r;
-
-    std::ranges::fold_left(r, S(), non_decayable_result());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<non_decayable_result, S, iterator_t<test_range<cpp20_input_iterator, S> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<non_decayable_result, S, cpp20_input_iterator<S *>, invoke_result_t<non_decayable_result &, S, iter_reference_t<cpp20_input_iterator<S *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<volatile S &, S>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<volatile S &, S>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, copyable_non_movable> r;
-    std::ranges::fold_left(r, non_movable(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, non_movable, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, non_movable, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<plus<void> &, non_movable, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<non_movable, non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<non_movable, non_movable>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, copyable_non_movable> r;
-
-    std::ranges::fold_left(r, 0, std::minus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::minus<void>, int, iterator_t<test_range<cpp20_input_iterator, copyable_non_movable> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::minus<void>, int, cpp20_input_iterator<copyable_non_movable *>, invoke_result_t<minus<void> &, int, iter_reference_t<cpp20_input_iterator<copyable_non_movable *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'movable'}}
-    // expected-note@*:* {{because 'copyable_non_movable' does not satisfy 'move_constructible'}}
-    // expected-note@*:* {{because 'constructible_from<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-    // expected-note@*:* {{because 'is_constructible_v<copyable_non_movable, copyable_non_movable>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, int> r;
-    std::ranges::fold_left(r, not_convertible_to_int(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_convertible_to_int, iterator_t<test_range<cpp20_input_iterator, int> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_convertible_to_int, cpp20_input_iterator<int *>, invoke_result_t<plus<void> &, not_convertible_to_int, iter_reference_t<cpp20_input_iterator<int *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'convertible_to<not_convertible_to_int, int>' evaluated to false}}
-    // expected-note@*:* {{because 'is_convertible_v<not_convertible_to_int, int>' evaluated to false}}
-  }
-  {
-    test_range<cpp20_input_iterator, not_invocable_with_decayed> r;
-    std::ranges::fold_left(r, 0, std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, int, iterator_t<test_range<cpp20_input_iterator, not_invocable_with_decayed> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, int, cpp20_input_iterator<not_invocable_with_decayed *>, invoke_result_t<plus<void> &, int, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'invocable<std::plus<void> &, not_invocable_with_decayed, iter_reference_t<cpp20_input_iterator<not_invocable_with_decayed *> > >' evaluated to false}}
-    // expected-note@*:* {{because 'std::invoke(std::forward<_Fn>(__fn), std::forward<_Args>(__args)...)' would be invalid: no matching function for call to 'invoke'}}
-  }
-  {
-    test_range<cpp20_input_iterator, not_assignable_to_decayed> r;
-    std::ranges::fold_left(r, not_assignable_to_decayed(), std::plus());
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable<std::plus<void>, not_assignable_to_decayed, iterator_t<test_range<cpp20_input_iterator, not_assignable_to_decayed> &> >' evaluated to false}}
-    // expected-note@*:* {{because '__indirectly_binary_left_foldable_impl<std::plus<void>, not_assignable_to_decayed, cpp20_input_iterator<not_assignable_to_decayed *>, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because 'assignable_from<not_assignable_to_decayed &, invoke_result_t<plus<void> &, not_assignable_to_decayed, iter_reference_t<cpp20_input_iterator<not_assignable_to_decayed *> > > >' evaluated to false}}
-    // expected-note@*:* {{because '__lhs = std::forward<_Rhs>(__rhs)' would be invalid: no viable overloaded '='}}
-  }
-}

>From 5523376798606e4a366205aab5cbcf5d069cbd54 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 15 Dec 2023 21:38:34 +0000
Subject: [PATCH 11/17] consolidates requirements.compile.pass.cpp for the fold
 algos

Per @ericwf's feedback, I've consolidated all the requirements tests
into a single file in order to eliminate duplication among the various
fold algorithms (which have similar requirements).
---
 .../fold_left/requirements.compile.pass.cpp   |  99 -------
 .../requirements.compile.pass.cpp             | 107 --------
 .../alg.fold/requirements.compile.pass.cpp    | 259 ++++++++++++++++++
 .../alg.nonmodifying/alg.fold/requirements.h  |  82 ------
 4 files changed, 259 insertions(+), 288 deletions(-)
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
deleted file mode 100644
index 0b1c1e4eae1ea5..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/requirements.compile.pass.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// Covers indirectly_readable<I> too
-template <std::input_or_output_iterator T>
-  requires(!std::input_iterator<T>)
-void requires_input_iterator() {
-  static_assert(!requires(T t) { std::ranges::fold_left(t, std::unreachable_sentinel, 0, std::plus()); });
-}
-
-template <std::equality_comparable T>
-  requires(!std::sentinel_for<int*, T>)
-void requires_sentinel() {
-  static_assert(!requires(T first, T last) { std::ranges::fold_left(first, last, 0, std::plus()); });
-}
-
-template <class F>
-  requires(!std::copy_constructible<F>)
-void requires_copy_constructible_F() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, std::move(f)); });
-}
-
-template <class F>
-  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
-void requires_raw_invocable() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left(first, last, 0, f); });
-}
-
-template <class F>
-  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
-                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
-void requires_decaying_invoke_result() {
-  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left(first, last, init, f); });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_init() {
-  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
-    std::ranges::fold_left(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_decayed() {
-  static_assert(!requires(T* first, T* last) { std::ranges::fold_left(first, last, 0, std::minus()); });
-}
-
-template <class T>
-  requires(!std::convertible_to<T, int>)
-void requires_init_is_convertible_to_decayed() {
-  static_assert(!requires(int* first, int* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-template <class T>
-  requires(!std::invocable<std::plus<>&, T, T&>)
-void requires_invocable_with_decayed() {
-  static_assert(!requires(T* first, T* last, int init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-template <class T>
-  requires(!std::assignable_from<T&, T volatile&>)
-void requires_assignable_from_invoke_result() {
-  static_assert(!requires(T* first, T* last, T init) { std::ranges::fold_left(first, last, init, std::plus()); });
-}
-
-void test() {
-  requires_input_iterator<bad_iterator_category>();
-  requires_sentinel<cpp17_input_iterator<int*>>();
-  requires_copy_constructible_F<non_copy_constructible_callable>();
-  requires_raw_invocable<not_invocable>();
-  requires_decaying_invoke_result<non_decayable_result>();
-  requires_movable_init<non_movable>();
-  requires_movable_decayed<copyable_non_movable>();
-  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
-  requires_invocable_with_decayed<not_invocable_with_decayed>();
-  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
deleted file mode 100644
index c0f647b5fc88f1..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/requirements.compile.pass.cpp
+++ /dev/null
@@ -1,107 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left_with_iter`'s requirements are correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <iterator>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "../requirements.h"
-
-// Covers indirectly_readable<I> too
-template <std::input_or_output_iterator T>
-  requires(!std::input_iterator<T>)
-void requires_input_iterator() {
-  static_assert(!requires(T t) { std::ranges::fold_left_with_iter(t, std::unreachable_sentinel, 0, std::plus()); });
-}
-
-template <std::equality_comparable T>
-  requires(!std::sentinel_for<int*, T>)
-void requires_sentinel() {
-  static_assert(!requires(T first, T last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
-}
-
-template <class F>
-  requires(!std::copy_constructible<F>)
-void requires_copy_constructible_F() {
-  static_assert(!requires(int* first, int* last, F f) {
-    std::ranges::fold_left_with_iter(first, last, 0, std::move(f));
-  });
-}
-
-template <class F>
-  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
-void requires_raw_invocable() {
-  static_assert(!requires(int* first, int* last, F f) { std::ranges::fold_left_with_iter(first, last, 0, f); });
-}
-
-template <class F>
-  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
-                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
-void requires_decaying_invoke_result() {
-  static_assert(!requires(S* first, S* last, S init, F f) { std::ranges::fold_left_with_iter(first, last, init, f); });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_init() {
-  static_assert(!requires(copyable_non_movable* first, copyable_non_movable* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::movable<T>)
-void requires_movable_decayed() {
-  static_assert(!requires(T* first, T* last) { std::ranges::fold_left_with_iter(first, last, 0, std::minus()); });
-}
-
-template <class T>
-  requires(!std::convertible_to<T, int>)
-void requires_init_is_convertible_to_decayed() {
-  static_assert(!requires(int* first, int* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::invocable<std::plus<>&, T, T&>)
-void requires_invocable_with_decayed() {
-  static_assert(!requires(T* first, T* last, int init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-template <class T>
-  requires(!std::assignable_from<T&, T volatile&>)
-void requires_assignable_from_invoke_result() {
-  static_assert(!requires(T* first, T* last, T init) {
-    std::ranges::fold_left_with_iter(first, last, init, std::plus());
-  });
-}
-
-void test() {
-  requires_input_iterator<bad_iterator_category>();
-  requires_sentinel<cpp17_input_iterator<int*>>();
-  requires_copy_constructible_F<non_copy_constructible_callable>();
-  requires_raw_invocable<not_invocable>();
-  requires_decaying_invoke_result<non_decayable_result>();
-  requires_movable_init<non_movable>();
-  requires_movable_decayed<copyable_non_movable>();
-  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
-  requires_invocable_with_decayed<not_invocable_with_decayed>();
-  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.compile.pass.cpp
new file mode 100644
index 00000000000000..cad96d4c10127f
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.compile.pass.cpp
@@ -0,0 +1,259 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// Checks that `std::ranges::fold_left_with_iter`'s requirements reject parameters that don't meet
+// the overloads' constraints.
+
+#include <algorithm>
+#include <concepts>
+#include <cstddef>
+#include <functional>
+#include <iterator>
+#include <ranges>
+
+#include "test_iterators.h"
+
+// FIXME(cjdb): deduplicate
+struct bad_iterator_category {
+  using value_type        = int;
+  using difference_type   = std::ptrdiff_t;
+  using iterator_category = void;
+
+  value_type operator*() const;
+
+  bad_iterator_category& operator++();
+  void operator++(int);
+};
+
+// Covers indirectly_readable<I> too
+template <std::input_or_output_iterator T>
+  requires(!std::input_iterator<T>)
+void requires_input_iterator() {
+  struct bad_range {
+    T begin();
+    std::unreachable_sentinel_t end();
+  };
+
+  static_assert(!requires(bad_range r) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), std::unreachable_sentinel, 0, std::plus());
+  });
+  static_assert(!requires(bad_range r) { std::ranges::fold_left_with_iter(r, 0, std::plus()); });
+
+  static_assert(!requires(bad_range r) {
+    std::ranges::fold_left(r.begin(), r.end(), std::unreachable_sentinel, 0, std::plus());
+  });
+
+  static_assert(!requires(bad_range r) { std::ranges::fold_left(r, 0, std::plus()); });
+}
+
+template <std::equality_comparable S>
+  requires(!std::sentinel_for<int*, S>)
+void requires_sentinel() {
+  static_assert(!requires(S first, S last) { std::ranges::fold_left_with_iter(first, last, 0, std::plus()); });
+  static_assert(!requires(S first, S last) { std::ranges::fold_left(first, last, 0, std::plus()); });
+}
+
+struct non_copy_constructible_callable {
+  non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
+  non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;
+
+  int operator()(int, int) const;
+};
+
+template <class F>
+  requires(!std::copy_constructible<F>)
+void requires_copy_constructible_F() {
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, std::move(f));
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
+    std::ranges::fold_left_with_iter(r, 0, std::move(f));
+  });
+
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
+    std::ranges::fold_left(r.begin(), r.end(), 0, std::move(f));
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left(r, 0, std::move(f)); });
+}
+
+struct not_invocable_with_lvalue_rhs {
+  int operator()(int, int&&);
+};
+
+template <class F>
+  requires(!std::invocable<F&, int, std::iter_reference_t<int*>>)
+void requires_raw_invocable() {
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, f);
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left_with_iter(r, 0, f); });
+
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) {
+    std::ranges::fold_left(r.begin(), r.end(), 0, f);
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, F f) { std::ranges::fold_left(r, 0, f); });
+}
+
+struct S {};
+
+struct non_decayable_result {
+  S volatile& operator()(S, S) const;
+};
+
+template <std::invocable<S, std::iter_reference_t<S*>> F>
+  requires(!std::convertible_to<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>,
+                                std::decay_t<std::invoke_result_t<F&, S, std::iter_reference_t<S*>>>>)
+void requires_decaying_invoke_result() {
+  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, f);
+  });
+  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
+    std::ranges::fold_left_with_iter(r, init, f);
+  });
+
+  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) {
+    std::ranges::fold_left(r.begin(), r.end(), init, f);
+  });
+  static_assert(!requires(std::ranges::subrange<S*, S*> r, S init, F f) { std::ranges::fold_left(r, init, f); });
+}
+
+struct non_movable {
+  non_movable(int);
+  non_movable(non_movable&&) = delete;
+
+  int apply(non_movable const&) const;
+};
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_init() {
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, &T::apply);
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left_with_iter(r, init, &T::apply);
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left(r.begin(), r.end(), init, &T::apply);
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) { std::ranges::fold_left(r, init, &T::apply); });
+}
+
+struct result_not_movable_after_decay {
+  result_not_movable_after_decay(int);
+  result_not_movable_after_decay(result_not_movable_after_decay&&) = delete;
+  result_not_movable_after_decay(result_not_movable_after_decay const&);
+
+  friend result_not_movable_after_decay const& operator+(int, result_not_movable_after_decay const&);
+  friend result_not_movable_after_decay const& operator+(result_not_movable_after_decay const&, int);
+  friend result_not_movable_after_decay const&
+  operator+(result_not_movable_after_decay const&, result_not_movable_after_decay const&);
+};
+
+template <class T>
+  requires(!std::movable<T>)
+void requires_movable_decayed() {
+  static_assert(!requires(std::ranges::subrange<T*, T*> r) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), 0, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r) { std::ranges::fold_left_with_iter(r, 0, std::plus()); });
+
+  static_assert(!requires(std::ranges::subrange<T*, T*> r) {
+    std::ranges::fold_left(r.begin(), r.end(), 0, T::apply);
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r) { std::ranges::fold_left(r, 0, std::plus()); });
+}
+
+struct not_convertible_to_int {
+  friend int operator+(not_convertible_to_int, not_convertible_to_int);
+  friend int operator+(not_convertible_to_int, int);
+  friend int operator+(int, not_convertible_to_int);
+};
+
+template <class T>
+  requires(!std::convertible_to<T, int>)
+void requires_init_is_convertible_to_decayed() {
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
+    std::ranges::fold_left_with_iter(r, init, std::plus());
+  });
+
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
+    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<int*, int*> r, T init) {
+    std::ranges::fold_left(r, init, std::plus());
+  });
+}
+
+struct not_invocable_with_decayed {
+  not_invocable_with_decayed(int);
+  friend not_invocable_with_decayed& operator+(int, not_invocable_with_decayed&);
+  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed&, int);
+  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed volatile&, not_invocable_with_decayed&);
+};
+
+template <class T>
+  requires(!std::invocable<std::plus<>&, T, T&>)
+void requires_invocable_with_decayed() {
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
+    std::ranges::fold_left_with_iter(r, init, std::plus());
+  });
+
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) {
+    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, int init) { std::ranges::fold_left(r, init, std::plus()); });
+}
+
+struct not_assignable_to_decayed {
+  not_assignable_to_decayed();
+  not_assignable_to_decayed(not_assignable_to_decayed&);
+  not_assignable_to_decayed(not_assignable_to_decayed const&);
+  not_assignable_to_decayed(not_assignable_to_decayed volatile&);
+  not_assignable_to_decayed(not_assignable_to_decayed const volatile&);
+  friend not_assignable_to_decayed volatile& operator+(not_assignable_to_decayed, not_assignable_to_decayed);
+};
+
+template <class T>
+  requires(!std::assignable_from<T&, T volatile&>)
+void requires_assignable_from_invoke_result() {
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left_with_iter(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left_with_iter(r, init, std::plus());
+  });
+
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) {
+    std::ranges::fold_left(r.begin(), r.end(), init, std::plus());
+  });
+  static_assert(!requires(std::ranges::subrange<T*, T*> r, T init) { std::ranges::fold_left(r, init, std::plus()); });
+}
+
+void test() {
+  requires_input_iterator<bad_iterator_category>();
+  requires_sentinel<cpp17_input_iterator<int*>>();
+  requires_copy_constructible_F<non_copy_constructible_callable>();
+  requires_raw_invocable<not_invocable_with_lvalue_rhs>();
+  requires_decaying_invoke_result<non_decayable_result>();
+  requires_movable_init<non_movable>();
+  requires_movable_decayed<result_not_movable_after_decay>();
+  requires_init_is_convertible_to_decayed<not_convertible_to_int>();
+  requires_invocable_with_decayed<not_invocable_with_decayed>();
+  requires_assignable_from_invoke_result<not_assignable_to_decayed>();
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
deleted file mode 100644
index 0104c71b11447e..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/requirements.h
+++ /dev/null
@@ -1,82 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LIBCXX_TEST_FOLD_REQUIREMENTS_H
-#define LIBCXX_TEST_FOLD_REQUIREMENTS_H
-
-#include <cstddef>
-
-// FIXME(cjdb): deduplicate
-struct bad_iterator_category {
-  using value_type        = int;
-  using difference_type   = std::ptrdiff_t;
-  using iterator_category = void;
-
-  value_type operator*() const;
-
-  bad_iterator_category& operator++();
-  void operator++(int);
-};
-
-struct non_movable {
-  non_movable(non_movable&&) = delete;
-};
-
-struct copyable_non_movable {
-  copyable_non_movable(int);
-  copyable_non_movable(copyable_non_movable&&) = delete;
-  copyable_non_movable(copyable_non_movable const&);
-
-  friend int operator+(copyable_non_movable const&, non_movable const&);
-  friend int operator+(non_movable const&, copyable_non_movable const&);
-
-  friend copyable_non_movable const& operator-(int, copyable_non_movable const&);
-  friend copyable_non_movable const& operator-(copyable_non_movable const&, int);
-  friend copyable_non_movable const& operator-(copyable_non_movable const&, copyable_non_movable const&);
-};
-
-struct non_copy_constructible_callable {
-  non_copy_constructible_callable(non_copy_constructible_callable&&)      = default;
-  non_copy_constructible_callable(non_copy_constructible_callable const&) = delete;
-
-  int operator()(int, int) const;
-};
-
-struct not_invocable {
-  int operator()(int, int&&);
-};
-
-struct S {};
-
-struct non_decayable_result {
-  S volatile& operator()(S, S) const;
-};
-
-struct not_convertible_to_int {
-  friend int operator+(not_convertible_to_int, not_convertible_to_int);
-  friend int operator+(not_convertible_to_int, int);
-  friend int operator+(int, not_convertible_to_int);
-};
-
-struct not_invocable_with_decayed {
-  not_invocable_with_decayed(int);
-  friend not_invocable_with_decayed& operator+(int, not_invocable_with_decayed&);
-  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed&, int);
-  friend not_invocable_with_decayed& operator+(not_invocable_with_decayed volatile&, not_invocable_with_decayed&);
-};
-
-struct not_assignable_to_decayed {
-  not_assignable_to_decayed();
-  not_assignable_to_decayed(not_assignable_to_decayed&);
-  not_assignable_to_decayed(not_assignable_to_decayed const&);
-  not_assignable_to_decayed(not_assignable_to_decayed volatile&);
-  not_assignable_to_decayed(not_assignable_to_decayed const volatile&);
-  friend not_assignable_to_decayed volatile& operator+(not_assignable_to_decayed, not_assignable_to_decayed);
-};
-
-#endif // LIBCXX_TEST_FOLD_REQUIREMENTS

>From 5822e9d499d0f428f9e15e7d98ae20805e6f3922 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Fri, 15 Dec 2023 21:41:28 +0000
Subject: [PATCH 12/17] eliminates unnecessary `__cpo` namespace

---
 libcxx/include/__algorithm/fold.h | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/libcxx/include/__algorithm/fold.h b/libcxx/include/__algorithm/fold.h
index 7364b676b787c4..ebc4bc414d740f 100644
--- a/libcxx/include/__algorithm/fold.h
+++ b/libcxx/include/__algorithm/fold.h
@@ -89,9 +89,7 @@ struct __fold_left_with_iter {
   }
 };
 
-inline namespace __cpo {
 inline constexpr auto fold_left_with_iter = __fold_left_with_iter();
-} // namespace __cpo
 
 struct __fold_left {
   template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
@@ -106,9 +104,7 @@ struct __fold_left {
   }
 };
 
-inline namespace __cpo {
 inline constexpr auto fold_left = __fold_left();
-} // namespace __cpo
 } // namespace ranges
 
 #endif // _LIBCPP_STD_VER >= 23

>From 2e20461182df0c05f0a3b0dedca0505d56c53bd8 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Mon, 18 Dec 2023 22:31:21 +0000
Subject: [PATCH 13/17] reworks valid.cpp to incorporate feedback

---
 .../fold_left/return_types.compile.pass.cpp   | 171 ----------
 .../alg.fold/fold_left/valid.pass.cpp         |  93 ------
 .../return_types.compile.pass.cpp             | 174 ----------
 .../fold_left_with_iter/valid.pass.cpp        | 116 -------
 .../alg.nonmodifying/alg.fold/gaussian_sum.h  |  19 --
 .../alg.fold/left_folds.pass.cpp              | 305 ++++++++++++++++++
 .../test/support/invocable_with_telemetry.h   | 100 ++++++
 libcxx/test/support/maths.h                   |  65 ++++
 8 files changed, 470 insertions(+), 573 deletions(-)
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
 delete mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
 create mode 100644 libcxx/test/support/invocable_with_telemetry.h
 create mode 100644 libcxx/test/support/maths.h

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
deleted file mode 100644
index 911ce1615a1c75..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/return_types.compile.pass.cpp
+++ /dev/null
@@ -1,171 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left`'s return type is correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "test_range.h"
-
-template <class Result, class T>
-concept is_T = std::same_as<Result, T>;
-
-using std::ranges::fold_left;
-[[maybe_unused]] auto f = [](int x, double y) { return x * y; };
-
-struct Int {
-  int value;
-};
-
-struct Long {
-  int value;
-
-  Long plus(Int) const;
-};
-
-namespace sentinel_based_ranges {
-template <class T>
-using cpp17_input_range = test_range<cpp17_input_iterator, T>;
-
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
-});
-
-template <class T>
-using cpp20_input_range = test_range<cpp20_input_iterator, T>;
-
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
-});
-
-template <class T>
-using forward_range = test_range<forward_iterator, T>;
-
-static_assert(requires(forward_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(forward_range<int> r) {
-  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(forward_range<int> r) {
-  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
-});
-
-template <class T>
-using bidirectional_range = test_range<bidirectional_iterator, T>;
-
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left(r, 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
-});
-
-template <class T>
-using random_access_range = test_range<random_access_iterator, T>;
-
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(r, 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
-});
-
-template <class T>
-using contiguous_range = test_range<contiguous_iterator, T>;
-
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0.0f, std::plus()) } -> std::same_as<float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(r, 0.0f, std::plus()) } -> std::same_as<float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(std::move(r), 0.0f, std::plus()) } -> std::same_as<float>;
-});
-} // namespace sentinel_based_ranges
-
-namespace common_ranges {
-template <class T>
-using forward_range = test_common_range<forward_iterator, T>;
-
-static_assert(requires(forward_range<Int> r) {
-  { fold_left(r.begin(), r.end(), Long(0), &Long::plus) } -> std::same_as<Long>;
-});
-static_assert(requires(forward_range<Int> r) {
-  { fold_left(r, Long(0), &Long::plus) } -> std::same_as<Long>;
-});
-static_assert(requires(forward_range<Int> r) {
-  { fold_left(std::move(r), Long(0), &Long::plus) } -> std::same_as<Long>;
-});
-
-template <class T>
-using bidirectional_range = test_common_range<bidirectional_iterator, T>;
-
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left(r, 0, std::plus()) } -> std::same_as<int>;
-});
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left(std::move(r), 0, std::plus()) } -> std::same_as<int>;
-});
-
-template <class T>
-using random_access_range = test_common_range<random_access_iterator, T>;
-
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(r, 0, f) } -> std::same_as<double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left(std::move(r), 0, f) } -> std::same_as<double>;
-});
-
-template <class T>
-using contiguous_range = test_common_range<contiguous_iterator, T>;
-
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(r.begin(), r.end(), 0.0f, std::plus()) } -> std::same_as<float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(r, 0.0f, std::plus()) } -> std::same_as<float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left(std::move(r), 0.0f, std::plus()) } -> std::same_as<float>;
-});
-} // namespace common_ranges
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
deleted file mode 100644
index ef0fbb4b2a6d39..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left/valid.pass.cpp
+++ /dev/null
@@ -1,93 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// template<input_iterator I, sentinel_for<I> S, class T,
-//          indirectly-binary-left-foldable<T, I> F>
-//   constexpr see below ranges::fold_left(I first, S last, T init, F f);
-//
-// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
-//   constexpr see below ranges::fold_left(R&& r, T init, F f);
-
-#include <algorithm>
-#include <cassert>
-#include <vector>
-#include <functional>
-
-#include "test_range.h"
-#include "../gaussian_sum.h"
-
-constexpr bool test() {
-  {
-    auto data         = std::vector<int>{1, 2, 3, 4};
-    auto const result = std::ranges::fold_left(data.begin(), data.begin(), 0, std::plus());
-
-    assert(result == 0);
-
-    auto range = std::span(data.data(), 0);
-    assert(std::ranges::fold_left(range, 0, std::plus()) == result);
-  }
-  {
-    auto data           = std::vector<int>{1, 2, 3, 4};
-    constexpr auto init = 100.1;
-    auto const result   = std::ranges::fold_left(data.begin(), data.begin(), init, std::plus());
-
-    assert(result == init);
-
-    auto range = std::span(data.data(), 0);
-    assert(std::ranges::fold_left(range, init, std::plus()) == result);
-  }
-  {
-    auto data         = std::vector{1, 3, 5, 7, 9};
-    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0, std::plus());
-
-    assert(result == gaussian_sum(data)); // sum of n ascending odd numbers = n^2
-    assert(std::ranges::fold_left(data, 0, std::plus()) == result);
-  }
-  {
-    auto data         = std::vector{2, 4, 6, 8, 10, 12};
-    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0L, std::plus<long>());
-
-    assert(result == gaussian_sum(data));
-    assert(std::ranges::fold_left(data, 0, std::plus()) == result);
-  }
-  {
-    auto data         = std::vector{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
-    auto plus         = [](int const x, double const y) { return x + y; };
-    auto const result = std::ranges::fold_left(data.begin(), data.end(), 0.0, plus);
-
-    assert(result == -21.6); // int(  0.0) + -1.1 =   0 + -1.1 =  -1.1
-                             // int(- 1.1) + -2.2 = - 1 + -2.2 =  -3.2
-                             // int(- 3.2) + -3.3 = - 3 + -3.3 =  -6.3
-                             // int(- 6.3) + -4.4 = - 6 + -4.4 = -10.4
-                             // int(-10.4) + -5.5 = -10 + -5.5 = -15.5
-                             // int(-15.5) + -6.6 = -15 + -6.6 = -21.6.
-
-    assert(std::ranges::fold_left(data, 0, plus) == result);
-  }
-  {
-    auto data           = std::vector<double>{1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1};
-    auto plus           = [](double const x, double const y) { return static_cast<short>(x + y); };
-    constexpr auto init = 10.0;
-    auto const result   = std::ranges::fold_left(data.begin(), data.end(), init, plus);
-
-    assert(result == static_cast<short>(gaussian_sum(data) + init));
-    assert(std::ranges::fold_left(data, init, plus) == result);
-  }
-
-  return true;
-}
-
-int main() {
-  test();
-  static_assert(test());
-  return 0;
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
deleted file mode 100644
index b930002336294d..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/return_types.compile.pass.cpp
+++ /dev/null
@@ -1,174 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// Checks that `std::ranges::fold_left_with_iter`'s return type is correct.
-
-#include <algorithm>
-#include <concepts>
-#include <functional>
-#include <ranges>
-
-#include "test_iterators.h"
-#include "test_range.h"
-
-template <class Result, class Range, class T>
-concept is_in_value_result = std::same_as<Result, std::ranges::in_value_result<std::ranges::iterator_t<Range>, T>>;
-
-template <class Result, class T>
-concept is_dangling_with = std::same_as<Result, std::ranges::in_value_result<std::ranges::dangling, T>>;
-
-using std::ranges::fold_left_with_iter;
-[[maybe_unused]] auto f = [](int x, double y) { return x * y; };
-
-struct Int {
-  int value;
-};
-
-struct Long {
-  int value;
-
-  Long plus(Int) const;
-};
-
-namespace sentinel_based_ranges {
-template <class T>
-using cpp17_input_range = test_range<cpp17_input_iterator, T>;
-
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<cpp17_input_range<int>, int>;
-});
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<cpp17_input_range<int>, int>;
-});
-static_assert(requires(cpp17_input_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
-});
-
-template <class T>
-using cpp20_input_range = test_range<cpp20_input_iterator, T>;
-
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<cpp20_input_range<int>, int>;
-});
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<cpp20_input_range<int>, int>;
-});
-static_assert(requires(cpp20_input_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
-});
-
-template <class T>
-using forward_range = test_range<forward_iterator, T>;
-
-static_assert(requires(forward_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<forward_range<int>, int>;
-});
-static_assert(requires(forward_range<int> r) {
-  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<forward_range<int>, int>;
-});
-static_assert(requires(forward_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
-});
-
-template <class T>
-using bidirectional_range = test_range<bidirectional_iterator, T>;
-
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<bidirectional_range<short>, double>;
-});
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<bidirectional_range<short>, double>;
-});
-static_assert(requires(bidirectional_range<short> r) {
-  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
-});
-
-template <class T>
-using random_access_range = test_range<random_access_iterator, T>;
-
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<random_access_range<int>, double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<random_access_range<int>, double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
-});
-
-template <class T>
-using contiguous_range = test_range<contiguous_iterator, T>;
-
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(r, 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0.0f, std::plus()) } -> is_dangling_with<float>;
-});
-} // namespace sentinel_based_ranges
-
-namespace common_ranges {
-template <class T>
-using forward_range = test_common_range<forward_iterator, T>;
-
-static_assert(requires(forward_range<Int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), Long(0), &Long::plus) } -> is_in_value_result<forward_range<Int>, Long>;
-});
-static_assert(requires(forward_range<Int> r) {
-  { fold_left_with_iter(r, Long(0), &Long::plus) } -> is_in_value_result<forward_range<Int>, Long>;
-});
-static_assert(requires(forward_range<Int> r) {
-  { fold_left_with_iter(std::move(r), Long(0), &Long::plus) } -> is_dangling_with<Long>;
-});
-
-template <class T>
-using bidirectional_range = test_common_range<bidirectional_iterator, T>;
-
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, std::plus()) } -> is_in_value_result<bidirectional_range<int>, int>;
-});
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left_with_iter(r, 0, std::plus()) } -> is_in_value_result<bidirectional_range<int>, int>;
-});
-static_assert(requires(bidirectional_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, std::plus()) } -> is_dangling_with<int>;
-});
-
-template <class T>
-using random_access_range = test_common_range<random_access_iterator, T>;
-
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0, f) } -> is_in_value_result<random_access_range<int>, double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(r, 0, f) } -> is_in_value_result<random_access_range<int>, double>;
-});
-static_assert(requires(random_access_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0, f) } -> is_dangling_with<double>;
-});
-
-template <class T>
-using contiguous_range = test_common_range<contiguous_iterator, T>;
-
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(r.begin(), r.end(), 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(r, 0.0f, std::plus()) } -> is_in_value_result<contiguous_range<int>, float>;
-});
-static_assert(requires(contiguous_range<int> r) {
-  { fold_left_with_iter(std::move(r), 0.0f, std::plus()) } -> is_dangling_with<float>;
-});
-} // namespace common_ranges
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
deleted file mode 100644
index d8fb6525119e0c..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/fold_left_with_iter/valid.pass.cpp
+++ /dev/null
@@ -1,116 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// <algorithm>
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
-
-// template<input_iterator I, sentinel_for<I> S, class T,
-//          indirectly-binary-left-foldable<T, I> F>
-//   constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
-//
-// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
-//   constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);
-
-#include <algorithm>
-#include <cassert>
-#include <vector>
-#include <functional>
-
-#include "test_range.h"
-#include "../gaussian_sum.h"
-
-constexpr bool test() {
-  {
-    auto data   = std::vector<int>{1, 2, 3, 4};
-    auto result = std::ranges::fold_left_with_iter(data.begin(), data.begin(), 0, std::plus());
-
-    assert(result.in == data.begin());
-    assert(result.result == 0);
-
-    auto range   = std::span(data.data(), 0);
-    auto rresult = std::ranges::fold_left_with_iter(range, 0, std::plus());
-
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-  {
-    auto data           = std::vector<int>{1, 2, 3, 4};
-    constexpr auto init = 100.1;
-    auto result         = std::ranges::fold_left_with_iter(data.begin(), data.begin(), init, std::plus());
-
-    assert(result.in == data.begin());
-    assert(result.result == init);
-
-    auto range   = std::span(data.data(), 0);
-    auto rresult = std::ranges::fold_left_with_iter(range, init, std::plus());
-
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-  {
-    auto data   = std::vector{1, 3, 5, 7, 9};
-    auto result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0, std::plus());
-
-    assert(result.in == data.end());
-    assert(result.result == gaussian_sum(data)); // sum of n ascending odd numbers = n^2
-
-    auto rresult = std::ranges::fold_left_with_iter(data, 0, std::plus());
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-  {
-    auto data         = std::vector{2, 4, 6, 8, 10, 12};
-    auto const result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0L, std::plus<long>());
-
-    assert(result.in == data.end());
-    assert(result.result == gaussian_sum(data));
-
-    auto rresult = std::ranges::fold_left_with_iter(data, 0, std::plus());
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-  {
-    auto data   = std::vector{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
-    auto plus   = [](int const x, double const y) { return x + y; };
-    auto result = std::ranges::fold_left_with_iter(data.begin(), data.end(), 0.0, plus);
-
-    assert(result.in == data.end());
-    assert(result.result == -21.6); // int(  0.0) + -1.1 =   0 + -1.1 =  -1.1
-                                    // int(- 1.1) + -2.2 = - 1 + -2.2 =  -3.2
-                                    // int(- 3.2) + -3.3 = - 3 + -3.3 =  -6.3
-                                    // int(- 6.3) + -4.4 = - 6 + -4.4 = -10.4
-                                    // int(-10.4) + -5.5 = -10 + -5.5 = -15.5
-                                    // int(-15.5) + -6.6 = -15 + -6.6 = -21.6.
-
-    auto rresult = std::ranges::fold_left_with_iter(data, 0, plus);
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-  {
-    auto data           = std::vector<double>{1.1, 2.1, 3.1, 4.1, 5.1, 6.1, 7.1, 8.1, 9.1};
-    auto plus           = [](double const x, double const y) { return static_cast<short>(x + y); };
-    constexpr auto init = 10.0;
-    auto result         = std::ranges::fold_left_with_iter(data.begin(), data.end(), init, plus);
-
-    assert(result.in == data.end());
-    assert(result.result == static_cast<short>(gaussian_sum(data) + init));
-
-    auto rresult = std::ranges::fold_left_with_iter(data, init, plus);
-    assert(rresult.in == result.in);
-    assert(rresult.result == result.result);
-  }
-
-  return true;
-}
-
-int main() {
-  test();
-  static_assert(test());
-  return 0;
-}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
deleted file mode 100644
index 060beaa6703b1a..00000000000000
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/gaussian_sum.h
+++ /dev/null
@@ -1,19 +0,0 @@
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
-#define LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
-
-#include <vector>
-
-template <class T>
-constexpr auto gaussian_sum(std::vector<T> const& input) {
-  return (static_cast<double>(input.size()) / 2) * (input.front() + input.back());
-}
-
-#endif // LIBCXX_TEST_FOLD_GAUSSIAN_SUM_H
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
new file mode 100644
index 00000000000000..8290b1d5b7e344
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
@@ -0,0 +1,305 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <algorithm>
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
+
+// template<input_iterator I, sentinel_for<I> S, class T,
+//          indirectly-binary-left-foldable<T, I> F>
+//   constexpr see below ranges::fold_left_with_iter(I first, S last, T init, F f);
+//
+// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+//   constexpr see below ranges::fold_left_with_iter(R&& r, T init, F f);
+
+// template<input_iterator I, sentinel_for<I> S, class T,
+//          indirectly-binary-left-foldable<T, I> F>
+//   constexpr see below ranges::fold_left(I first, S last, T init, F f);
+//
+// template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+//   constexpr see below ranges::fold_left(R&& r, T init, F f);
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <deque>
+#include <forward_list>
+#include <functional>
+#include <iterator>
+#include <list>
+#include <ranges>
+#include <sstream>
+#include <string_view>
+#include <string>
+#include <vector>
+
+#include "test_range.h"
+#include "invocable_with_telemetry.h"
+#include "maths.h"
+
+using std::ranges::fold_left;
+using std::ranges::fold_left_with_iter;
+
+template <class Result, class Range, class T>
+concept is_in_value_result = std::same_as<Result, std::ranges::in_value_result<std::ranges::iterator_t<Range>, T>>;
+
+template <class Result, class T>
+concept is_dangling_with = std::same_as<Result, std::ranges::in_value_result<std::ranges::dangling, T>>;
+
+template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected) {
+  {
+    is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r.begin(), r.end(), init, f);
+    assert(result.in == r.end());
+    assert(result.result == expected);
+  }
+  {
+    auto invocations                                      = 0;
+    auto moves                                            = 0;
+    auto copies                                           = 0;
+    auto f2                                               = invocable_with_telemetry(f, invocations, moves, copies);
+    is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r.begin(), r.end(), init, f2);
+    assert(result.in == r.end());
+    assert(result.result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+
+  {
+    std::same_as<Expected> decltype(auto) result = fold_left(r.begin(), r.end(), init, f);
+    assert(result == expected);
+  }
+  {
+    auto invocations                             = 0;
+    auto moves                                   = 0;
+    auto copies                                  = 0;
+    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    std::same_as<Expected> decltype(auto) result = fold_left(r.begin(), r.end(), init, f2);
+    assert(result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+}
+
+template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_lvalue_range(R& r, T const& init, F f, Expected const& expected) {
+  {
+    is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r, init, f);
+    assert(result.in == r.end());
+    assert(result.result == expected);
+  }
+  {
+    auto invocations                             = 0;
+    auto moves                                   = 0;
+    auto copies                                  = 0;
+    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    std::same_as<Expected> decltype(auto) result = fold_left(r, init, f2);
+    assert(result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+
+  {
+    std::same_as<Expected> decltype(auto) result = fold_left(r, init, f);
+    assert(result == expected);
+  }
+  {
+    auto invocations                             = 0;
+    auto moves                                   = 0;
+    auto copies                                  = 0;
+    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    std::same_as<Expected> decltype(auto) result = fold_left(r, init, f2);
+    assert(result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+}
+
+template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check_rvalue_range(R& r, T const& init, F f, Expected const& expected) {
+  {
+    auto r2                                          = r;
+    is_dangling_with<Expected> decltype(auto) result = fold_left_with_iter(std::move(r2), init, f);
+    assert(result.result == expected);
+  }
+  {
+    auto invocations                                 = 0;
+    auto moves                                       = 0;
+    auto copies                                      = 0;
+    auto f2                                          = invocable_with_telemetry(f, invocations, moves, copies);
+    auto r2                                          = r;
+    is_dangling_with<Expected> decltype(auto) result = fold_left_with_iter(std::move(r2), init, f2);
+    assert(result.result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+
+  {
+    auto r2                                      = r;
+    std::same_as<Expected> decltype(auto) result = fold_left(std::move(r2), init, f);
+    assert(result == expected);
+  }
+  {
+    auto invocations                             = 0;
+    auto moves                                   = 0;
+    auto copies                                  = 0;
+    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    auto r2                                      = r;
+    std::same_as<Expected> decltype(auto) result = fold_left(std::move(r2), init, f2);
+    assert(result == expected);
+    assert(invocations == std::ranges::distance(r));
+    assert(moves == 0);
+    assert(copies == 1);
+  }
+}
+
+template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
+  requires std::copyable<R>
+constexpr void check(R r, T const& init, F f, Expected const& expected) {
+  check_iterator(r, init, f, expected);
+  check_lvalue_range(r, init, f, expected);
+  check_rvalue_range(r, init, f, expected);
+}
+
+constexpr bool check() {
+  {
+    auto const data = std::vector<int>{};
+    check(data, 100, std::plus(), 100);
+    check(data, -100, std::multiplies(), -100);
+  }
+
+  {
+    auto const data = std::vector<int>{1, 2, 3, 4};
+    check(data, 0, std::plus(), triangular_sum(data));
+    check(data, 1, std::multiplies(), factorial(data.back()));
+
+    auto multiply_with_prev = [n = 1](auto const x, auto const y) mutable {
+      auto const result = x * y * n;
+      n                 = y;
+      return static_cast<std::size_t>(result);
+    };
+    check(data, 1, multiply_with_prev, factorial(data.size()) * factorial(data.size() - 1));
+
+    auto fib = [n = 1](auto x, auto) mutable {
+      auto old_x = x;
+      x += n;
+      n = old_x;
+      return x;
+    };
+    check(data, 0, fib, fibonacci(data.back()));
+  }
+
+  auto parse = [](std::string_view const s) -> double {
+    return s == "zero"  ? 0
+         : s == "one"   ? 1
+         : s == "two"   ? 2
+         : s == "three" ? 3
+         : s == "four"  ? 4
+         : s == "five"  ? 5
+         : s == "six"   ? 6
+         : s == "seven" ? 7
+         : s == "eight" ? 8
+         : s == "nine"  ? 9
+                        : throw std::runtime_error("parse error");
+  };
+
+  {
+    auto data  = std::vector<std::string>{"five", "three", "two", "six", "one", "four"};
+    auto range = data | std::views::transform(parse);
+    check(range, 0, std::plus(), triangular_sum(range));
+  }
+  {
+    auto data           = std::string("five three two six one four");
+    auto to_string_view = [](auto&& r) {
+      auto const n = std::ranges::distance(r);
+      return std::string_view(&*r.begin(), n);
+    };
+    auto range =
+        std::views::lazy_split(data, ' ') | std::views::transform(to_string_view) | std::views::transform(parse);
+    check(range, 0, std::plus(), triangular_sum(range));
+  }
+
+  return true;
+}
+
+// Most containers aren't constexpr
+void runtime_only_test() {
+  { // istream_view is a genuine input range and needs specific handling.
+    constexpr auto raw_data = "Shells Orange Syrup Baratie Cocoyashi Loguetown";
+    constexpr auto expected = "WindmillShellsOrangeSyrupBaratieCocoyashiLoguetown";
+    auto const init         = std::string("Windmill");
+
+    {
+      auto input = std::istringstream(raw_data);
+      auto data  = std::views::istream<std::string>(input);
+      is_in_value_result<std::ranges::basic_istream_view<std::string, char>, std::string> decltype(auto) result =
+          fold_left_with_iter(data.begin(), data.end(), init, std::plus());
+
+      assert(result.in == data.end());
+      assert(result.result == expected);
+    }
+    {
+      auto input = std::istringstream(raw_data);
+      auto data  = std::views::istream<std::string>(input);
+      is_in_value_result<std::ranges::basic_istream_view<std::string, char>, std::string> decltype(auto) result =
+          fold_left_with_iter(data, init, std::plus());
+      assert(result.in == data.end());
+      assert(result.result == expected);
+    }
+    {
+      auto input = std::istringstream(raw_data);
+      auto data  = std::views::istream<std::string>(input);
+      assert(fold_left(data.begin(), data.end(), init, std::plus()) == expected);
+    }
+    {
+      auto input = std::istringstream(raw_data);
+      auto data  = std::views::istream<std::string>(input);
+      assert(fold_left(data, init, std::plus()) == expected);
+    }
+  }
+
+  {
+    auto const data     = std::forward_list<int>{1, 3, 5, 7, 9};
+    auto const n        = std::ranges::distance(data);
+    auto const expected = static_cast<float>(n * n); // sum of n consecutive odd numbers = n^2
+    check(data, 0.0f, std::plus(), expected);
+  }
+
+  {
+    auto const data     = std::list<int>{2, 4, 6, 8, 10, 12};
+    auto const expected = triangular_sum(data);
+    check(data, 0, std::plus<long>(), static_cast<long>(expected));
+  }
+
+  {
+    auto const data     = std::deque<double>{-1.1, -2.2, -3.3, -4.4, -5.5, -6.6};
+    auto plus           = [](int const x, double const y) { return x + y; };
+    auto const expected = -21.6; // int(  0.0) + -1.1 =   0 + -1.1 =  -1.1
+                                 // int(- 1.1) + -2.2 = - 1 + -2.2 =  -3.2
+                                 // int(- 3.2) + -3.3 = - 3 + -3.3 =  -6.3
+                                 // int(- 6.3) + -4.4 = - 6 + -4.4 = -10.4
+                                 // int(-10.4) + -5.5 = -10 + -5.5 = -15.5
+                                 // int(-15.5) + -6.6 = -15 + -6.6 = -21.6.
+    check(data, 0.0, plus, expected);
+  }
+}
+
+int main(int, char**) {
+  check();
+  static_assert(check());
+  runtime_only_test();
+  return 0;
+}
diff --git a/libcxx/test/support/invocable_with_telemetry.h b/libcxx/test/support/invocable_with_telemetry.h
new file mode 100644
index 00000000000000..594e9a429d91f3
--- /dev/null
+++ b/libcxx/test/support/invocable_with_telemetry.h
@@ -0,0 +1,100 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef TEST_SUPPORT_INVOCABLE_WITH_TELEMETRY_H
+#define TEST_SUPPORT_INVOCABLE_WITH_TELEMETRY_H
+
+#include <cassert>
+#include <concepts>
+#include <utility>
+
+#if TEST_STD_VER < 20
+#  error invocable_with_telemetry requires C++20
+#else
+template <class F>
+class invocable_with_telemetry {
+public:
+  invocable_with_telemetry() = default;
+
+  constexpr invocable_with_telemetry(F f, int& invocations, int& moves, int& copies)
+      : f_(f), invocations_(&invocations), moves_(&moves), copies_(&copies) {}
+
+  constexpr invocable_with_telemetry(invocable_with_telemetry&& other)
+    requires std::move_constructible<F>
+      : f_(std::move(other.f_)),
+        invocations_(assert(invocations_ != nullptr), std::exchange(other.invocations_, nullptr)),
+        moves_(assert(moves_ != nullptr), std::exchange(other.moves_, nullptr)),
+        copies_(assert(copies_ != nullptr), std::exchange(other.copies_, nullptr)) {
+    ++*moves_;
+  }
+
+  constexpr invocable_with_telemetry(invocable_with_telemetry const& other)
+    requires std::copy_constructible<F>
+      : f_(other.f_),
+        invocations_((assert(other.invocations_ != nullptr), other.invocations_)),
+        moves_((assert(other.moves_ != nullptr), other.moves_)),
+        copies_((assert(other.copies_ != nullptr), other.copies_)) {
+    ++*copies_;
+  }
+
+  constexpr invocable_with_telemetry& operator=(invocable_with_telemetry&& other)
+    requires std::movable<F>
+  {
+    // Not using move-and-swap idiom to ensure that copies and moves remain accurate.
+    assert(&other != this);
+    assert(other.invocations_ != nullptr);
+    assert(other.moves_ != nullptr);
+    assert(other.copies_ != nullptr);
+
+    f_           = std::move(other.f_);
+    invocations_ = std::exchange(other.invocations_, nullptr);
+    moves_       = std::exchange(other.moves_, nullptr);
+    copies_      = std::exchange(other.copies_, nullptr);
+
+    ++*moves_;
+    return *this;
+  }
+
+  constexpr invocable_with_telemetry& operator=(invocable_with_telemetry const& other)
+    requires std::copyable<F>
+  {
+    // Not using copy-and-swap idiom to ensure that copies and moves remain accurate.
+    assert(&other != this);
+    assert(invocations_ != nullptr);
+    assert(moves_ != nullptr);
+    assert(copies_ != nullptr);
+
+    f_           = other.f_;
+    invocations_ = other.invocations_;
+    moves_       = other.moves_;
+    copies_      = other.copies_;
+
+    ++*copies_;
+    return *this;
+  }
+
+  template <class... Args>
+    requires std::invocable<F&, Args...>
+  constexpr decltype(auto) operator()(Args&&... args) noexcept(std::is_nothrow_invocable_v<F&, Args...>) {
+    assert(invocations_);
+    ++*invocations_;
+    return f_(std::forward<Args>(args)...);
+  }
+
+private:
+  F f_              = F();
+  int* invocations_ = nullptr;
+  int* moves_       = nullptr;
+  int* copies_      = nullptr;
+};
+
+template <class F>
+invocable_with_telemetry(F f, int& invocations, int& moves, int& copies) -> invocable_with_telemetry<F>;
+
+#endif // TEST_STD_VER < 20
+#endif // TEST_SUPPORT_INVOCABLE_WITH_TELEMETRY_H
diff --git a/libcxx/test/support/maths.h b/libcxx/test/support/maths.h
new file mode 100644
index 00000000000000..9fd7721375bf7a
--- /dev/null
+++ b/libcxx/test/support/maths.h
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LIBCXX_TEST_MATHS_H
+#define LIBCXX_TEST_MATHS_H
+
+#include <algorithm>
+#include <cassert>
+#include <concepts>
+#include <ranges>
+#include <vector>
+
+template <std::ranges::forward_range R>
+constexpr std::ranges::range_value_t<R> triangular_sum(R& input) {
+  assert(not std::ranges::empty(input));
+  auto [min, max] = std::ranges::minmax_element(input);
+  return static_cast<std::ranges::range_value_t<R>>(
+      (static_cast<double>(std::ranges::distance(input)) / 2) * (*min + *max));
+}
+
+template <std::integral I>
+constexpr I factorial(I const n) {
+  assert(n >= 0);
+  auto result = I(1);
+  for (auto i = I(1); i <= n; ++i) {
+    result *= i;
+  }
+
+  return result;
+}
+static_assert(factorial(0) == 1);
+static_assert(factorial(1) == 1);
+static_assert(factorial(2) == 2);
+static_assert(factorial(3) == 6);
+static_assert(factorial(4) == 24);
+static_assert(factorial(5) == 120);
+
+template <std::integral I>
+constexpr I fibonacci(I const n) {
+  assert(n >= 0);
+
+  auto result = I(0);
+  auto prev   = I(1);
+  for (auto i = I(0); i < n; ++i) {
+    result += std::exchange(prev, result);
+  }
+  return result;
+}
+static_assert(fibonacci(0) == 0);
+static_assert(fibonacci(1) == 1);
+static_assert(fibonacci(2) == 1);
+static_assert(fibonacci(3) == 2);
+static_assert(fibonacci(4) == 3);
+static_assert(fibonacci(5) == 5);
+static_assert(fibonacci(6) == 8);
+static_assert(fibonacci(7) == 13);
+static_assert(fibonacci(8) == 21);
+static_assert(fibonacci(9) == 34);
+
+#endif // LIBCXX_TEST_MATHS_H

>From b0e5f7c5fe47761d8e6f0084219ba8af12bb8932 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Mon, 18 Dec 2023 22:47:35 +0000
Subject: [PATCH 14/17] adds documentation and fixes telemetry move

---
 libcxx/docs/Status/RangesAlgorithms.csv       |  6 ++++++
 libcxx/include/algorithm                      | 20 +++++++++++++++++++
 .../test/support/invocable_with_telemetry.h   |  6 +++---
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index 17c8953bf8d85d..2fe530bf75fd94 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -10,3 +10,9 @@ C++23,`shift_right <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not sta
 C++23,`iota (algorithm) <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
 C++23,`fold <https://wg21.link/p2322r5>`_,Unassigned,No patch yet,Not started
 C++23,`contains <https://wg21.link/p2302r2>`_,Zijun Zhao,No patch yet,In Progress
+C++23,`fold_left_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
+C++23,`fold_left <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,Complete
+C++23,`fold_left_first_with_iter <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
+C++23,`fold_left_first <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
+C++23,`fold_right <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
+C++23,`fold_right_last <https://wg21.link/p2322r6>`_,Christopher Di Bella,N/A,In progress
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 3fd9ba10f6310b..5ce017e065baf4 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -42,6 +42,9 @@ namespace ranges {
   template <class I>
     struct in_found_result;              // since C++20
 
+  template <class I, class T>
+    struct in_value_result;              // since C++23
+
   template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
     indirect_strict_weak_order<projected<I, Proj>> Comp = ranges::less>                                   // since C++20
   constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {});
@@ -873,6 +876,23 @@ namespace ranges {
       ranges::search_n(R&& r, range_difference_t<R> count,
                        const T& value, Pred pred = {}, Proj proj = {});                             // since C++20
 
+  template<input_iterator I, sentinel_for<I> S, class T,
+           indirectly-binary-left-foldable<T, I> F>
+    constexpr auto ranges::fold_left(I first, S last, T init, F f);                                 // since C++23
+
+  template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+    constexpr auto fold_left(R&& r, T init, F f);                                                   // since C++23
+
+  template<class I, class T>
+    using fold_left_with_iter_result = in_value_result<I, T>;                                       // since C++23
+
+  template<input_iterator I, sentinel_for<I> S, class T,
+           indirectly-binary-left-foldable<T, I> F>
+    constexpr see below fold_left_with_iter(I first, S last, T init, F f);                          // since C++23
+
+  template<input_range R, class T, indirectly-binary-left-foldable<T, iterator_t<R>> F>
+    constexpr see below fold_left_with_iter(R&& r, T init, F f);                                    // since C++23
+
   template<forward_iterator I1, sentinel_for<I1> S1, forward_iterator I2, sentinel_for<I2> S2,
            class Pred = ranges::equal_to, class Proj1 = identity, class Proj2 = identity>
     requires indirectly_comparable<I1, I2, Pred, Proj1, Proj2>
diff --git a/libcxx/test/support/invocable_with_telemetry.h b/libcxx/test/support/invocable_with_telemetry.h
index 594e9a429d91f3..51e0356eb064e6 100644
--- a/libcxx/test/support/invocable_with_telemetry.h
+++ b/libcxx/test/support/invocable_with_telemetry.h
@@ -27,9 +27,9 @@ class invocable_with_telemetry {
   constexpr invocable_with_telemetry(invocable_with_telemetry&& other)
     requires std::move_constructible<F>
       : f_(std::move(other.f_)),
-        invocations_(assert(invocations_ != nullptr), std::exchange(other.invocations_, nullptr)),
-        moves_(assert(moves_ != nullptr), std::exchange(other.moves_, nullptr)),
-        copies_(assert(copies_ != nullptr), std::exchange(other.copies_, nullptr)) {
+        invocations_(assert(other.invocations_ != nullptr), std::exchange(other.invocations_, nullptr)),
+        moves_(assert(other.moves_ != nullptr), std::exchange(other.moves_, nullptr)),
+        copies_(assert(other.copies_ != nullptr), std::exchange(other.copies_, nullptr)) {
     ++*moves_;
   }
 

>From 824e1feb98d6da464fdcf6e28e360b0fe0d5f338 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Mon, 18 Dec 2023 22:48:28 +0000
Subject: [PATCH 15/17] fixes telemetry move assignment

---
 libcxx/test/support/invocable_with_telemetry.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/support/invocable_with_telemetry.h b/libcxx/test/support/invocable_with_telemetry.h
index 51e0356eb064e6..405efbc69be531 100644
--- a/libcxx/test/support/invocable_with_telemetry.h
+++ b/libcxx/test/support/invocable_with_telemetry.h
@@ -65,9 +65,9 @@ class invocable_with_telemetry {
   {
     // Not using copy-and-swap idiom to ensure that copies and moves remain accurate.
     assert(&other != this);
-    assert(invocations_ != nullptr);
-    assert(moves_ != nullptr);
-    assert(copies_ != nullptr);
+    assert(other.invocations_ != nullptr);
+    assert(other.moves_ != nullptr);
+    assert(other.copies_ != nullptr);
 
     f_           = other.f_;
     invocations_ = other.invocations_;

>From 1b1b3d45e290f59c97b5bd5e4e0f9cd11777ff3d Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 19 Dec 2023 00:25:41 +0000
Subject: [PATCH 16/17] fixes in_value_result

---
 libcxx/include/__algorithm/fold.h             |  23 +++-
 .../alg.fold/left_folds.pass.cpp              |  19 ++--
 .../in_value_result.pass.cpp                  | 104 ++++++++++++++++++
 .../no_unique_address.compile.pass.cpp        |   4 +
 ...result_alias_declarations.compile.pass.cpp |   2 +
 5 files changed, 137 insertions(+), 15 deletions(-)
 create mode 100644 libcxx/test/std/algorithms/algorithms.results/in_value_result.pass.cpp

diff --git a/libcxx/include/__algorithm/fold.h b/libcxx/include/__algorithm/fold.h
index ebc4bc414d740f..40f1fde42a47cd 100644
--- a/libcxx/include/__algorithm/fold.h
+++ b/libcxx/include/__algorithm/fold.h
@@ -39,8 +39,20 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 namespace ranges {
 template <class _Ip, class _Tp>
 struct in_value_result {
-  _Ip in;
-  _Tp result;
+  [[no_unique_address]] _Ip in;
+  [[no_unique_address]] _Tp value;
+
+  template <class _I2, class _T2>
+    requires convertible_to<const _Ip&, _I2> && convertible_to<const _Tp&, _T2>
+  constexpr operator in_value_result<_I2, _T2>() const& {
+    return {in, value};
+  }
+
+  template <class _I2, class _T2>
+    requires convertible_to<_Ip, _I2> && convertible_to<_Tp, _T2>
+  constexpr operator in_value_result<_I2, _T2>() && {
+    return {std::move(in), std::move(value)};
+  }
 };
 
 template <class _Ip, class _Tp>
@@ -84,8 +96,7 @@ struct __fold_left_with_iter {
     auto __result = operator()(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f));
 
     using _Up = decay_t<invoke_result_t<_Fp&, _Tp, range_reference_t<_Rp>>>;
-    return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{
-        std::move(__result.in), std::move(__result.result)};
+    return fold_left_with_iter_result<borrowed_iterator_t<_Rp>, _Up>{std::move(__result.in), std::move(__result.value)};
   }
 };
 
@@ -95,12 +106,12 @@ struct __fold_left {
   template <input_iterator _Ip, sentinel_for<_Ip> _Sp, class _Tp, __indirectly_binary_left_foldable<_Tp, _Ip> _Fp>
   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto
   operator()(_Ip __first, _Sp __last, _Tp __init, _Fp __f) {
-    return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).result;
+    return fold_left_with_iter(std::move(__first), std::move(__last), std::move(__init), std::ref(__f)).value;
   }
 
   template <input_range _Rp, class _Tp, __indirectly_binary_left_foldable<_Tp, iterator_t<_Rp>> _Fp>
   _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI static constexpr auto operator()(_Rp&& __r, _Tp __init, _Fp __f) {
-    return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).result;
+    return fold_left_with_iter(ranges::begin(__r), ranges::end(__r), std::move(__init), std::ref(__f)).value;
   }
 };
 
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
index 8290b1d5b7e344..0e1acf1d0a97c6 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
@@ -46,10 +46,11 @@ using std::ranges::fold_left;
 using std::ranges::fold_left_with_iter;
 
 template <class Result, class Range, class T>
-concept is_in_value_result = std::same_as<Result, std::ranges::in_value_result<std::ranges::iterator_t<Range>, T>>;
+concept is_in_value_result =
+    std::same_as<Result, std::ranges::fold_left_with_iter_result<std::ranges::iterator_t<Range>, T>>;
 
 template <class Result, class T>
-concept is_dangling_with = std::same_as<Result, std::ranges::in_value_result<std::ranges::dangling, T>>;
+concept is_dangling_with = std::same_as<Result, std::ranges::fold_left_with_iter_result<std::ranges::dangling, T>>;
 
 template <std::ranges::input_range R, class T, class F, std::equality_comparable Expected>
   requires std::copyable<R>
@@ -57,7 +58,7 @@ constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected
   {
     is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r.begin(), r.end(), init, f);
     assert(result.in == r.end());
-    assert(result.result == expected);
+    assert(result.value == expected);
   }
   {
     auto invocations                                      = 0;
@@ -66,7 +67,7 @@ constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected
     auto f2                                               = invocable_with_telemetry(f, invocations, moves, copies);
     is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r.begin(), r.end(), init, f2);
     assert(result.in == r.end());
-    assert(result.result == expected);
+    assert(result.value == expected);
     assert(invocations == std::ranges::distance(r));
     assert(moves == 0);
     assert(copies == 1);
@@ -95,7 +96,7 @@ constexpr void check_lvalue_range(R& r, T const& init, F f, Expected const& expe
   {
     is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r, init, f);
     assert(result.in == r.end());
-    assert(result.result == expected);
+    assert(result.value == expected);
   }
   {
     auto invocations                             = 0;
@@ -132,7 +133,7 @@ constexpr void check_rvalue_range(R& r, T const& init, F f, Expected const& expe
   {
     auto r2                                          = r;
     is_dangling_with<Expected> decltype(auto) result = fold_left_with_iter(std::move(r2), init, f);
-    assert(result.result == expected);
+    assert(result.value == expected);
   }
   {
     auto invocations                                 = 0;
@@ -141,7 +142,7 @@ constexpr void check_rvalue_range(R& r, T const& init, F f, Expected const& expe
     auto f2                                          = invocable_with_telemetry(f, invocations, moves, copies);
     auto r2                                          = r;
     is_dangling_with<Expected> decltype(auto) result = fold_left_with_iter(std::move(r2), init, f2);
-    assert(result.result == expected);
+    assert(result.value == expected);
     assert(invocations == std::ranges::distance(r));
     assert(moves == 0);
     assert(copies == 1);
@@ -249,7 +250,7 @@ void runtime_only_test() {
           fold_left_with_iter(data.begin(), data.end(), init, std::plus());
 
       assert(result.in == data.end());
-      assert(result.result == expected);
+      assert(result.value == expected);
     }
     {
       auto input = std::istringstream(raw_data);
@@ -257,7 +258,7 @@ void runtime_only_test() {
       is_in_value_result<std::ranges::basic_istream_view<std::string, char>, std::string> decltype(auto) result =
           fold_left_with_iter(data, init, std::plus());
       assert(result.in == data.end());
-      assert(result.result == expected);
+      assert(result.value == expected);
     }
     {
       auto input = std::istringstream(raw_data);
diff --git a/libcxx/test/std/algorithms/algorithms.results/in_value_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/in_value_result.pass.cpp
new file mode 100644
index 00000000000000..f80b15e385dc9b
--- /dev/null
+++ b/libcxx/test/std/algorithms/algorithms.results/in_value_result.pass.cpp
@@ -0,0 +1,104 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// template <class I1, class I2>
+// struct in_value_result;
+
+#include <algorithm>
+#include <cassert>
+#include <type_traits>
+#include <utility>
+
+#include "MoveOnly.h"
+
+struct A {
+  explicit A(int);
+};
+// no implicit conversion
+static_assert(!std::is_constructible_v<std::ranges::in_value_result<A, A>, std::ranges::in_value_result<int, int>>);
+
+struct B {
+  B(int);
+};
+// implicit conversion
+static_assert(std::is_constructible_v<std::ranges::in_value_result<B, B>, std::ranges::in_value_result<int, int>>);
+static_assert(std::is_constructible_v<std::ranges::in_value_result<B, B>, std::ranges::in_value_result<int, int>&>);
+static_assert(
+    std::is_constructible_v<std::ranges::in_value_result<B, B>, const std::ranges::in_value_result<int, int>>);
+static_assert(
+    std::is_constructible_v<std::ranges::in_value_result<B, B>, const std::ranges::in_value_result<int, int>&>);
+
+struct C {
+  C(int&);
+};
+static_assert(!std::is_constructible_v<std::ranges::in_value_result<C, C>, std::ranges::in_value_result<int, int>&>);
+
+// has to be convertible via const&
+static_assert(std::is_convertible_v<std::ranges::in_value_result<int, int>&, std::ranges::in_value_result<long, long>>);
+static_assert(
+    std::is_convertible_v<const std::ranges::in_value_result<int, int>&, std::ranges::in_value_result<long, long>>);
+static_assert(
+    std::is_convertible_v<std::ranges::in_value_result<int, int>&&, std::ranges::in_value_result<long, long>>);
+static_assert(
+    std::is_convertible_v<const std::ranges::in_value_result<int, int>&&, std::ranges::in_value_result<long, long>>);
+
+// should be move constructible
+static_assert(std::is_move_constructible_v<std::ranges::in_value_result<MoveOnly, int>>);
+static_assert(std::is_move_constructible_v<std::ranges::in_value_result<int, MoveOnly>>);
+
+// should not copy constructible with move-only type
+static_assert(!std::is_copy_constructible_v<std::ranges::in_value_result<MoveOnly, int>>);
+static_assert(!std::is_copy_constructible_v<std::ranges::in_value_result<int, MoveOnly>>);
+
+struct NotConvertible {};
+// conversions should not work if there is no conversion
+static_assert(
+    !std::is_convertible_v<std::ranges::in_value_result<NotConvertible, int>, std::ranges::in_value_result<int, int>>);
+static_assert(
+    !std::is_convertible_v<std::ranges::in_value_result<int, NotConvertible>, std::ranges::in_value_result<int, int>>);
+
+template <class T>
+struct ConvertibleFrom {
+  constexpr ConvertibleFrom(T c) : content{c} {}
+  T content;
+};
+
+constexpr bool test() {
+  {
+    std::ranges::in_value_result<int, double> res{10, 0.};
+    assert(res.in == 10);
+    assert(res.value == 0.);
+    std::ranges::in_value_result<ConvertibleFrom<int>, ConvertibleFrom<double>> res2 = res;
+    assert(res2.in.content == 10);
+    assert(res2.value.content == 0.);
+  }
+  {
+    std::ranges::in_value_result<MoveOnly, int> res{MoveOnly{}, 2};
+    assert(res.in.get() == 1);
+    assert(res.value == 2);
+    auto res2 = static_cast<std::ranges::in_value_result<MoveOnly, int>>(std::move(res));
+    assert(res.in.get() == 0);
+    assert(res2.in.get() == 1);
+    assert(res2.value == 2);
+  }
+  {
+    auto [in, value] = std::ranges::in_value_result<int, int>{1, 2};
+    assert(in == 1);
+    assert(value == 2);
+  }
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp
index 8e0a81959f27c8..11ff32c3ab976c 100644
--- a/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp
+++ b/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp
@@ -53,6 +53,10 @@ static_assert(sizeof(std::ranges::in_out_out_result<Empty, Empty, char>) == 2);
 static_assert(sizeof(std::ranges::in_out_out_result<int, Empty, Empty2>) == sizeof(int));
 static_assert(sizeof(std::ranges::in_out_out_result<Empty, Empty, Empty>) == 3);
 
+static_assert(sizeof(std::ranges::in_value_result<Empty, int>) == sizeof(int));
+static_assert(sizeof(std::ranges::in_value_result<int, Empty>) == sizeof(int));
+static_assert(sizeof(std::ranges::in_value_result<Empty, Empty2>) == sizeof(Empty2));
+
 // In min_max_result both elements have the same type, so they can't have the same address.
 // So the only way to test that [[no_unique_address]] is used is to have it in another struct
 struct MinMaxNoUniqueAddress {
diff --git a/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp b/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
index a72c3a374c504c..0f93ac06019991 100644
--- a/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_result_alias_declarations.compile.pass.cpp
@@ -59,4 +59,6 @@ static_assert(std::is_same_v<min_max_result<int>, minmax_element_result<int>>);
 static_assert(std::is_same_v<in_found_result<int>, next_permutation_result<int>>);
 static_assert(std::is_same_v<in_found_result<int>, prev_permutation_result<int>>);
 
+static_assert(std::is_same_v<in_value_result<int, long>, fold_left_with_iter_result<int, long>>);
+
 // static_assert(std::is_same_v<out_value_result<int>, iota_result<int>>);

>From 141be9754313d239d5a0df61fa18935cd276b0b5 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Tue, 19 Dec 2023 01:29:37 +0000
Subject: [PATCH 17/17] replaces rogue integers with a telemetry struct

---
 .../alg.fold/left_folds.pass.cpp              | 72 ++++++++-----------
 .../test/support/invocable_with_telemetry.h   | 56 ++++++---------
 libcxx/test/support/maths.h                   |  3 +
 3 files changed, 55 insertions(+), 76 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
index 0e1acf1d0a97c6..3318525d4fe4be 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.fold/left_folds.pass.cpp
@@ -61,16 +61,14 @@ constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected
     assert(result.value == expected);
   }
   {
-    auto invocations                                      = 0;
-    auto moves                                            = 0;
-    auto copies                                           = 0;
-    auto f2                                               = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                                        = invocable_telemetry();
+    auto f2                                               = invocable_with_telemetry(f, telemetry);
     is_in_value_result<R, Expected> decltype(auto) result = fold_left_with_iter(r.begin(), r.end(), init, f2);
     assert(result.in == r.end());
     assert(result.value == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 
   {
@@ -78,15 +76,13 @@ constexpr void check_iterator(R& r, T const& init, F f, Expected const& expected
     assert(result == expected);
   }
   {
-    auto invocations                             = 0;
-    auto moves                                   = 0;
-    auto copies                                  = 0;
-    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                               = invocable_telemetry();
+    auto f2                                      = invocable_with_telemetry(f, telemetry);
     std::same_as<Expected> decltype(auto) result = fold_left(r.begin(), r.end(), init, f2);
     assert(result == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 }
 
@@ -99,15 +95,13 @@ constexpr void check_lvalue_range(R& r, T const& init, F f, Expected const& expe
     assert(result.value == expected);
   }
   {
-    auto invocations                             = 0;
-    auto moves                                   = 0;
-    auto copies                                  = 0;
-    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                               = invocable_telemetry();
+    auto f2                                      = invocable_with_telemetry(f, telemetry);
     std::same_as<Expected> decltype(auto) result = fold_left(r, init, f2);
     assert(result == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 
   {
@@ -115,15 +109,13 @@ constexpr void check_lvalue_range(R& r, T const& init, F f, Expected const& expe
     assert(result == expected);
   }
   {
-    auto invocations                             = 0;
-    auto moves                                   = 0;
-    auto copies                                  = 0;
-    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                               = invocable_telemetry();
+    auto f2                                      = invocable_with_telemetry(f, telemetry);
     std::same_as<Expected> decltype(auto) result = fold_left(r, init, f2);
     assert(result == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 }
 
@@ -136,16 +128,14 @@ constexpr void check_rvalue_range(R& r, T const& init, F f, Expected const& expe
     assert(result.value == expected);
   }
   {
-    auto invocations                                 = 0;
-    auto moves                                       = 0;
-    auto copies                                      = 0;
-    auto f2                                          = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                                   = invocable_telemetry();
+    auto f2                                          = invocable_with_telemetry(f, telemetry);
     auto r2                                          = r;
     is_dangling_with<Expected> decltype(auto) result = fold_left_with_iter(std::move(r2), init, f2);
     assert(result.value == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 
   {
@@ -154,16 +144,14 @@ constexpr void check_rvalue_range(R& r, T const& init, F f, Expected const& expe
     assert(result == expected);
   }
   {
-    auto invocations                             = 0;
-    auto moves                                   = 0;
-    auto copies                                  = 0;
-    auto f2                                      = invocable_with_telemetry(f, invocations, moves, copies);
+    auto telemetry                               = invocable_telemetry();
+    auto f2                                      = invocable_with_telemetry(f, telemetry);
     auto r2                                      = r;
     std::same_as<Expected> decltype(auto) result = fold_left(std::move(r2), init, f2);
     assert(result == expected);
-    assert(invocations == std::ranges::distance(r));
-    assert(moves == 0);
-    assert(copies == 1);
+    assert(telemetry.invocations == std::ranges::distance(r));
+    assert(telemetry.moves == 0);
+    assert(telemetry.copies == 1);
   }
 }
 
diff --git a/libcxx/test/support/invocable_with_telemetry.h b/libcxx/test/support/invocable_with_telemetry.h
index 405efbc69be531..42c6b31a9b1883 100644
--- a/libcxx/test/support/invocable_with_telemetry.h
+++ b/libcxx/test/support/invocable_with_telemetry.h
@@ -16,30 +16,28 @@
 #if TEST_STD_VER < 20
 #  error invocable_with_telemetry requires C++20
 #else
+struct invocable_telemetry {
+  int invocations;
+  int moves;
+  int copies;
+};
+
 template <class F>
 class invocable_with_telemetry {
 public:
-  invocable_with_telemetry() = default;
-
-  constexpr invocable_with_telemetry(F f, int& invocations, int& moves, int& copies)
-      : f_(f), invocations_(&invocations), moves_(&moves), copies_(&copies) {}
+  constexpr invocable_with_telemetry(F f, invocable_telemetry& telemetry) : f_(f), telemetry_(&telemetry) {}
 
   constexpr invocable_with_telemetry(invocable_with_telemetry&& other)
     requires std::move_constructible<F>
       : f_(std::move(other.f_)),
-        invocations_(assert(other.invocations_ != nullptr), std::exchange(other.invocations_, nullptr)),
-        moves_(assert(other.moves_ != nullptr), std::exchange(other.moves_, nullptr)),
-        copies_(assert(other.copies_ != nullptr), std::exchange(other.copies_, nullptr)) {
-    ++*moves_;
+        telemetry_(assert(other.telemetry_ != nullptr), std::exchange(other.telemetry_, nullptr)) {
+    ++telemetry_->moves;
   }
 
   constexpr invocable_with_telemetry(invocable_with_telemetry const& other)
     requires std::copy_constructible<F>
-      : f_(other.f_),
-        invocations_((assert(other.invocations_ != nullptr), other.invocations_)),
-        moves_((assert(other.moves_ != nullptr), other.moves_)),
-        copies_((assert(other.copies_ != nullptr), other.copies_)) {
-    ++*copies_;
+      : f_(other.f_), telemetry_((assert(other.telemetry_ != nullptr), other.telemetry_)) {
+    ++telemetry_->copies;
   }
 
   constexpr invocable_with_telemetry& operator=(invocable_with_telemetry&& other)
@@ -47,16 +45,12 @@ class invocable_with_telemetry {
   {
     // Not using move-and-swap idiom to ensure that copies and moves remain accurate.
     assert(&other != this);
-    assert(other.invocations_ != nullptr);
-    assert(other.moves_ != nullptr);
-    assert(other.copies_ != nullptr);
+    assert(other.telemetry_ != nullptr);
 
-    f_           = std::move(other.f_);
-    invocations_ = std::exchange(other.invocations_, nullptr);
-    moves_       = std::exchange(other.moves_, nullptr);
-    copies_      = std::exchange(other.copies_, nullptr);
+    f_         = std::move(other.f_);
+    telemetry_ = std::exchange(other.telemetry_, nullptr);
 
-    ++*moves_;
+    ++telemetry_->moves;
     return *this;
   }
 
@@ -65,32 +59,26 @@ class invocable_with_telemetry {
   {
     // Not using copy-and-swap idiom to ensure that copies and moves remain accurate.
     assert(&other != this);
-    assert(other.invocations_ != nullptr);
-    assert(other.moves_ != nullptr);
-    assert(other.copies_ != nullptr);
+    assert(other.telemetry_ != nullptr);
 
-    f_           = other.f_;
-    invocations_ = other.invocations_;
-    moves_       = other.moves_;
-    copies_      = other.copies_;
+    f_         = other.f_;
+    telemetry_ = other.telemetry_;
 
-    ++*copies_;
+    ++telemetry_->copies;
     return *this;
   }
 
   template <class... Args>
     requires std::invocable<F&, Args...>
   constexpr decltype(auto) operator()(Args&&... args) noexcept(std::is_nothrow_invocable_v<F&, Args...>) {
-    assert(invocations_);
-    ++*invocations_;
+    assert(telemetry_);
+    ++telemetry_->invocations;
     return f_(std::forward<Args>(args)...);
   }
 
 private:
   F f_              = F();
-  int* invocations_ = nullptr;
-  int* moves_       = nullptr;
-  int* copies_      = nullptr;
+  invocable_telemetry* telemetry_ = nullptr;
 };
 
 template <class F>
diff --git a/libcxx/test/support/maths.h b/libcxx/test/support/maths.h
index 9fd7721375bf7a..11c507bcb07cf2 100644
--- a/libcxx/test/support/maths.h
+++ b/libcxx/test/support/maths.h
@@ -6,6 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
+// Implementations of well-known functions in mathematics that are useful for
+// testing algorithms.
+
 #ifndef LIBCXX_TEST_MATHS_H
 #define LIBCXX_TEST_MATHS_H
 



More information about the cfe-commits mailing list