[libcxx-commits] [libcxx] [libc++][ranges] P1223R5: `find_last` (PR #99312)

nicole mazzuca via libcxx-commits libcxx-commits at lists.llvm.org
Thu Jul 18 14:34:27 PDT 2024


https://github.com/strega-nil updated https://github.com/llvm/llvm-project/pull/99312

>From c162555c5b540f69f657d2fc7d0ec54002b83da6 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Wed, 17 Jul 2024 00:43:09 +0200
Subject: [PATCH 1/9] P1223R5: find_last

Implements [P1223R5][] completely.

Includes an implementation of `find_last`, `find_last_if`, and
`find_last_if_not`.

[P1223R5]: https://wg21.link/p1223r5
---
 libcxx/docs/FeatureTestMacroTable.rst         |   2 +
 libcxx/docs/Status/Cxx23Papers.csv            |   2 +-
 libcxx/include/CMakeLists.txt                 |   3 +
 libcxx/include/__algorithm/ranges_find_if.h   |   2 +
 libcxx/include/__algorithm/ranges_find_last.h | 103 ++++++
 .../include/__algorithm/ranges_find_last_if.h | 106 +++++++
 .../__algorithm/ranges_find_last_if_not.h     |  70 +++++
 libcxx/include/algorithm                      |  23 ++
 libcxx/include/module.modulemap               |   3 +
 libcxx/include/version                        |   2 +
 libcxx/modules/std/algorithm.inc              |   3 +-
 ...obust_against_copying_comparators.pass.cpp |   6 +
 ...obust_against_copying_projections.pass.cpp |   8 +
 .../algorithm.nodiscard.verify.cpp            |  12 +
 .../alg.find.last/ranges.find_last.pass.cpp   | 294 ++++++++++++++++++
 .../ranges.find_last_if.pass.cpp              | 263 ++++++++++++++++
 .../ranges.find_last_if_not.pass.cpp          | 266 ++++++++++++++++
 .../ranges_robust_against_dangling.pass.cpp   |   5 +
 ...es_robust_against_nonbool.compile.pass.cpp |   4 +
 ...es_robust_against_proxy_iterators.pass.cpp |   5 +
 .../algorithm.version.compile.pass.cpp        |  31 ++
 .../version.version.compile.pass.cpp          |  31 ++
 .../niebloid.compile.pass.cpp                 |   3 +
 .../generate_feature_test_macro_components.py |   5 +
 24 files changed, 1249 insertions(+), 3 deletions(-)
 create mode 100644 libcxx/include/__algorithm/ranges_find_last.h
 create mode 100644 libcxx/include/__algorithm/ranges_find_last_if.h
 create mode 100644 libcxx/include/__algorithm/ranges_find_last_if_not.h
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
 create mode 100644 libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp

diff --git a/libcxx/docs/FeatureTestMacroTable.rst b/libcxx/docs/FeatureTestMacroTable.rst
index 1e347d043ef69..fcffa8d619ab9 100644
--- a/libcxx/docs/FeatureTestMacroTable.rst
+++ b/libcxx/docs/FeatureTestMacroTable.rst
@@ -360,6 +360,8 @@ Status
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_contains``                              ``202207L``
     ---------------------------------------------------------- -----------------
+    ``__cpp_lib_ranges_find_last``                             ``202207L``
+    ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_iota``                                  *unimplemented*
     ---------------------------------------------------------- -----------------
     ``__cpp_lib_ranges_join_with``                             *unimplemented*
diff --git a/libcxx/docs/Status/Cxx23Papers.csv b/libcxx/docs/Status/Cxx23Papers.csv
index 4f589cd938d7c..ed50498238fd8 100644
--- a/libcxx/docs/Status/Cxx23Papers.csv
+++ b/libcxx/docs/Status/Cxx23Papers.csv
@@ -55,7 +55,7 @@
 "`P0429R9 <https://wg21.link/P0429R9>`__","LWG","A Standard ``flat_map``","July 2022","",""
 "`P1169R4 <https://wg21.link/P1169R4>`__","LWG","``static operator()``","July 2022","|Complete|","16.0"
 "`P1222R4 <https://wg21.link/P1222R4>`__","LWG","A Standard ``flat_set``","July 2022","",""
-"`P1223R5 <https://wg21.link/P1223R5>`__","LWG","``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``","July 2022","","","|ranges|"
+"`P1223R5 <https://wg21.link/P1223R5>`__","LWG","``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``","July 2022","|Complete|","19.0","|ranges|"
 "`P1467R9 <https://wg21.link/P1467R9>`__","LWG","Extended ``floating-point`` types and standard names","July 2022","",""
 "`P1642R11 <https://wg21.link/P1642R11>`__","LWG","Freestanding ``[utilities]``, ``[ranges]``, and ``[iterators]``","July 2022","",""
 "`P1899R3 <https://wg21.link/P1899R3>`__","LWG","``stride_view``","July 2022","","","|ranges|"
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index cd64fe91449c2..824806d562d23 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -97,6 +97,9 @@ set(files
   __algorithm/ranges_find_first_of.h
   __algorithm/ranges_find_if.h
   __algorithm/ranges_find_if_not.h
+  __algorithm/ranges_find_last.h
+  __algorithm/ranges_find_last_if.h
+  __algorithm/ranges_find_last_if_not.h
   __algorithm/ranges_for_each.h
   __algorithm/ranges_for_each_n.h
   __algorithm/ranges_generate.h
diff --git a/libcxx/include/__algorithm/ranges_find_if.h b/libcxx/include/__algorithm/ranges_find_if.h
index 888f9ec3cb2d5..fd2c090af2d6d 100644
--- a/libcxx/include/__algorithm/ranges_find_if.h
+++ b/libcxx/include/__algorithm/ranges_find_if.h
@@ -14,6 +14,8 @@
 #include <__functional/invoke.h>
 #include <__functional/ranges_operations.h>
 #include <__iterator/concepts.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
 #include <__iterator/projected.h>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
diff --git a/libcxx/include/__algorithm/ranges_find_last.h b/libcxx/include/__algorithm/ranges_find_last.h
new file mode 100644
index 0000000000000..1ce056d719a09
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_find_last.h
@@ -0,0 +1,103 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_RANGES_FIND_LAST_H
+#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_H
+
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__functional/ranges_operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/indirectly_comparable.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+namespace __find_last {
+struct __fn {
+  template <class _Iter, class _Sent, class _Type, class _Proj>
+  _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+  __find_last_impl(_Iter __first, _Sent __last, const _Type& __value, _Proj& __proj) {
+    if (__first == __last) {
+      return subrange<_Iter>(__first, __first);
+    }
+
+    if constexpr (bidirectional_iterator<_Iter>) {
+      auto __last_it = ranges::next(__first, __last);
+      for (auto __it = ranges::prev(__last_it); __it != __first; --__it) {
+        if (std::invoke(__proj, *__it) == __value) {
+          return subrange<_Iter>(std::move(__it), std::move(__last_it));
+        }
+      }
+      if (std::invoke(__proj, *__first) == __value) {
+        return subrange<_Iter>(std::move(__first), std::move(__last_it));
+      }
+      return subrange<_Iter>(__last_it, __last_it);
+    } else {
+      bool __found = false;
+      _Iter __found_it;
+      for (; __first != __last; ++__first) {
+        if (std::invoke(__proj, *__first) == __value) {
+          __found    = true;
+          __found_it = __first;
+        }
+      }
+
+      if (__found) {
+        return subrange<_Iter>(std::move(__found_it), std::move(__first));
+      } else {
+        return subrange<_Iter>(__first, __first);
+      }
+    }
+  }
+
+  template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
+    requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) const {
+    return __find_last_impl(std::move(__first), std::move(__last), __value, __proj);
+  }
+
+  template <forward_range _Range, class _Type, class _Proj = identity>
+    requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) const {
+    return __find_last_impl(ranges::begin(__range), ranges::end(__range), __value, __proj);
+  }
+};
+} // namespace __find_last
+
+inline namespace __cpo {
+inline constexpr auto find_last = __find_last::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_H
diff --git a/libcxx/include/__algorithm/ranges_find_last_if.h b/libcxx/include/__algorithm/ranges_find_last_if.h
new file mode 100644
index 0000000000000..2435672a83b63
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_find_last_if.h
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_RANGES_FIND_LAST_IF_H
+#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H
+
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__functional/ranges_operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/next.h>
+#include <__iterator/prev.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+
+template <class _Iter, class _Sent, class _Pred, class _Proj>
+_LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+__find_last_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
+  if (__first == __last) {
+    return subrange<_Iter>(__first, __first);
+  }
+
+  if constexpr (bidirectional_iterator<_Iter>) {
+    auto __last_it = ranges::next(__first, __last);
+    for (auto __it = ranges::prev(__last_it); __it != __first; --__it) {
+      if (std::invoke(__pred, std::invoke(__proj, *__it))) {
+        return subrange<_Iter>(std::move(__it), std::move(__last_it));
+      }
+    }
+    if (std::invoke(__pred, std::invoke(__proj, *__first))) {
+      return subrange<_Iter>(std::move(__first), std::move(__last_it));
+    }
+    return subrange<_Iter>(__last_it, __last_it);
+  } else {
+    bool __found = false;
+    _Iter __found_it;
+    for (; __first != __last; ++__first) {
+      if (std::invoke(__pred, std::invoke(__proj, *__first))) {
+        __found    = true;
+        __found_it = __first;
+      }
+    }
+
+    if (__found) {
+      return subrange<_Iter>(std::move(__found_it), std::move(__first));
+    } else {
+      return subrange<_Iter>(__first, __first);
+    }
+  }
+}
+
+namespace __find_last_if {
+struct __fn {
+  template <forward_iterator _Iter,
+            sentinel_for<_Iter> _Sent,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
+    return __find_last_if_impl(std::move(__first), std::move(__last), __pred, __proj);
+  }
+
+  template <forward_range _Range,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
+    return __find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
+  }
+};
+} // namespace __find_last_if
+
+inline namespace __cpo {
+inline constexpr auto find_last_if = __find_last_if::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H
diff --git a/libcxx/include/__algorithm/ranges_find_last_if_not.h b/libcxx/include/__algorithm/ranges_find_last_if_not.h
new file mode 100644
index 0000000000000..4b246c96695c2
--- /dev/null
+++ b/libcxx/include/__algorithm/ranges_find_last_if_not.h
@@ -0,0 +1,70 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_RANGES_FIND_LAST_IF_NOT_H
+#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_NOT_H
+
+#include <__algorithm/ranges_find_last_if.h>
+#include <__config>
+#include <__functional/identity.h>
+#include <__functional/invoke.h>
+#include <__functional/ranges_operations.h>
+#include <__iterator/concepts.h>
+#include <__iterator/projected.h>
+#include <__ranges/access.h>
+#include <__ranges/concepts.h>
+#include <__ranges/subrange.h>
+#include <__utility/move.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+#if _LIBCPP_STD_VER >= 23
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+namespace ranges {
+namespace __find_last_if_not {
+struct __fn {
+  template <forward_iterator _Iter,
+            sentinel_for<_Iter> _Sent,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
+    auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
+    return ranges::__find_last_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
+  }
+
+  template <forward_range _Range,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
+    auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
+    return ranges::__find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred2, __proj);
+  }
+};
+} // namespace __find_last_if_not
+
+inline namespace __cpo {
+inline constexpr auto find_last_if_not = __find_last_if_not::__fn{};
+} // namespace __cpo
+} // namespace ranges
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP_STD_VER >= 23
+
+_LIBCPP_POP_MACROS
+
+#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_NOT_H
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index a522a60f1b551..f36e396246a1d 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -101,6 +101,26 @@ namespace ranges {
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
     constexpr borrowed_iterator_t<R>
       find_if_not(R&& r, Pred pred, Proj proj = {});                                                      // since C++20
+																																																					//
+  template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity>
+    requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
+    constexpr subrange<I> find_last(I first, S last, const T& value, Proj proj = {});
+	template<forward_range R, class T, class Proj = identity>
+		requires
+			indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
+		constexpr borrowed_subrange_t<R> find_last(R&& r, const T& value, Proj proj = {});
+	template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+           indirect_unary_predicate<projected<I, Proj>> Pred>
+		constexpr subrange<I> find_last_if(I first, S last, Pred pred, Proj proj = {});
+	template<forward_range R, class Proj = identity,
+           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+		constexpr borrowed_subrange_t<R> find_last_if(R&& r, Pred pred, Proj proj = {});
+	template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+           indirect_unary_predicate<projected<I, Proj>> Pred>
+		constexpr subrange<I> find_last_if_not(I first, S last, Pred pred, Proj proj = {});
+	template<forward_range R, class Proj = identity,
+           indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+		constexpr borrowed_subrange_t<R> find_last_if_not(R&& r, Pred pred, Proj proj = {});
 
   template<class T, class Proj = identity,
            indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>
@@ -1989,6 +2009,9 @@ template <class BidirectionalIterator, class Compare>
 #  include <__algorithm/fold.h>
 #  include <__algorithm/ranges_contains_subrange.h>
 #  include <__algorithm/ranges_ends_with.h>
+#  include <__algorithm/ranges_find_last.h>
+#  include <__algorithm/ranges_find_last_if.h>
+#  include <__algorithm/ranges_find_last_if_not.h>
 #  include <__algorithm/ranges_starts_with.h>
 #endif // _LIBCPP_STD_VER >= 23
 
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index f4aaa14c1c2ee..cbe7d03ebc0a2 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -764,6 +764,9 @@ module std_private_algorithm_ranges_find_end                             [system
 module std_private_algorithm_ranges_find_first_of                        [system] { header "__algorithm/ranges_find_first_of.h" }
 module std_private_algorithm_ranges_find_if                              [system] { header "__algorithm/ranges_find_if.h" }
 module std_private_algorithm_ranges_find_if_not                          [system] { header "__algorithm/ranges_find_if_not.h" }
+module std_private_algorithm_ranges_find_last                            [system] { header "__algorithm/ranges_find_last.h" }
+module std_private_algorithm_ranges_find_last_if                         [system] { header "__algorithm/ranges_find_last_if.h" }
+module std_private_algorithm_ranges_find_last_if_not                     [system] { header "__algorithm/ranges_find_last_if_not.h" }
 module std_private_algorithm_ranges_for_each                             [system] {
   header "__algorithm/ranges_for_each.h"
   export std_private_algorithm_in_fun_result
diff --git a/libcxx/include/version b/libcxx/include/version
index c971336bcb85c..6b3c46f8f7715 100644
--- a/libcxx/include/version
+++ b/libcxx/include/version
@@ -190,6 +190,7 @@ __cpp_lib_ranges_chunk                                  202202L <ranges>
 __cpp_lib_ranges_chunk_by                               202202L <ranges>
 __cpp_lib_ranges_concat                                 202403L <ranges>
 __cpp_lib_ranges_contains                               202207L <algorithm>
+__cpp_lib_ranges_find_last                              202207L <algorithm>
 __cpp_lib_ranges_iota                                   202202L <numeric>
 __cpp_lib_ranges_join_with                              202202L <ranges>
 __cpp_lib_ranges_repeat                                 202207L <ranges>
@@ -483,6 +484,7 @@ __cpp_lib_void_t                                        201411L <type_traits>
 // # define __cpp_lib_ranges_chunk                         202202L
 # define __cpp_lib_ranges_chunk_by                      202202L
 # define __cpp_lib_ranges_contains                      202207L
+# define __cpp_lib_ranges_find_last                     202207L
 // # define __cpp_lib_ranges_iota                          202202L
 // # define __cpp_lib_ranges_join_with                     202202L
 # define __cpp_lib_ranges_repeat                        202207L
diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index e7796bfa26af8..6c7a30330008a 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -77,12 +77,11 @@ export namespace std {
     using std::ranges::find_if_not;
   } // namespace ranges
 
+  // [alg.find.last], find last
   namespace ranges {
-#if 0
     using std::ranges::find_last;
     using std::ranges::find_last_if;
     using std::ranges::find_last_if_not;
-#endif
   } // namespace ranges
 
   // [alg.find.end], find end
diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
index 1a6c0d11460c5..3c1cd606c6bff 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
@@ -119,6 +119,12 @@ constexpr bool all_the_algorithms()
     (void)std::ranges::find_if(a, UnaryTrue(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(first, last, UnaryTrue(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(a, UnaryTrue(&copies)); assert(copies == 0);
+#if TEST_STD_VER >= 23
+    (void)std::ranges::find_last_if(first, last, UnaryTrue(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if(a, UnaryTrue(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if_not(a, UnaryTrue(&copies)); assert(copies == 0);
+#endif
     (void)std::ranges::for_each(first, last, UnaryVoid(&copies)); assert(copies == 1); copies = 0;
     (void)std::ranges::for_each(a, UnaryVoid(&copies)); assert(copies == 1); copies = 0;
     (void)std::ranges::for_each_n(first, count, UnaryVoid(&copies)); assert(copies == 1); copies = 0;
diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
index 71823d9afc1a4..95f0893b1440b 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
@@ -115,6 +115,14 @@ constexpr bool all_the_algorithms()
     (void)std::ranges::find_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+#if TEST_STD_VER >= 23
+    (void)std::ranges::find_last(first, last, value, Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last(a, value, Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if_not(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+#endif
     (void)std::ranges::for_each(first, last, UnaryVoid(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::for_each(a, UnaryVoid(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::for_each_n(first, count, UnaryVoid(), Proj(&copies)); assert(copies == 0);
diff --git a/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp b/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
index b36a426082ccb..14febc12a8a2d 100644
--- a/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
+++ b/libcxx/test/libcxx/diagnostics/algorithm.nodiscard.verify.cpp
@@ -372,6 +372,18 @@ void test() {
   // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   std::ranges::contains_subrange(iter, iter, iter, iter);
   // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last_if_not(iter, iter, pred);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last_if_not(range, pred);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last_if(iter, iter, pred);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last_if(range, pred);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last(iter, iter, 1);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
+  std::ranges::find_last(range, 1);
+  // expected-warning at -1 {{ignoring return value of function declared with 'nodiscard' attribute}}
   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());
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
new file mode 100644
index 0000000000000..fe5fd35aeb1bd
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
@@ -0,0 +1,294 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-sign-compare
+// MSVC warning C4242: 'argument': conversion from 'const _Ty' to 'ElementT', possible loss of data
+// MSVC warning C4244: 'argument': conversion from 'const _Ty' to 'ElementT', possible loss of data
+// ADDITIONAL_COMPILE_FLAGS(cl-style-warnings): /wd4242 /wd4244
+
+// template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity>
+//   requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
+//   constexpr subrange<I> ranges::find_last(I first, S last, const T& value, Proj proj = {});
+// template<forward_range R, class T, class Proj = identity>
+//   requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
+//   constexpr borrowed_subrange_t<R> ranges::find_last(R&& r, const T& value, Proj proj = {});
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <deque>
+#include <ranges>
+#include <vector>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+struct NotEqualityComparable {};
+
+template <class It, class Sent = It>
+concept HasFindLastIt = requires(It it, Sent sent) { std::ranges::find_last(it, sent, *it); };
+static_assert(HasFindLastIt<int*>);
+static_assert(HasFindLastIt<forward_iterator<int*>>);
+static_assert(!HasFindLastIt<cpp20_input_iterator<int*>>);
+static_assert(!HasFindLastIt<NotEqualityComparable*>);
+static_assert(!HasFindLastIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasFindLastIt<ForwardIteratorNotIncrementable>);
+static_assert(!HasFindLastIt<forward_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasFindLastIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+
+static_assert(!HasFindLastIt<int*, int>);
+static_assert(!HasFindLastIt<int, int*>);
+
+template <class Range, class ValT>
+concept HasFindLastR = requires(Range r) { std::ranges::find_last(r, ValT{}); };
+static_assert(HasFindLastR<std::array<int, 1>, int>);
+static_assert(!HasFindLastR<int, int>);
+static_assert(!HasFindLastR<std::array<NotEqualityComparable, 1>, NotEqualityComparable>);
+static_assert(!HasFindLastR<ForwardRangeNotDerivedFrom, int>);
+static_assert(!HasFindLastR<ForwardRangeNotIncrementable, int>);
+static_assert(!HasFindLastR<ForwardRangeNotSentinelSemiregular, int>);
+static_assert(!HasFindLastR<ForwardRangeNotSentinelEqualityComparableWith, int>);
+
+static std::vector<int> comparable_data;
+
+template <class It, class Sent = It>
+constexpr void test_iterators() {
+  using ValueT = std::iter_value_t<It>;
+  { // simple test
+    {
+      ValueT a[] = {1, 2, 3, 4};
+
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last(It(a), Sent(It(a + 4)), 2);
+      assert(base(ret.begin()) == a + 1);
+      assert(*ret.begin() == 2);
+    }
+    {
+      ValueT a[] = {1, 2, 3, 4};
+      auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
+
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last(range, 2);
+      assert(base(ret.begin()) == a + 1);
+      assert(*ret.begin() == 2);
+    }
+  }
+
+  { // check that an empty range works
+    {
+      std::array<ValueT, 0> a = {};
+
+      auto ret = std::ranges::find_last(It(a.data()), Sent(It(a.data())), 1).begin();
+      assert(base(ret) == a.data());
+    }
+    {
+      std::array<ValueT, 0> a = {};
+
+      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data())));
+      auto ret   = std::ranges::find_last(range, 1).begin();
+      assert(base(ret) == a.data());
+    }
+  }
+
+  { // check that last is returned with no match
+    {
+      ValueT a[] = {1, 1, 1};
+
+      auto ret = std::ranges::find_last(a, a + 3, 0).begin();
+      assert(ret == a + 3);
+    }
+    {
+      ValueT a[] = {1, 1, 1};
+
+      auto ret = std::ranges::find_last(a, 0).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  if (!std::is_constant_evaluated())
+    comparable_data.clear();
+}
+
+template <class ElementT>
+class TriviallyComparable {
+  ElementT el_;
+
+public:
+  TEST_CONSTEXPR TriviallyComparable(ElementT el) : el_(el) {}
+  bool operator==(const TriviallyComparable&) const = default;
+};
+
+constexpr bool test() {
+  types::for_each(types::type_list<char, wchar_t, int, long, TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
+                  []<class T> {
+                    types::for_each(types::forward_iterator_list<T*>{}, []<class Iter> {
+                      if constexpr (std::forward_iterator<Iter>)
+                        test_iterators<Iter>();
+                      test_iterators<Iter, sentinel_wrapper<Iter>>();
+                      test_iterators<Iter, sized_sentinel<Iter>>();
+                    });
+                  });
+
+  {
+    std::vector<std::vector<int>> vec = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
+    auto view                         = vec | std::views::join;
+    assert(std::ranges::find_last(view.begin(), view.end(), 4).begin() == std::next(view.begin(), 3));
+    assert(std::ranges::find_last(view, 4).begin() == std::next(view.begin(), 3));
+  }
+
+  { // check that the last element is returned
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last(a, 0, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last(a, a + 3, 0, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+  }
+
+  {
+    // check that an iterator is returned with a borrowing range
+    int a[]                                            = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<int*>> auto ret = std::ranges::find_last(std::views::all(a), 1);
+    assert(ret.begin() == a);
+    assert(*ret.begin() == 1);
+  }
+
+  {
+    // count invocations of the projection
+    {
+      int a[]              = {1, 2, 3, 4};
+      int projection_count = 0;
+      auto ret             = std::ranges::find_last(a, a + 4, 2, [&](int i) {
+                   ++projection_count;
+                   return i;
+                 }).begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(projection_count == 3);
+    }
+    {
+      int a[]              = {1, 2, 3, 4};
+      int projection_count = 0;
+      auto ret             = std::ranges::find_last(a, 2, [&](int i) {
+                   ++projection_count;
+                   return i;
+                 }).begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(projection_count == 3);
+    }
+  }
+
+  return true;
+}
+
+template <class IndexT>
+class Comparable {
+  IndexT index_;
+
+public:
+  Comparable(IndexT i)
+      : index_([&]() {
+          IndexT size = static_cast<IndexT>(comparable_data.size());
+          comparable_data.push_back(i);
+          return size;
+        }()) {}
+
+  bool operator==(const Comparable& other) const { return comparable_data[other.index_] == comparable_data[index_]; }
+
+  friend bool operator==(const Comparable& lhs, long long rhs) { return comparable_data[lhs.index_] == rhs; }
+};
+
+void test_deque() {
+  { // empty deque
+    std::deque<int> data;
+    assert(std::ranges::find_last(data, 4).begin() == data.end());
+    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
+  }
+
+  { // single element - match
+    std::deque<int> data = {4};
+    assert(std::ranges::find_last(data, 4).begin() == data.begin());
+    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.begin());
+  }
+
+  { // single element - no match
+    std::deque<int> data = {3};
+    assert(std::ranges::find_last(data, 4).begin() == data.end());
+    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
+  }
+
+  // many elements
+  for (auto size : {2, 3, 1023, 1024, 1025, 2047, 2048, 2049}) {
+    { // last element match
+      std::deque<int> data;
+      data.resize(size);
+      std::fill(data.begin(), data.end(), 3);
+      data[size - 1] = 4;
+      assert(std::ranges::find_last(data, 4).begin() == data.end() - 1);
+      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end() - 1);
+    }
+
+    { // second-last element match
+      std::deque<int> data;
+      data.resize(size);
+      std::fill(data.begin(), data.end(), 3);
+      data[size - 2] = 4;
+      assert(std::ranges::find_last(data, 4).begin() == data.end() - 2);
+      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end() - 2);
+    }
+
+    { // no match
+      std::deque<int> data;
+      data.resize(size);
+      std::fill(data.begin(), data.end(), 3);
+      assert(std::ranges::find_last(data, 4).begin() == data.end());
+      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
+    }
+  }
+}
+
+int main(int, char**) {
+  test_deque();
+  test();
+  static_assert(test());
+
+  types::for_each(types::forward_iterator_list<Comparable<char>*>{}, []<class Iter> {
+    if constexpr (std::forward_iterator<Iter>)
+      test_iterators<Iter>();
+    test_iterators<Iter, sentinel_wrapper<Iter>>();
+    test_iterators<Iter, sized_sentinel<Iter>>();
+  });
+
+  types::for_each(types::forward_iterator_list<Comparable<wchar_t>*>{}, []<class Iter> {
+    if constexpr (std::forward_iterator<Iter>)
+      test_iterators<Iter>();
+    test_iterators<Iter, sentinel_wrapper<Iter>>();
+    test_iterators<Iter, sized_sentinel<Iter>>();
+  });
+
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
new file mode 100644
index 0000000000000..91474de407384
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
@@ -0,0 +1,263 @@
+//===----------------------------------------------------------------------===//
+//
+// 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<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+//          indirect_unary_predicate<projected<I, Proj>> Pred>
+//   constexpr subrange<I> ranges::find_last_if(I first, S last, Pred pred, Proj proj = {});
+// template<forward_range R, class Proj = identity,
+//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+//   constexpr borrowed_subrange_t<R> ranges::find_last_if(R&& r, Pred pred, Proj proj = {});
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+struct Predicate {
+  bool operator()(int);
+};
+
+template <class It, class Sent = It>
+concept HasFindLastIfIt = requires(It it, Sent sent) { std::ranges::find_last_if(it, sent, Predicate{}); };
+static_assert(HasFindLastIfIt<int*>);
+static_assert(HasFindLastIfIt<forward_iterator<int*>>);
+static_assert(!HasFindLastIfIt<cpp20_input_iterator<int*>>);
+static_assert(!HasFindLastIfIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasFindLastIfIt<ForwardIteratorNotIncrementable>);
+static_assert(!HasFindLastIfIt<forward_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasFindLastIfIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+
+static_assert(!HasFindLastIfIt<int*, int>);
+static_assert(!HasFindLastIfIt<int, int*>);
+
+template <class Pred>
+concept HasFindLastIfPred = requires(int* it, Pred pred) { std::ranges::find_last_if(it, it, pred); };
+
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotPredicate>);
+
+template <class R>
+concept HasFindLastIfR = requires(R r) { std::ranges::find_last_if(r, Predicate{}); };
+static_assert(HasFindLastIfR<std::array<int, 0>>);
+static_assert(!HasFindLastIfR<int>);
+static_assert(!HasFindLastIfR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasFindLastIfR<ForwardRangeNotIncrementable>);
+static_assert(!HasFindLastIfR<ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasFindLastIfR<ForwardRangeNotSentinelEqualityComparableWith>);
+
+template <class It, class Sent = It>
+constexpr void test_iterators() {
+  {
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<It>> auto ret =
+        std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 4; });
+    assert(base(ret.begin()) == a + 3);
+    assert(*ret.begin() == 4);
+  }
+  {
+    int a[]    = {1, 2, 3, 4};
+    auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
+
+    std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(range, [](int x) { return x == 4; });
+    assert(base(ret.begin()) == a + 3);
+    assert(*ret.begin() == 4);
+  }
+}
+
+struct NonConstComparable {
+  friend constexpr bool operator==(const NonConstComparable&, const NonConstComparable&) { return false; }
+  friend constexpr bool operator==(NonConstComparable&, NonConstComparable&) { return false; }
+  friend constexpr bool operator==(const NonConstComparable&, NonConstComparable&) { return false; }
+  friend constexpr bool operator==(NonConstComparable&, const NonConstComparable&) { return true; }
+};
+
+constexpr bool test() {
+  test_iterators<int*>();
+  test_iterators<const int*>();
+  test_iterators<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
+  test_iterators<bidirectional_iterator<int*>>();
+  test_iterators<forward_iterator<int*>>();
+  test_iterators<random_access_iterator<int*>>();
+  test_iterators<contiguous_iterator<int*>>();
+
+  {
+    // check that projections are used properly and that they are called with the iterator directly
+    {
+      int a[] = {1, 2, 3, 4};
+      auto ret =
+          std::ranges::find_last_if(a, a + 4, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
+    }
+    {
+      int a[]  = {1, 2, 3, 4};
+      auto ret = std::ranges::find_last_if(a, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  {
+    // check that the last element is returned
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last_if(a, [](int i) { return i == 0; }, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last_if(a, a + 3, [](int i) { return i == 0; }, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+  }
+
+  {
+    // check that end iterator is returned with no match
+    {
+      int a[]  = {1, 1, 1};
+      auto ret = std::ranges::find_last_if(a, a + 3, [](int) { return false; }).begin();
+      assert(ret == a + 3);
+    }
+    {
+      int a[]  = {1, 1, 1};
+      auto ret = std::ranges::find_last_if(a, [](int) { return false; }).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  {
+    // check that ranges::dangling is returned
+    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
+        std::ranges::find_last_if(std::array{1, 2}, [](int) { return false; });
+  }
+
+  {
+    // check that an iterator is returned with a borrowing range
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<int*>> auto ret =
+        std::ranges::find_last_if(std::views::all(a), [](int) { return true; });
+    assert(ret.begin() == a + 3);
+    assert(*ret.begin() == 4);
+  }
+
+  {
+    // check that std::invoke is used
+    struct S {
+      int i;
+    };
+    S a[] = {S{1}, S{3}, S{2}};
+
+    std::same_as<S*> auto ret = std::ranges::find_last_if(a, [](int) { return false; }, &S::i).begin();
+    assert(ret == a + 3);
+  }
+
+  {
+    // count projection and predicate invocation count
+    {
+      int a[]              = {1, 2, 3, 4};
+      int predicate_count  = 0;
+      int projection_count = 0;
+      auto ret =
+          std::ranges::find_last_if(
+              a,
+              a + 4,
+              [&](int i) {
+                ++predicate_count;
+                return i == 2;
+              },
+              [&](int i) {
+                ++projection_count;
+                return i;
+              })
+              .begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(predicate_count == 3);
+      assert(projection_count == 3);
+    }
+    {
+      int a[]              = {1, 2, 3, 4};
+      int predicate_count  = 0;
+      int projection_count = 0;
+      auto ret =
+          std::ranges::find_last_if(
+              a,
+              [&](int i) {
+                ++predicate_count;
+                return i == 2;
+              },
+              [&](int i) {
+                ++projection_count;
+                return i;
+              })
+              .begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(predicate_count == 3);
+      assert(projection_count == 3);
+    }
+  }
+
+  {
+    // check that the return type of `iter::operator*` doesn't change
+    {
+      NonConstComparable a[] = {NonConstComparable{}};
+
+      auto ret = std::ranges::find_last_if(a, a + 1, [](auto&& e) { return e == NonConstComparable{}; }).begin();
+      assert(ret == a);
+    }
+    {
+      NonConstComparable a[] = {NonConstComparable{}};
+
+      auto ret = std::ranges::find_last_if(a, [](auto&& e) { return e == NonConstComparable{}; }).begin();
+      assert(ret == a);
+    }
+  }
+
+  {
+    // check that an empty range works
+    {
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if(a.begin(), a.end(), [](int) { return true; }).begin();
+      assert(ret == a.begin());
+    }
+    {
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if(a, [](int) { return true; }).begin();
+      assert(ret == a.begin());
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
new file mode 100644
index 0000000000000..445f884c710e7
--- /dev/null
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
@@ -0,0 +1,266 @@
+//===----------------------------------------------------------------------===//
+//
+// 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<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+//          indirect_unary_predicate<projected<I, Proj>> Pred>
+//   constexpr subrange<I> ranges::find_last_if_not(I first, S last, Pred pred, Proj proj = {});
+// template<forward_range R, class Proj = identity,
+//          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
+//   constexpr borrowed_subrange_t<R> ranges::find_last_if_not(R&& r, Pred pred, Proj proj = {});
+
+#include <stdio.h>
+
+#include <algorithm>
+#include <array>
+#include <cassert>
+#include <ranges>
+
+#include "almost_satisfies_types.h"
+#include "test_iterators.h"
+
+struct Predicate {
+  bool operator()(int);
+};
+
+template <class It, class Sent = It>
+concept HasFindLastIfNotIt = requires(It it, Sent sent) { std::ranges::find_last_if_not(it, sent, Predicate{}); };
+static_assert(HasFindLastIfNotIt<int*>);
+static_assert(HasFindLastIfNotIt<forward_iterator<int*>>);
+static_assert(!HasFindLastIfNotIt<cpp20_input_iterator<int*>>);
+static_assert(!HasFindLastIfNotIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasFindLastIfNotIt<ForwardIteratorNotIncrementable>);
+static_assert(!HasFindLastIfNotIt<forward_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasFindLastIfNotIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+
+static_assert(!HasFindLastIfNotIt<int*, int>);
+static_assert(!HasFindLastIfNotIt<int, int*>);
+
+template <class Pred>
+concept HasFindLastIfNotPred = requires(int* it, Pred pred) { std::ranges::find_last_if_not(it, it, pred); };
+
+static_assert(!HasFindLastIfNotPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasFindLastIfNotPred<IndirectUnaryPredicateNotPredicate>);
+
+template <class R>
+concept HasFindLastIfNotR = requires(R r) { std::ranges::find_last_if_not(r, Predicate{}); };
+static_assert(HasFindLastIfNotR<std::array<int, 0>>);
+static_assert(!HasFindLastIfNotR<int>);
+static_assert(!HasFindLastIfNotR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasFindLastIfNotR<ForwardRangeNotIncrementable>);
+static_assert(!HasFindLastIfNotR<ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasFindLastIfNotR<ForwardRangeNotSentinelEqualityComparableWith>);
+
+template <class It, class Sent = It>
+constexpr void test_iterators() {
+  {
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<It>> auto ret =
+        std::ranges::find_last_if_not(It(a), Sent(It(a + 4)), [](int x) { return x != 4; });
+    assert(base(ret.begin()) == a + 3);
+    assert(*ret.begin() == 4);
+  }
+  {
+    int a[]    = {1, 2, 3, 4};
+    auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
+    std::same_as<std::ranges::subrange<It>> auto ret =
+        std::ranges::find_last_if_not(range, [](int x) { return x != 4; });
+    assert(base(ret.begin()) == a + 3);
+    assert(*ret.begin() == 4);
+  }
+}
+
+struct NonConstComparableLValue {
+  friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
+  friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
+  friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
+  friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
+};
+
+constexpr bool test() {
+  test_iterators<int*>();
+  test_iterators<const int*>();
+  test_iterators<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
+  test_iterators<bidirectional_iterator<int*>>();
+  test_iterators<forward_iterator<int*>>();
+  test_iterators<random_access_iterator<int*>>();
+  test_iterators<contiguous_iterator<int*>>();
+
+  {
+    // check that projections are used properly and that they are called with the iterator directly
+    {
+      int a[]  = {1, 2, 3, 4};
+      auto ret = std::ranges::find_last_if_not(
+                     a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; })
+                     .begin();
+      assert(ret == a + 3);
+    }
+    {
+      int a[] = {1, 2, 3, 4};
+      auto ret =
+          std::ranges::find_last_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  {
+    // check that the last element is returned
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last_if_not(a, [](int i) { return i != 0; }, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+    {
+      struct S {
+        int comp;
+        int other;
+      };
+      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+      auto ret = std::ranges::find_last_if_not(a, a + 3, [](int i) { return i != 0; }, &S::comp).begin();
+      assert(ret == a + 2);
+      assert(ret->comp == 0);
+      assert(ret->other == 1);
+    }
+  }
+
+  {
+    // check that end iterator is returned with no match
+    {
+      int a[]  = {1, 1, 1};
+      auto ret = std::ranges::find_last_if_not(a, a + 3, [](int) { return true; }).begin();
+      assert(ret == a + 3);
+    }
+    {
+      int a[]  = {1, 1, 1};
+      auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  {
+    // check that ranges::dangling is returned
+    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
+        std::ranges::find_last_if_not(std::array{1, 2}, [](int) { return true; });
+  }
+
+  {
+    // check that an iterator is returned with a borrowing range
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<int*>> auto ret =
+        std::ranges::find_last_if_not(std::views::all(a), [](int) { return false; });
+    assert(ret.begin() == a + 3);
+    assert(*ret.begin() == 4);
+  }
+
+  {
+    // check that std::invoke is used
+    struct S {
+      int i;
+    };
+    S a[] = {S{1}, S{3}, S{2}};
+
+    std::same_as<S*> auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }, &S::i).begin();
+    assert(ret == a + 3);
+  }
+
+  {
+    // count projection and predicate invocation count
+    {
+      int a[]              = {1, 2, 3, 4};
+      int predicate_count  = 0;
+      int projection_count = 0;
+      auto ret =
+          std::ranges::find_last_if_not(
+              a,
+              a + 4,
+              [&](int i) {
+                ++predicate_count;
+                return i != 2;
+              },
+              [&](int i) {
+                ++projection_count;
+                return i;
+              })
+              .begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(predicate_count == 3);
+      assert(projection_count == 3);
+    }
+    {
+      int a[]              = {1, 2, 3, 4};
+      int predicate_count  = 0;
+      int projection_count = 0;
+      auto ret =
+          std::ranges::find_last_if_not(
+              a,
+              [&](int i) {
+                ++predicate_count;
+                return i != 2;
+              },
+              [&](int i) {
+                ++projection_count;
+                return i;
+              })
+              .begin();
+      assert(ret == a + 1);
+      assert(*ret == 2);
+      assert(predicate_count == 3);
+      assert(projection_count == 3);
+    }
+  }
+
+  {
+    // check that the return type of `iter::operator*` doesn't change
+    {
+      NonConstComparableLValue a[] = {NonConstComparableLValue{}};
+      auto ret =
+          std::ranges::find_last_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparableLValue{}; }).begin();
+      assert(ret == a);
+    }
+    {
+      NonConstComparableLValue a[] = {NonConstComparableLValue{}};
+      auto ret = std::ranges::find_last_if_not(a, [](auto&& e) { return e != NonConstComparableLValue{}; }).begin();
+      assert(ret == a);
+    }
+  }
+
+  {
+    // check that an empty range works
+    {
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if_not(a.begin(), a.end(), [](int) { return true; }).begin();
+      assert(ret == a.begin());
+    }
+    {
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }).begin();
+      assert(ret == a.begin());
+    }
+  }
+
+  return true;
+}
+
+int main(int, char**) {
+  test();
+  static_assert(test());
+
+  return 0;
+}
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
index e01a86d9f746f..0624a6c2d49c7 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_dangling.pass.cpp
@@ -116,6 +116,11 @@ constexpr bool test_all() {
   dangling_1st(std::ranges::find, in, x);
   dangling_1st(std::ranges::find_if, in, unary_pred);
   dangling_1st(std::ranges::find_if_not, in, unary_pred);
+#if TEST_STD_VER >= 23
+  dangling_1st(std::ranges::find_last, in, x);
+  dangling_1st(std::ranges::find_last_if, in, unary_pred);
+  dangling_1st(std::ranges::find_last_if_not, in, unary_pred);
+#endif
   dangling_1st(std::ranges::find_first_of, in, in2);
   dangling_1st(std::ranges::adjacent_find, in);
   dangling_1st<mismatch_result<dangling, InIter>>(std::ranges::mismatch, in, in2);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_nonbool.compile.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_nonbool.compile.pass.cpp
index a5668f20ccfd2..5203ee18d7bb0 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_nonbool.compile.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_nonbool.compile.pass.cpp
@@ -53,6 +53,10 @@ void f(Iterator it, Range in, Iterator out, std::size_t n, Value const& val, std
   in_pred(std::ranges::none_of, pred1);
   in_pred(std::ranges::find_if, pred1);
   in_pred(std::ranges::find_if_not, pred1);
+#if TEST_STD_VER >= 23
+  in_pred(std::ranges::find_last_if, pred1);
+  in_pred(std::ranges::find_last_if_not, pred1);
+#endif
   in_in_pred(std::ranges::find_first_of, pred2);
   in_pred(std::ranges::adjacent_find, pred2);
   in_in_pred(std::ranges::mismatch, pred2);
diff --git a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
index 139f1999bc9dc..ca1433b778751 100644
--- a/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
+++ b/libcxx/test/std/algorithms/ranges_robust_against_proxy_iterators.pass.cpp
@@ -82,6 +82,11 @@ constexpr void run_tests() {
   test(std::ranges::find, in, x);
   test(std::ranges::find_if, in, unary_pred);
   test(std::ranges::find_if_not, in, unary_pred);
+#if TEST_STD_VER >= 23
+  test(std::ranges::find_last, in, x);
+  test(std::ranges::find_last_if, in, unary_pred);
+  test(std::ranges::find_last_if_not, in, unary_pred);
+#endif
   test(std::ranges::find_first_of, in, in2);
   test(std::ranges::adjacent_find, in);
   test(std::ranges::mismatch, in, in2);
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
index 8ccd252115ac8..ded8006063241 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/algorithm.version.compile.pass.cpp
@@ -23,6 +23,7 @@
     __cpp_lib_parallel_algorithm                            201603L [C++17]
     __cpp_lib_ranges                                        202207L [C++20]
     __cpp_lib_ranges_contains                               202207L [C++23]
+    __cpp_lib_ranges_find_last                              202207L [C++23]
     __cpp_lib_ranges_starts_ends_with                       202106L [C++23]
     __cpp_lib_robust_nonmodifying_seq_ops                   201304L [C++14]
     __cpp_lib_sample                                        201603L [C++17]
@@ -62,6 +63,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 # endif
@@ -108,6 +113,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 # endif
@@ -169,6 +178,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 # endif
@@ -239,6 +252,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should not be defined before c++23"
 # endif
@@ -315,6 +332,13 @@
 #   error "__cpp_lib_ranges_contains should have the value 202207L in c++23"
 # endif
 
+# ifndef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should be defined in c++23"
+# endif
+# if __cpp_lib_ranges_find_last != 202207L
+#   error "__cpp_lib_ranges_find_last should have the value 202207L in c++23"
+# endif
+
 # ifndef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should be defined in c++23"
 # endif
@@ -412,6 +436,13 @@
 #   error "__cpp_lib_ranges_contains should have the value 202207L in c++26"
 # endif
 
+# ifndef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should be defined in c++26"
+# endif
+# if __cpp_lib_ranges_find_last != 202207L
+#   error "__cpp_lib_ranges_find_last should have the value 202207L in c++26"
+# endif
+
 # ifndef __cpp_lib_ranges_starts_ends_with
 #   error "__cpp_lib_ranges_starts_ends_with should be defined in c++26"
 # endif
diff --git a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
index a01ee702a5172..fb62e57eaf424 100644
--- a/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
+++ b/libcxx/test/std/language.support/support.limits/support.limits.general/version.version.compile.pass.cpp
@@ -173,6 +173,7 @@
     __cpp_lib_ranges_chunk_by                               202202L [C++23]
     __cpp_lib_ranges_concat                                 202403L [C++26]
     __cpp_lib_ranges_contains                               202207L [C++23]
+    __cpp_lib_ranges_find_last                              202207L [C++23]
     __cpp_lib_ranges_iota                                   202202L [C++23]
     __cpp_lib_ranges_join_with                              202202L [C++23]
     __cpp_lib_ranges_repeat                                 202207L [C++23]
@@ -848,6 +849,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_iota
 #   error "__cpp_lib_ranges_iota should not be defined before c++23"
 # endif
@@ -1744,6 +1749,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_iota
 #   error "__cpp_lib_ranges_iota should not be defined before c++23"
 # endif
@@ -2811,6 +2820,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_iota
 #   error "__cpp_lib_ranges_iota should not be defined before c++23"
 # endif
@@ -4145,6 +4158,10 @@
 #   error "__cpp_lib_ranges_contains should not be defined before c++23"
 # endif
 
+# ifdef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should not be defined before c++23"
+# endif
+
 # ifdef __cpp_lib_ranges_iota
 #   error "__cpp_lib_ranges_iota should not be defined before c++23"
 # endif
@@ -5662,6 +5679,13 @@
 #   error "__cpp_lib_ranges_contains should have the value 202207L in c++23"
 # endif
 
+# ifndef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should be defined in c++23"
+# endif
+# if __cpp_lib_ranges_find_last != 202207L
+#   error "__cpp_lib_ranges_find_last should have the value 202207L in c++23"
+# endif
+
 # if !defined(_LIBCPP_VERSION)
 #   ifndef __cpp_lib_ranges_iota
 #     error "__cpp_lib_ranges_iota should be defined in c++23"
@@ -7521,6 +7545,13 @@
 #   error "__cpp_lib_ranges_contains should have the value 202207L in c++26"
 # endif
 
+# ifndef __cpp_lib_ranges_find_last
+#   error "__cpp_lib_ranges_find_last should be defined in c++26"
+# endif
+# if __cpp_lib_ranges_find_last != 202207L
+#   error "__cpp_lib_ranges_find_last should have the value 202207L in c++26"
+# endif
+
 # if !defined(_LIBCPP_VERSION)
 #   ifndef __cpp_lib_ranges_iota
 #     error "__cpp_lib_ranges_iota should be defined in c++26"
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 9506ca1c768bd..b26b397056343 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
@@ -88,6 +88,9 @@ 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::find_last, a, 42));
+static_assert(test(std::ranges::find_last_if, a, odd));
+static_assert(test(std::ranges::find_last_if_not, a, odd));
 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
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 773b1523cde4e..e2eef4e884488 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -1033,6 +1033,11 @@ def add_version_header(tc):
             "values": {"c++23": 202207},
             "headers": ["algorithm"],
         },
+        {
+            "name": "__cpp_lib_ranges_find_last",
+            "values": {"c++23": 202207},
+            "headers": ["algorithm"],
+        },
         {
             "name": "__cpp_lib_ranges_iota",
             "values": {"c++23": 202202},

>From 26fb3285d6380e0f80ec54fdc2f4d84980ea62f0 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Wed, 17 Jul 2024 15:05:55 +0200
Subject: [PATCH 2/9] code review comments

- add `static` to `operator()`
- update release notes
---
 libcxx/docs/ReleaseNotes/19.rst                      | 1 +
 libcxx/docs/Status/RangesAlgorithms.csv              | 1 +
 libcxx/include/__algorithm/ranges_find_last.h        | 4 ++--
 libcxx/include/__algorithm/ranges_find_last_if.h     | 4 ++--
 libcxx/include/__algorithm/ranges_find_last_if_not.h | 4 ++--
 5 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/libcxx/docs/ReleaseNotes/19.rst b/libcxx/docs/ReleaseNotes/19.rst
index e6d8acb74aeb2..03a021023aa5e 100644
--- a/libcxx/docs/ReleaseNotes/19.rst
+++ b/libcxx/docs/ReleaseNotes/19.rst
@@ -55,6 +55,7 @@ Implemented Papers
 - P2231R1 - Missing ``constexpr`` in ``std::optional`` and ``std::variant``
 - P0019R8 - ``std::atomic_ref``
 - P2389R2 - Alias template ``dims`` for the ``extents`` of ``mdspan``
+- P1223R5 - ``find_last``
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index f7a51f732c4b1..adf474711cf32 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -10,6 +10,7 @@ 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, `#65148 <https://github.com/llvm/llvm-project/pull/65148>`_,Complete
+C++23,`find_last <https://wg21.link/p1223r5>`_,Nicole Mazzuca,`#99312 <https://github.com/llvm/llvm-project/pull/99312>`_,Complete
 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
diff --git a/libcxx/include/__algorithm/ranges_find_last.h b/libcxx/include/__algorithm/ranges_find_last.h
index 1ce056d719a09..56bdcde59d003 100644
--- a/libcxx/include/__algorithm/ranges_find_last.h
+++ b/libcxx/include/__algorithm/ranges_find_last.h
@@ -75,14 +75,14 @@ struct __fn {
 
   template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
   operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) const {
     return __find_last_impl(std::move(__first), std::move(__last), __value, __proj);
   }
 
   template <forward_range _Range, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
   operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) const {
     return __find_last_impl(ranges::begin(__range), ranges::end(__range), __value, __proj);
   }
diff --git a/libcxx/include/__algorithm/ranges_find_last_if.h b/libcxx/include/__algorithm/ranges_find_last_if.h
index 2435672a83b63..9c016f3e8d250 100644
--- a/libcxx/include/__algorithm/ranges_find_last_if.h
+++ b/libcxx/include/__algorithm/ranges_find_last_if.h
@@ -77,7 +77,7 @@ struct __fn {
             sentinel_for<_Iter> _Sent,
             class _Proj = identity,
             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
   operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
     return __find_last_if_impl(std::move(__first), std::move(__last), __pred, __proj);
   }
@@ -85,7 +85,7 @@ struct __fn {
   template <forward_range _Range,
             class _Proj = identity,
             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
     return __find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
   }
diff --git a/libcxx/include/__algorithm/ranges_find_last_if_not.h b/libcxx/include/__algorithm/ranges_find_last_if_not.h
index 4b246c96695c2..5102b33709cd0 100644
--- a/libcxx/include/__algorithm/ranges_find_last_if_not.h
+++ b/libcxx/include/__algorithm/ranges_find_last_if_not.h
@@ -39,7 +39,7 @@ struct __fn {
             sentinel_for<_Iter> _Sent,
             class _Proj = identity,
             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
   operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
     auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
     return ranges::__find_last_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
@@ -48,7 +48,7 @@ struct __fn {
   template <forward_range _Range,
             class _Proj = identity,
             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr borrowed_subrange_t<_Range>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
   operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
     auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
     return ranges::__find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred2, __proj);

>From c07e7f80f66c4d29ee7ef39abf6007b9d24828dc Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Wed, 17 Jul 2024 15:28:51 +0200
Subject: [PATCH 3/9] attempt to fix it?

---
 libcxx/docs/Status/RangesAlgorithms.csv |  7 +++----
 libcxx/include/algorithm                | 28 ++++++++++++-------------
 2 files changed, 17 insertions(+), 18 deletions(-)

diff --git a/libcxx/docs/Status/RangesAlgorithms.csv b/libcxx/docs/Status/RangesAlgorithms.csv
index adf474711cf32..469ea21a76aab 100644
--- a/libcxx/docs/Status/RangesAlgorithms.csv
+++ b/libcxx/docs/Status/RangesAlgorithms.csv
@@ -1,8 +1,8 @@
 Standard,Algorithm,Assignee,CL,Status
 C++20,all C++20 algorithms,N/A,N/A,✅
-C++23,`find_last <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
-C++23,`find_last_if <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
-C++23,`find_last_if_not <https://wg21.link/P1223R5>`_,Unassigned,No patch yet,Not started
+C++23,`find_last <https://wg21.link/P1223R5>`_,Nicole Mazzuca,`#99312 <https://github.com/llvm/llvm-project/pull/99312>`_,Complete
+C++23,`find_last_if <https://wg21.link/P1223R5>`_,Nicole Mazzuca,`#99312 <https://github.com/llvm/llvm-project/pull/99312>`_,Complete
+C++23,`find_last_if_not <https://wg21.link/P1223R5>`_,Nicole Mazzuca,`#99312 <https://github.com/llvm/llvm-project/pull/99312>`_,Complete
 C++23,`starts_with <https://wg21.link/P1659R3>`_,Zijun Zhao,`D150735 <https://llvm.org/D150735>`_,Complete
 C++23,`ends_with <https://wg21.link/P1659R3>`_,Zijun Zhao, `D150831 <https://llvm.org/D150831>`_,Complete
 C++23,`shift_left <https://wg21.link/p2440r1>`_,Unassigned,No patch yet,Not started
@@ -10,7 +10,6 @@ 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, `#65148 <https://github.com/llvm/llvm-project/pull/65148>`_,Complete
-C++23,`find_last <https://wg21.link/p1223r5>`_,Nicole Mazzuca,`#99312 <https://github.com/llvm/llvm-project/pull/99312>`_,Complete
 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
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index f36e396246a1d..9ad056442d8ec 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -101,26 +101,26 @@ namespace ranges {
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
     constexpr borrowed_iterator_t<R>
       find_if_not(R&& r, Pred pred, Proj proj = {});                                                      // since C++20
-																																																					//
+
   template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
-    constexpr subrange<I> find_last(I first, S last, const T& value, Proj proj = {});
-	template<forward_range R, class T, class Proj = identity>
-		requires
-			indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
-		constexpr borrowed_subrange_t<R> find_last(R&& r, const T& value, Proj proj = {});
-	template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+    constexpr subrange<I> find_last(I first, S last, const T& value, Proj proj = {});                     // since C++23
+  template<forward_range R, class T, class Proj = identity>
+    requires
+      indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
+    constexpr borrowed_subrange_t<R> find_last(R&& r, const T& value, Proj proj = {});                   // since C++23
+  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
-		constexpr subrange<I> find_last_if(I first, S last, Pred pred, Proj proj = {});
-	template<forward_range R, class Proj = identity,
+    constexpr subrange<I> find_last_if(I first, S last, Pred pred, Proj proj = {});                      // since C++23
+  template<forward_range R, class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
-		constexpr borrowed_subrange_t<R> find_last_if(R&& r, Pred pred, Proj proj = {});
-	template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
+    constexpr borrowed_subrange_t<R> find_last_if(R&& r, Pred pred, Proj proj = {});                     // since C++23
+  template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
-		constexpr subrange<I> find_last_if_not(I first, S last, Pred pred, Proj proj = {});
-	template<forward_range R, class Proj = identity,
+    constexpr subrange<I> find_last_if_not(I first, S last, Pred pred, Proj proj = {});                  // since C++23
+  template<forward_range R, class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
-		constexpr borrowed_subrange_t<R> find_last_if_not(R&& r, Pred pred, Proj proj = {});
+    constexpr borrowed_subrange_t<R> find_last_if_not(R&& r, Pred pred, Proj proj = {});                 // since C++23
 
   template<class T, class Proj = identity,
            indirect_strict_weak_order<projected<const T*, Proj>> Comp = ranges::less>

>From f7b799ddc91b343764e7f2e8fd70b41841fc7402 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Wed, 17 Jul 2024 16:50:21 +0200
Subject: [PATCH 4/9] fixy fixy

---
 libcxx/include/__algorithm/ranges_find_last.h        | 4 ++--
 libcxx/include/__algorithm/ranges_find_last_if.h     | 4 ++--
 libcxx/include/__algorithm/ranges_find_last_if_not.h | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/libcxx/include/__algorithm/ranges_find_last.h b/libcxx/include/__algorithm/ranges_find_last.h
index 56bdcde59d003..5e54da3e7060b 100644
--- a/libcxx/include/__algorithm/ranges_find_last.h
+++ b/libcxx/include/__algorithm/ranges_find_last.h
@@ -76,14 +76,14 @@ struct __fn {
   template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) const {
+  operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) {
     return __find_last_impl(std::move(__first), std::move(__last), __value, __proj);
   }
 
   template <forward_range _Range, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
-  operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) const {
+  operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) {
     return __find_last_impl(ranges::begin(__range), ranges::end(__range), __value, __proj);
   }
 };
diff --git a/libcxx/include/__algorithm/ranges_find_last_if.h b/libcxx/include/__algorithm/ranges_find_last_if.h
index 9c016f3e8d250..84fea65c97f73 100644
--- a/libcxx/include/__algorithm/ranges_find_last_if.h
+++ b/libcxx/include/__algorithm/ranges_find_last_if.h
@@ -78,7 +78,7 @@ struct __fn {
             class _Proj = identity,
             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
     return __find_last_if_impl(std::move(__first), std::move(__last), __pred, __proj);
   }
 
@@ -86,7 +86,7 @@ struct __fn {
             class _Proj = identity,
             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
-  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
     return __find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
   }
 };
diff --git a/libcxx/include/__algorithm/ranges_find_last_if_not.h b/libcxx/include/__algorithm/ranges_find_last_if_not.h
index 5102b33709cd0..87bab319a0c0f 100644
--- a/libcxx/include/__algorithm/ranges_find_last_if_not.h
+++ b/libcxx/include/__algorithm/ranges_find_last_if_not.h
@@ -40,7 +40,7 @@ struct __fn {
             class _Proj = identity,
             indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) const {
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
     auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
     return ranges::__find_last_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
   }
@@ -49,7 +49,7 @@ struct __fn {
             class _Proj = identity,
             indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
-  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) const {
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
     auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
     return ranges::__find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred2, __proj);
   }

>From ec67129d036864ee25f07fe9fce98e7d6bfdb013 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Thu, 18 Jul 2024 15:59:51 +0200
Subject: [PATCH 5/9] (wip) merge ranges_find_last*.h into ranges_find_last.h

Additionally, make __find_last_impl into one function for all three

Finally, change the tests for find_last to see if people are good with that version
(hopefully, yes, in which case I'll do the rest)
---
 libcxx/include/CMakeLists.txt                 |   2 -
 libcxx/include/__algorithm/ranges_find_if.h   |   2 -
 libcxx/include/__algorithm/ranges_find_last.h | 140 +++++++++----
 .../include/__algorithm/ranges_find_last_if.h | 106 ----------
 .../__algorithm/ranges_find_last_if_not.h     |  70 -------
 libcxx/include/algorithm                      |   2 -
 libcxx/include/module.modulemap               |   2 -
 .../alg.find.last/ranges.find_last.pass.cpp   | 187 +++++-------------
 8 files changed, 155 insertions(+), 356 deletions(-)
 delete mode 100644 libcxx/include/__algorithm/ranges_find_last_if.h
 delete mode 100644 libcxx/include/__algorithm/ranges_find_last_if_not.h

diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index 824806d562d23..5f45127f691b3 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -98,8 +98,6 @@ set(files
   __algorithm/ranges_find_if.h
   __algorithm/ranges_find_if_not.h
   __algorithm/ranges_find_last.h
-  __algorithm/ranges_find_last_if.h
-  __algorithm/ranges_find_last_if_not.h
   __algorithm/ranges_for_each.h
   __algorithm/ranges_for_each_n.h
   __algorithm/ranges_generate.h
diff --git a/libcxx/include/__algorithm/ranges_find_if.h b/libcxx/include/__algorithm/ranges_find_if.h
index fd2c090af2d6d..888f9ec3cb2d5 100644
--- a/libcxx/include/__algorithm/ranges_find_if.h
+++ b/libcxx/include/__algorithm/ranges_find_if.h
@@ -14,8 +14,6 @@
 #include <__functional/invoke.h>
 #include <__functional/ranges_operations.h>
 #include <__iterator/concepts.h>
-#include <__iterator/next.h>
-#include <__iterator/prev.h>
 #include <__iterator/projected.h>
 #include <__ranges/access.h>
 #include <__ranges/concepts.h>
diff --git a/libcxx/include/__algorithm/ranges_find_last.h b/libcxx/include/__algorithm/ranges_find_last.h
index 5e54da3e7060b..e6ca74349081b 100644
--- a/libcxx/include/__algorithm/ranges_find_last.h
+++ b/libcxx/include/__algorithm/ranges_find_last.h
@@ -35,62 +35,134 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 namespace ranges {
-namespace __find_last {
-struct __fn {
-  template <class _Iter, class _Sent, class _Type, class _Proj>
-  _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  __find_last_impl(_Iter __first, _Sent __last, const _Type& __value, _Proj& __proj) {
-    if (__first == __last) {
-      return subrange<_Iter>(__first, __first);
-    }
 
-    if constexpr (bidirectional_iterator<_Iter>) {
-      auto __last_it = ranges::next(__first, __last);
-      for (auto __it = ranges::prev(__last_it); __it != __first; --__it) {
-        if (std::invoke(__proj, *__it) == __value) {
-          return subrange<_Iter>(std::move(__it), std::move(__last_it));
-        }
-      }
-      if (std::invoke(__proj, *__first) == __value) {
-        return subrange<_Iter>(std::move(__first), std::move(__last_it));
+template <class _Iter, class _Sent, class _Pred, class _Proj>
+_LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+__find_last_impl(_Iter __first, _Sent __last, _Pred __pred, _Proj& __proj) {
+  if (__first == __last) {
+    return subrange<_Iter>(__first, __first);
+  }
+
+  if constexpr (bidirectional_iterator<_Iter>) {
+    auto __last_it = ranges::next(__first, __last);
+    for (auto __it = ranges::prev(__last_it); __it != __first; --__it) {
+      if (__pred(std::invoke(__proj, *__it))) {
+        return subrange<_Iter>(std::move(__it), std::move(__last_it));
       }
-      return subrange<_Iter>(__last_it, __last_it);
-    } else {
-      bool __found = false;
-      _Iter __found_it;
-      for (; __first != __last; ++__first) {
-        if (std::invoke(__proj, *__first) == __value) {
-          __found    = true;
-          __found_it = __first;
-        }
+    }
+    if (__pred(std::invoke(__proj, *__first))) {
+      return subrange<_Iter>(std::move(__first), std::move(__last_it));
+    }
+    return subrange<_Iter>(__last_it, __last_it);
+  } else {
+    bool __found = false;
+    _Iter __found_it;
+    for (; __first != __last; ++__first) {
+      if (__pred(std::invoke(__proj, *__first))) {
+        __found    = true;
+        __found_it = __first;
       }
+    }
 
-      if (__found) {
-        return subrange<_Iter>(std::move(__found_it), std::move(__first));
-      } else {
-        return subrange<_Iter>(__first, __first);
-      }
+    if (__found) {
+      return subrange<_Iter>(std::move(__found_it), std::move(__first));
+    } else {
+      return subrange<_Iter>(__first, __first);
     }
   }
+}
+
+namespace __find_last {
+struct __fn {
+  template <class _Type>
+  struct __op {
+    const _Type& __value;
+    template <class _Elem>
+    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+      return std::forward<_Elem>(__elem) == __value;
+    }
+  };
 
   template <forward_iterator _Iter, sentinel_for<_Iter> _Sent, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<_Iter, _Proj>, const _Type*>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
   operator()(_Iter __first, _Sent __last, const _Type& __value, _Proj __proj = {}) {
-    return __find_last_impl(std::move(__first), std::move(__last), __value, __proj);
+    return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Type>{__value}, __proj);
   }
 
   template <forward_range _Range, class _Type, class _Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<iterator_t<_Range>, _Proj>, const _Type*>
   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
   operator()(_Range&& __range, const _Type& __value, _Proj __proj = {}) {
-    return __find_last_impl(ranges::begin(__range), ranges::end(__range), __value, __proj);
+    return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Type>{__value}, __proj);
   }
 };
 } // namespace __find_last
 
+namespace __find_last_if {
+struct __fn {
+  template <class _Pred>
+  struct __op {
+    _Pred& __pred;
+    template <class _Elem>
+    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+      return std::invoke(__pred, std::forward<_Elem>(__elem));
+    }
+  };
+
+  template <forward_iterator _Iter,
+            sentinel_for<_Iter> _Sent,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
+    return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Pred>{__pred}, __proj);
+  }
+
+  template <forward_range _Range,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
+    return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Pred>{__pred}, __proj);
+  }
+};
+} // namespace __find_last_if
+
+namespace __find_last_if_not {
+struct __fn {
+  template <class _Pred>
+  struct __op {
+    _Pred& __pred;
+    template <class _Elem>
+    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+      return !std::invoke(__pred, std::forward<_Elem>(__elem));
+    }
+  };
+
+  template <forward_iterator _Iter,
+            sentinel_for<_Iter> _Sent,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
+    return ranges::__find_last_impl(std::move(__first), std::move(__last), __op<_Pred>{__pred}, __proj);
+  }
+
+  template <forward_range _Range,
+            class _Proj = identity,
+            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
+  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
+  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
+    return ranges::__find_last_impl(ranges::begin(__range), ranges::end(__range), __op<_Pred>{__pred}, __proj);
+  }
+};
+} // namespace __find_last_if_not
+
 inline namespace __cpo {
-inline constexpr auto find_last = __find_last::__fn{};
+inline constexpr auto find_last        = __find_last::__fn{};
+inline constexpr auto find_last_if     = __find_last_if::__fn{};
+inline constexpr auto find_last_if_not = __find_last_if_not::__fn{};
 } // namespace __cpo
 } // namespace ranges
 
diff --git a/libcxx/include/__algorithm/ranges_find_last_if.h b/libcxx/include/__algorithm/ranges_find_last_if.h
deleted file mode 100644
index 84fea65c97f73..0000000000000
--- a/libcxx/include/__algorithm/ranges_find_last_if.h
+++ /dev/null
@@ -1,106 +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 _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H
-#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H
-
-#include <__config>
-#include <__functional/identity.h>
-#include <__functional/invoke.h>
-#include <__functional/ranges_operations.h>
-#include <__iterator/concepts.h>
-#include <__iterator/next.h>
-#include <__iterator/prev.h>
-#include <__iterator/projected.h>
-#include <__ranges/access.h>
-#include <__ranges/concepts.h>
-#include <__ranges/subrange.h>
-#include <__utility/move.h>
-
-#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-#  pragma GCC system_header
-#endif
-
-_LIBCPP_PUSH_MACROS
-#include <__undef_macros>
-
-#if _LIBCPP_STD_VER >= 23
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-namespace ranges {
-
-template <class _Iter, class _Sent, class _Pred, class _Proj>
-_LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-__find_last_if_impl(_Iter __first, _Sent __last, _Pred& __pred, _Proj& __proj) {
-  if (__first == __last) {
-    return subrange<_Iter>(__first, __first);
-  }
-
-  if constexpr (bidirectional_iterator<_Iter>) {
-    auto __last_it = ranges::next(__first, __last);
-    for (auto __it = ranges::prev(__last_it); __it != __first; --__it) {
-      if (std::invoke(__pred, std::invoke(__proj, *__it))) {
-        return subrange<_Iter>(std::move(__it), std::move(__last_it));
-      }
-    }
-    if (std::invoke(__pred, std::invoke(__proj, *__first))) {
-      return subrange<_Iter>(std::move(__first), std::move(__last_it));
-    }
-    return subrange<_Iter>(__last_it, __last_it);
-  } else {
-    bool __found = false;
-    _Iter __found_it;
-    for (; __first != __last; ++__first) {
-      if (std::invoke(__pred, std::invoke(__proj, *__first))) {
-        __found    = true;
-        __found_it = __first;
-      }
-    }
-
-    if (__found) {
-      return subrange<_Iter>(std::move(__found_it), std::move(__first));
-    } else {
-      return subrange<_Iter>(__first, __first);
-    }
-  }
-}
-
-namespace __find_last_if {
-struct __fn {
-  template <forward_iterator _Iter,
-            sentinel_for<_Iter> _Sent,
-            class _Proj = identity,
-            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
-    return __find_last_if_impl(std::move(__first), std::move(__last), __pred, __proj);
-  }
-
-  template <forward_range _Range,
-            class _Proj = identity,
-            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
-  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
-    return __find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred, __proj);
-  }
-};
-} // namespace __find_last_if
-
-inline namespace __cpo {
-inline constexpr auto find_last_if = __find_last_if::__fn{};
-} // namespace __cpo
-} // namespace ranges
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP_STD_VER >= 23
-
-_LIBCPP_POP_MACROS
-
-#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_H
diff --git a/libcxx/include/__algorithm/ranges_find_last_if_not.h b/libcxx/include/__algorithm/ranges_find_last_if_not.h
deleted file mode 100644
index 87bab319a0c0f..0000000000000
--- a/libcxx/include/__algorithm/ranges_find_last_if_not.h
+++ /dev/null
@@ -1,70 +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 _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_NOT_H
-#define _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_NOT_H
-
-#include <__algorithm/ranges_find_last_if.h>
-#include <__config>
-#include <__functional/identity.h>
-#include <__functional/invoke.h>
-#include <__functional/ranges_operations.h>
-#include <__iterator/concepts.h>
-#include <__iterator/projected.h>
-#include <__ranges/access.h>
-#include <__ranges/concepts.h>
-#include <__ranges/subrange.h>
-#include <__utility/move.h>
-
-#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
-#  pragma GCC system_header
-#endif
-
-_LIBCPP_PUSH_MACROS
-#include <__undef_macros>
-
-#if _LIBCPP_STD_VER >= 23
-
-_LIBCPP_BEGIN_NAMESPACE_STD
-
-namespace ranges {
-namespace __find_last_if_not {
-struct __fn {
-  template <forward_iterator _Iter,
-            sentinel_for<_Iter> _Sent,
-            class _Proj = identity,
-            indirect_unary_predicate<projected<_Iter, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
-  operator()(_Iter __first, _Sent __last, _Pred __pred, _Proj __proj = {}) {
-    auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
-    return ranges::__find_last_if_impl(std::move(__first), std::move(__last), __pred2, __proj);
-  }
-
-  template <forward_range _Range,
-            class _Proj = identity,
-            indirect_unary_predicate<projected<iterator_t<_Range>, _Proj>> _Pred>
-  [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr static borrowed_subrange_t<_Range>
-  operator()(_Range&& __range, _Pred __pred, _Proj __proj = {}) {
-    auto __pred2 = [&__pred](auto&& __e) -> bool { return !std::invoke(__pred, std::forward<decltype(__e)>(__e)); };
-    return ranges::__find_last_if_impl(ranges::begin(__range), ranges::end(__range), __pred2, __proj);
-  }
-};
-} // namespace __find_last_if_not
-
-inline namespace __cpo {
-inline constexpr auto find_last_if_not = __find_last_if_not::__fn{};
-} // namespace __cpo
-} // namespace ranges
-
-_LIBCPP_END_NAMESPACE_STD
-
-#endif // _LIBCPP_STD_VER >= 23
-
-_LIBCPP_POP_MACROS
-
-#endif // _LIBCPP___ALGORITHM_RANGES_FIND_LAST_IF_NOT_H
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 9ad056442d8ec..e7f7ee00591b2 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -2010,8 +2010,6 @@ template <class BidirectionalIterator, class Compare>
 #  include <__algorithm/ranges_contains_subrange.h>
 #  include <__algorithm/ranges_ends_with.h>
 #  include <__algorithm/ranges_find_last.h>
-#  include <__algorithm/ranges_find_last_if.h>
-#  include <__algorithm/ranges_find_last_if_not.h>
 #  include <__algorithm/ranges_starts_with.h>
 #endif // _LIBCPP_STD_VER >= 23
 
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index cbe7d03ebc0a2..590c840dd2778 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -765,8 +765,6 @@ module std_private_algorithm_ranges_find_first_of                        [system
 module std_private_algorithm_ranges_find_if                              [system] { header "__algorithm/ranges_find_if.h" }
 module std_private_algorithm_ranges_find_if_not                          [system] { header "__algorithm/ranges_find_if_not.h" }
 module std_private_algorithm_ranges_find_last                            [system] { header "__algorithm/ranges_find_last.h" }
-module std_private_algorithm_ranges_find_last_if                         [system] { header "__algorithm/ranges_find_last_if.h" }
-module std_private_algorithm_ranges_find_last_if_not                     [system] { header "__algorithm/ranges_find_last_if_not.h" }
 module std_private_algorithm_ranges_for_each                             [system] {
   header "__algorithm/ranges_for_each.h"
   export std_private_algorithm_in_fun_result
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
index fe5fd35aeb1bd..52e6eb6b4c7e7 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
@@ -58,8 +58,6 @@ static_assert(!HasFindLastR<ForwardRangeNotIncrementable, int>);
 static_assert(!HasFindLastR<ForwardRangeNotSentinelSemiregular, int>);
 static_assert(!HasFindLastR<ForwardRangeNotSentinelEqualityComparableWith, int>);
 
-static std::vector<int> comparable_data;
-
 template <class It, class Sent = It>
 constexpr void test_iterators() {
   using ValueT = std::iter_value_t<It>;
@@ -111,9 +109,43 @@ constexpr void test_iterators() {
       assert(ret == a + 3);
     }
   }
+}
 
-  if (!std::is_constant_evaluated())
-    comparable_data.clear();
+template <template <class> class IteratorT>
+constexpr void test_iterator_classes() {
+  { // check that the last element is returned
+    struct S {
+      int comp;
+      int other;
+    };
+    using it = IteratorT<S*>;
+    S a[]    = {{0, 0}, {0, 2}, {0, 1}};
+
+    auto ret = std::ranges::find_last(it(std::begin(a)), it(std::end(a)), 0, &S::comp).begin();
+    assert(ret == it(a + 2));
+    assert((*ret).comp == 0);
+    assert((*ret).other == 1);
+  }
+
+  {
+    // count invocations of the projection
+    using it = IteratorT<int*>;
+
+    int a[]              = {1, 2, 3, 4};
+    int projection_count = 0;
+
+    auto ret = std::ranges::find_last(it(std::begin(a)), it(std::end(a)), 2, [&](int i) {
+                 ++projection_count;
+                 return i;
+               }).begin();
+    assert(ret == it(a + 1));
+    assert(*ret == 2);
+    if (std::bidirectional_iterator<it>) {
+      assert(projection_count == 3);
+    } else {
+      assert(projection_count == 4); // must go through entire list
+    }
+  }
 }
 
 template <class ElementT>
@@ -129,166 +161,45 @@ constexpr bool test() {
   types::for_each(types::type_list<char, wchar_t, int, long, TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
                   []<class T> {
                     types::for_each(types::forward_iterator_list<T*>{}, []<class Iter> {
-                      if constexpr (std::forward_iterator<Iter>)
-                        test_iterators<Iter>();
+                      test_iterators<Iter>();
                       test_iterators<Iter, sentinel_wrapper<Iter>>();
                       test_iterators<Iter, sized_sentinel<Iter>>();
                     });
                   });
 
+  test_iterator_classes<forward_iterator>();
+  test_iterator_classes<bidirectional_iterator>();
+  test_iterator_classes<random_access_iterator>();
+  test_iterator_classes<std::type_identity_t>();
+
   {
     std::vector<std::vector<int>> vec = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
-    auto view                         = vec | std::views::join;
+
+    auto view = vec | std::views::join;
     assert(std::ranges::find_last(view.begin(), view.end(), 4).begin() == std::next(view.begin(), 3));
     assert(std::ranges::find_last(view, 4).begin() == std::next(view.begin(), 3));
   }
 
-  { // check that the last element is returned
-    {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last(a, 0, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
-    }
-    {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last(a, a + 3, 0, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
-    }
-  }
-
   {
     // check that an iterator is returned with a borrowing range
-    int a[]                                            = {1, 2, 3, 4};
+    int a[] = {1, 2, 3, 4};
+
     std::same_as<std::ranges::subrange<int*>> auto ret = std::ranges::find_last(std::views::all(a), 1);
     assert(ret.begin() == a);
     assert(*ret.begin() == 1);
   }
 
   {
-    // count invocations of the projection
-    {
-      int a[]              = {1, 2, 3, 4};
-      int projection_count = 0;
-      auto ret             = std::ranges::find_last(a, a + 4, 2, [&](int i) {
-                   ++projection_count;
-                   return i;
-                 }).begin();
-      assert(ret == a + 1);
-      assert(*ret == 2);
-      assert(projection_count == 3);
-    }
-    {
-      int a[]              = {1, 2, 3, 4};
-      int projection_count = 0;
-      auto ret             = std::ranges::find_last(a, 2, [&](int i) {
-                   ++projection_count;
-                   return i;
-                 }).begin();
-      assert(ret == a + 1);
-      assert(*ret == 2);
-      assert(projection_count == 3);
-    }
+    // check that dangling ranges are dangling
+    std::same_as<std::ranges::dangling> auto ret = std::ranges::find_last(std::vector<int>(), 0);
+    (void)ret;
   }
 
   return true;
 }
 
-template <class IndexT>
-class Comparable {
-  IndexT index_;
-
-public:
-  Comparable(IndexT i)
-      : index_([&]() {
-          IndexT size = static_cast<IndexT>(comparable_data.size());
-          comparable_data.push_back(i);
-          return size;
-        }()) {}
-
-  bool operator==(const Comparable& other) const { return comparable_data[other.index_] == comparable_data[index_]; }
-
-  friend bool operator==(const Comparable& lhs, long long rhs) { return comparable_data[lhs.index_] == rhs; }
-};
-
-void test_deque() {
-  { // empty deque
-    std::deque<int> data;
-    assert(std::ranges::find_last(data, 4).begin() == data.end());
-    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
-  }
-
-  { // single element - match
-    std::deque<int> data = {4};
-    assert(std::ranges::find_last(data, 4).begin() == data.begin());
-    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.begin());
-  }
-
-  { // single element - no match
-    std::deque<int> data = {3};
-    assert(std::ranges::find_last(data, 4).begin() == data.end());
-    assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
-  }
-
-  // many elements
-  for (auto size : {2, 3, 1023, 1024, 1025, 2047, 2048, 2049}) {
-    { // last element match
-      std::deque<int> data;
-      data.resize(size);
-      std::fill(data.begin(), data.end(), 3);
-      data[size - 1] = 4;
-      assert(std::ranges::find_last(data, 4).begin() == data.end() - 1);
-      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end() - 1);
-    }
-
-    { // second-last element match
-      std::deque<int> data;
-      data.resize(size);
-      std::fill(data.begin(), data.end(), 3);
-      data[size - 2] = 4;
-      assert(std::ranges::find_last(data, 4).begin() == data.end() - 2);
-      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end() - 2);
-    }
-
-    { // no match
-      std::deque<int> data;
-      data.resize(size);
-      std::fill(data.begin(), data.end(), 3);
-      assert(std::ranges::find_last(data, 4).begin() == data.end());
-      assert(std::ranges::find_last(data.begin(), data.end(), 4).begin() == data.end());
-    }
-  }
-}
-
 int main(int, char**) {
-  test_deque();
   test();
   static_assert(test());
-
-  types::for_each(types::forward_iterator_list<Comparable<char>*>{}, []<class Iter> {
-    if constexpr (std::forward_iterator<Iter>)
-      test_iterators<Iter>();
-    test_iterators<Iter, sentinel_wrapper<Iter>>();
-    test_iterators<Iter, sized_sentinel<Iter>>();
-  });
-
-  types::for_each(types::forward_iterator_list<Comparable<wchar_t>*>{}, []<class Iter> {
-    if constexpr (std::forward_iterator<Iter>)
-      test_iterators<Iter>();
-    test_iterators<Iter, sentinel_wrapper<Iter>>();
-    test_iterators<Iter, sized_sentinel<Iter>>();
-  });
-
   return 0;
 }

>From 8b9e401bae9b53909d6901d2b2d3de9ae01ef7e7 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Thu, 18 Jul 2024 19:01:46 +0200
Subject: [PATCH 6/9] fix tests

---
 libcxx/modules/std/algorithm.inc               |  2 ++
 ...robust_against_copying_projections.pass.cpp | 18 ++++++++++++------
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/libcxx/modules/std/algorithm.inc b/libcxx/modules/std/algorithm.inc
index 6c7a30330008a..3c2139cd64ee4 100644
--- a/libcxx/modules/std/algorithm.inc
+++ b/libcxx/modules/std/algorithm.inc
@@ -77,12 +77,14 @@ export namespace std {
     using std::ranges::find_if_not;
   } // namespace ranges
 
+#if _LIBCPP_STD_VER >= 23
   // [alg.find.last], find last
   namespace ranges {
     using std::ranges::find_last;
     using std::ranges::find_last_if;
     using std::ranges::find_last_if_not;
   } // namespace ranges
+#endif
 
   // [alg.find.end], find end
   using std::find_end;
diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
index 95f0893b1440b..bc71150c9e2d8 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_projections.pass.cpp
@@ -116,12 +116,18 @@ constexpr bool all_the_algorithms()
     (void)std::ranges::find_if_not(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
 #if TEST_STD_VER >= 23
-    (void)std::ranges::find_last(first, last, value, Proj(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last(a, value, Proj(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(), Proj(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if_not(a, UnaryTrue(), Proj(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last(first, last, value, Proj(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last(a, value, Proj(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if(first, last, UnaryTrue(), Proj(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if(a, UnaryTrue(), Proj(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(), Proj(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if_not(a, UnaryTrue(), Proj(&copies));
+    assert(copies == 0);
 #endif
     (void)std::ranges::for_each(first, last, UnaryVoid(), Proj(&copies)); assert(copies == 0);
     (void)std::ranges::for_each(a, UnaryVoid(), Proj(&copies)); assert(copies == 0);

>From 86a52f5dc078e1a98889b963668a34ec39866f1e Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Thu, 18 Jul 2024 20:58:50 +0200
Subject: [PATCH 7/9] finish off the tests

---
 .../alg.find.last/ranges.find_last.pass.cpp   |  23 +-
 .../ranges.find_last_if.pass.cpp              | 251 +++++++--------
 .../ranges.find_last_if_not.pass.cpp          | 304 +++++++++---------
 3 files changed, 284 insertions(+), 294 deletions(-)

diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
index 52e6eb6b4c7e7..499528fab2cbc 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
@@ -60,7 +60,10 @@ static_assert(!HasFindLastR<ForwardRangeNotSentinelEqualityComparableWith, int>)
 
 template <class It, class Sent = It>
 constexpr void test_iterators() {
-  using ValueT = std::iter_value_t<It>;
+  using ValueT    = std::iter_value_t<It>;
+  auto make_range = [](auto& a) {
+    return std::ranges::subrange(It(std::ranges::begin(a)), Sent(It(std::ranges::end(a))));
+  };
   { // simple test
     {
       ValueT a[] = {1, 2, 3, 4};
@@ -71,9 +74,8 @@ constexpr void test_iterators() {
     }
     {
       ValueT a[] = {1, 2, 3, 4};
-      auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
 
-      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last(range, 2);
+      std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last(make_range(a), 2);
       assert(base(ret.begin()) == a + 1);
       assert(*ret.begin() == 2);
     }
@@ -84,14 +86,13 @@ constexpr void test_iterators() {
       std::array<ValueT, 0> a = {};
 
       auto ret = std::ranges::find_last(It(a.data()), Sent(It(a.data())), 1).begin();
-      assert(base(ret) == a.data());
+      assert(ret == It(a.data()));
     }
     {
       std::array<ValueT, 0> a = {};
 
-      auto range = std::ranges::subrange(It(a.data()), Sent(It(a.data())));
-      auto ret   = std::ranges::find_last(range, 1).begin();
-      assert(base(ret) == a.data());
+      auto ret = std::ranges::find_last(make_range(a), 1).begin();
+      assert(ret == It(a.begin()));
     }
   }
 
@@ -99,14 +100,14 @@ constexpr void test_iterators() {
     {
       ValueT a[] = {1, 1, 1};
 
-      auto ret = std::ranges::find_last(a, a + 3, 0).begin();
-      assert(ret == a + 3);
+      auto ret = std::ranges::find_last(It(a), Sent(It(a + 3)), 0).begin();
+      assert(ret == It(a + 3));
     }
     {
       ValueT a[] = {1, 1, 1};
 
-      auto ret = std::ranges::find_last(a, 0).begin();
-      assert(ret == a + 3);
+      auto ret = std::ranges::find_last(make_range(a), 0).begin();
+      assert(ret == It(a + 3));
     }
   }
 }
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
index 91474de407384..0ce3cae693038 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
@@ -57,120 +57,86 @@ static_assert(!HasFindLastIfR<ForwardRangeNotIncrementable>);
 static_assert(!HasFindLastIfR<ForwardRangeNotSentinelSemiregular>);
 static_assert(!HasFindLastIfR<ForwardRangeNotSentinelEqualityComparableWith>);
 
-template <class It, class Sent = It>
-constexpr void test_iterators() {
-  {
-    int a[] = {1, 2, 3, 4};
-    std::same_as<std::ranges::subrange<It>> auto ret =
-        std::ranges::find_last_if(It(a), Sent(It(a + 4)), [](int x) { return x == 4; });
-    assert(base(ret.begin()) == a + 3);
-    assert(*ret.begin() == 4);
-  }
-  {
-    int a[]    = {1, 2, 3, 4};
-    auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
-
-    std::same_as<std::ranges::subrange<It>> auto ret = std::ranges::find_last_if(range, [](int x) { return x == 4; });
-    assert(base(ret.begin()) == a + 3);
-    assert(*ret.begin() == 4);
-  }
+template <class It, class Sent>
+constexpr auto make_range(auto& a) {
+  return std::ranges::subrange(It(std::ranges::begin(a)), Sent(It(std::ranges::end(a))));
 }
 
-struct NonConstComparable {
-  friend constexpr bool operator==(const NonConstComparable&, const NonConstComparable&) { return false; }
-  friend constexpr bool operator==(NonConstComparable&, NonConstComparable&) { return false; }
-  friend constexpr bool operator==(const NonConstComparable&, NonConstComparable&) { return false; }
-  friend constexpr bool operator==(NonConstComparable&, const NonConstComparable&) { return true; }
-};
-
-constexpr bool test() {
-  test_iterators<int*>();
-  test_iterators<const int*>();
-  test_iterators<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
-  test_iterators<bidirectional_iterator<int*>>();
-  test_iterators<forward_iterator<int*>>();
-  test_iterators<random_access_iterator<int*>>();
-  test_iterators<contiguous_iterator<int*>>();
-
+template <template <class> class IteratorT, template <class> class SentinelT>
+constexpr void test_iterator_classes() {
   {
-    // check that projections are used properly and that they are called with the iterator directly
+    using it   = IteratorT<int*>;
+    using sent = SentinelT<it>;
+
     {
       int a[] = {1, 2, 3, 4};
-      auto ret =
-          std::ranges::find_last_if(a, a + 4, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
-      assert(ret == a + 3);
+      std::same_as<std::ranges::subrange<it>> auto ret =
+          std::ranges::find_last_if(it(a), sent(it(a + 4)), [](int x) { return x == 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
     }
     {
-      int a[]  = {1, 2, 3, 4};
-      auto ret = std::ranges::find_last_if(a, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
-      assert(ret == a + 3);
+      int a[] = {1, 2, 3, 4};
+
+      std::same_as<std::ranges::subrange<it>> auto ret =
+          std::ranges::find_last_if(make_range<it, sent>(a), [](int x) { return x == 4; });
+      assert(ret.begin() == it(a + 3));
+      assert(*ret.begin() == 4);
     }
   }
 
-  {
-    // check that the last element is returned
+  { // check that an empty range works
+    using it   = IteratorT<std::ranges::iterator_t<std::array<int, 0>&>>;
+    using sent = SentinelT<it>;
+
     {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last_if(a, [](int i) { return i == 0; }, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if(it(a.data()), sent(it(a.data())), [](auto&&) { return true; }).begin();
+      assert(ret == it(a.data()));
     }
     {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last_if(a, a + 3, [](int i) { return i == 0; }, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if(make_range<it, sent>(a), [](auto&&) { return true; }).begin();
+      assert(ret == it(a.begin()));
     }
   }
 
-  {
-    // check that end iterator is returned with no match
+  { // check that last is returned with no match
+    using it   = IteratorT<int*>;
+    using sent = SentinelT<it>;
+
     {
-      int a[]  = {1, 1, 1};
-      auto ret = std::ranges::find_last_if(a, a + 3, [](int) { return false; }).begin();
-      assert(ret == a + 3);
+      int a[] = {1, 1, 1};
+
+      auto ret = std::ranges::find_last_if(it(a), sent(it(a + 3)), [](auto&&) { return false; }).begin();
+      assert(ret == it(a + 3));
     }
     {
-      int a[]  = {1, 1, 1};
-      auto ret = std::ranges::find_last_if(a, [](int) { return false; }).begin();
-      assert(ret == a + 3);
-    }
-  }
-
-  {
-    // check that ranges::dangling is returned
-    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
-        std::ranges::find_last_if(std::array{1, 2}, [](int) { return false; });
-  }
+      int a[] = {1, 1, 1};
 
-  {
-    // check that an iterator is returned with a borrowing range
-    int a[] = {1, 2, 3, 4};
-    std::same_as<std::ranges::subrange<int*>> auto ret =
-        std::ranges::find_last_if(std::views::all(a), [](int) { return true; });
-    assert(ret.begin() == a + 3);
-    assert(*ret.begin() == 4);
+      auto ret = std::ranges::find_last_if(make_range<it, sent>(a), [](auto&&) { return false; }).begin();
+      assert(ret == it(a + 3));
+    }
   }
 
-  {
-    // check that std::invoke is used
+  { // check that the last element is returned
     struct S {
-      int i;
+      int comp;
+      int other;
     };
-    S a[] = {S{1}, S{3}, S{2}};
+    using it   = IteratorT<S*>;
+    using sent = SentinelT<it>;
+
+    S a[] = {{0, 0}, {0, 2}, {0, 1}};
 
-    std::same_as<S*> auto ret = std::ranges::find_last_if(a, [](int) { return false; }, &S::i).begin();
-    assert(ret == a + 3);
+    auto ret = std::ranges::find_last_if(
+                   it(std::begin(a)), sent(it(std::end(a))), [](int c) { return c == 0; }, &S::comp)
+                   .begin();
+    assert(ret == it(a + 2));
+    assert((*ret).comp == 0);
+    assert((*ret).other == 1);
   }
 
   {
@@ -179,10 +145,14 @@ constexpr bool test() {
       int a[]              = {1, 2, 3, 4};
       int predicate_count  = 0;
       int projection_count = 0;
+
+      using it   = IteratorT<int*>;
+      using sent = SentinelT<it>;
+
       auto ret =
           std::ranges::find_last_if(
-              a,
-              a + 4,
+              it(a),
+              sent(it(a + 4)),
               [&](int i) {
                 ++predicate_count;
                 return i == 2;
@@ -192,32 +162,73 @@ constexpr bool test() {
                 return i;
               })
               .begin();
-      assert(ret == a + 1);
+      assert(ret == it(a + 1));
       assert(*ret == 2);
-      assert(predicate_count == 3);
-      assert(projection_count == 3);
+
+      if constexpr (std::bidirectional_iterator<it>) {
+        assert(predicate_count == 3);
+        assert(projection_count == 3);
+      } else {
+        assert(predicate_count == 4);
+        assert(projection_count == 4);
+      }
     }
+  }
+}
+
+struct NonConstComparable {
+  friend constexpr bool operator==(const NonConstComparable&, const NonConstComparable&) { return false; }
+  friend constexpr bool operator==(NonConstComparable&, NonConstComparable&) { return false; }
+  friend constexpr bool operator==(const NonConstComparable&, NonConstComparable&) { return false; }
+  friend constexpr bool operator==(NonConstComparable&, const NonConstComparable&) { return true; }
+};
+
+template <class T>
+struct add_const_to_ptr;
+template <class T>
+struct add_const_to_ptr<T*> {
+  using type = const T*;
+};
+template <class T>
+using add_const_to_ptr_t = add_const_to_ptr<T>::type;
+
+constexpr bool test() {
+  test_iterator_classes<std::type_identity_t, std::type_identity_t>();
+  test_iterator_classes<add_const_to_ptr_t, std::type_identity_t>();
+  test_iterator_classes<contiguous_iterator, std::type_identity_t>();
+  test_iterator_classes<random_access_iterator, std::type_identity_t>();
+  test_iterator_classes<bidirectional_iterator, std::type_identity_t>();
+  test_iterator_classes<forward_iterator, std::type_identity_t>();
+  test_iterator_classes<forward_iterator, sentinel_wrapper>();
+
+  {
+    // check that projections are used properly and that they are called with the iterator directly
     {
-      int a[]              = {1, 2, 3, 4};
-      int predicate_count  = 0;
-      int projection_count = 0;
+      int a[] = {1, 2, 3, 4};
       auto ret =
-          std::ranges::find_last_if(
-              a,
-              [&](int i) {
-                ++predicate_count;
-                return i == 2;
-              },
-              [&](int i) {
-                ++projection_count;
-                return i;
-              })
-              .begin();
-      assert(ret == a + 1);
-      assert(*ret == 2);
-      assert(predicate_count == 3);
-      assert(projection_count == 3);
+          std::ranges::find_last_if(a, a + 4, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
     }
+    {
+      int a[]  = {1, 2, 3, 4};
+      auto ret = std::ranges::find_last_if(a, [&](int* i) { return i == a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
+    }
+  }
+
+  {
+    // check that ranges::dangling is returned
+    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
+        std::ranges::find_last_if(std::array{1, 2}, [](int) { return false; });
+  }
+
+  {
+    // check that a subrange is returned with a borrowing range
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<int*>> auto ret =
+        std::ranges::find_last_if(std::views::all(a), [](int) { return true; });
+    assert(ret.begin() == a + 3);
+    assert(*ret.begin() == 4);
   }
 
   {
@@ -236,22 +247,6 @@ constexpr bool test() {
     }
   }
 
-  {
-    // check that an empty range works
-    {
-      std::array<int, 0> a = {};
-
-      auto ret = std::ranges::find_last_if(a.begin(), a.end(), [](int) { return true; }).begin();
-      assert(ret == a.begin());
-    }
-    {
-      std::array<int, 0> a = {};
-
-      auto ret = std::ranges::find_last_if(a, [](int) { return true; }).begin();
-      assert(ret == a.begin());
-    }
-  }
-
   return true;
 }
 
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
index 445f884c710e7..87538a5acd412 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
@@ -17,8 +17,6 @@
 //          indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
 //   constexpr borrowed_subrange_t<R> ranges::find_last_if_not(R&& r, Pred pred, Proj proj = {});
 
-#include <stdio.h>
-
 #include <algorithm>
 #include <array>
 #include <cassert>
@@ -32,149 +30,113 @@ struct Predicate {
 };
 
 template <class It, class Sent = It>
-concept HasFindLastIfNotIt = requires(It it, Sent sent) { std::ranges::find_last_if_not(it, sent, Predicate{}); };
-static_assert(HasFindLastIfNotIt<int*>);
-static_assert(HasFindLastIfNotIt<forward_iterator<int*>>);
-static_assert(!HasFindLastIfNotIt<cpp20_input_iterator<int*>>);
-static_assert(!HasFindLastIfNotIt<ForwardIteratorNotDerivedFrom>);
-static_assert(!HasFindLastIfNotIt<ForwardIteratorNotIncrementable>);
-static_assert(!HasFindLastIfNotIt<forward_iterator<int*>, SentinelForNotSemiregular>);
-static_assert(!HasFindLastIfNotIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
-
-static_assert(!HasFindLastIfNotIt<int*, int>);
-static_assert(!HasFindLastIfNotIt<int, int*>);
+concept HasFindLastIfIt = requires(It it, Sent sent) { std::ranges::find_last_if_not(it, sent, Predicate{}); };
+static_assert(HasFindLastIfIt<int*>);
+static_assert(HasFindLastIfIt<forward_iterator<int*>>);
+static_assert(!HasFindLastIfIt<cpp20_input_iterator<int*>>);
+static_assert(!HasFindLastIfIt<ForwardIteratorNotDerivedFrom>);
+static_assert(!HasFindLastIfIt<ForwardIteratorNotIncrementable>);
+static_assert(!HasFindLastIfIt<forward_iterator<int*>, SentinelForNotSemiregular>);
+static_assert(!HasFindLastIfIt<forward_iterator<int*>, InputRangeNotSentinelEqualityComparableWith>);
+
+static_assert(!HasFindLastIfIt<int*, int>);
+static_assert(!HasFindLastIfIt<int, int*>);
 
 template <class Pred>
-concept HasFindLastIfNotPred = requires(int* it, Pred pred) { std::ranges::find_last_if_not(it, it, pred); };
+concept HasFindLastIfPred = requires(int* it, Pred pred) { std::ranges::find_last_if_not(it, it, pred); };
 
-static_assert(!HasFindLastIfNotPred<IndirectUnaryPredicateNotCopyConstructible>);
-static_assert(!HasFindLastIfNotPred<IndirectUnaryPredicateNotPredicate>);
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotCopyConstructible>);
+static_assert(!HasFindLastIfPred<IndirectUnaryPredicateNotPredicate>);
 
 template <class R>
-concept HasFindLastIfNotR = requires(R r) { std::ranges::find_last_if_not(r, Predicate{}); };
-static_assert(HasFindLastIfNotR<std::array<int, 0>>);
-static_assert(!HasFindLastIfNotR<int>);
-static_assert(!HasFindLastIfNotR<ForwardRangeNotDerivedFrom>);
-static_assert(!HasFindLastIfNotR<ForwardRangeNotIncrementable>);
-static_assert(!HasFindLastIfNotR<ForwardRangeNotSentinelSemiregular>);
-static_assert(!HasFindLastIfNotR<ForwardRangeNotSentinelEqualityComparableWith>);
+concept HasFindLastIfR = requires(R r) { std::ranges::find_last_if_not(r, Predicate{}); };
+static_assert(HasFindLastIfR<std::array<int, 0>>);
+static_assert(!HasFindLastIfR<int>);
+static_assert(!HasFindLastIfR<ForwardRangeNotDerivedFrom>);
+static_assert(!HasFindLastIfR<ForwardRangeNotIncrementable>);
+static_assert(!HasFindLastIfR<ForwardRangeNotSentinelSemiregular>);
+static_assert(!HasFindLastIfR<ForwardRangeNotSentinelEqualityComparableWith>);
 
-template <class It, class Sent = It>
-constexpr void test_iterators() {
-  {
-    int a[] = {1, 2, 3, 4};
-    std::same_as<std::ranges::subrange<It>> auto ret =
-        std::ranges::find_last_if_not(It(a), Sent(It(a + 4)), [](int x) { return x != 4; });
-    assert(base(ret.begin()) == a + 3);
-    assert(*ret.begin() == 4);
-  }
-  {
-    int a[]    = {1, 2, 3, 4};
-    auto range = std::ranges::subrange(It(a), Sent(It(a + 4)));
-    std::same_as<std::ranges::subrange<It>> auto ret =
-        std::ranges::find_last_if_not(range, [](int x) { return x != 4; });
-    assert(base(ret.begin()) == a + 3);
-    assert(*ret.begin() == 4);
-  }
+template <class It, class Sent>
+constexpr auto make_range(auto& a) {
+  return std::ranges::subrange(It(std::ranges::begin(a)), Sent(It(std::ranges::end(a))));
 }
 
-struct NonConstComparableLValue {
-  friend constexpr bool operator==(const NonConstComparableLValue&, const NonConstComparableLValue&) { return false; }
-  friend constexpr bool operator==(NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
-  friend constexpr bool operator==(const NonConstComparableLValue&, NonConstComparableLValue&) { return false; }
-  friend constexpr bool operator==(NonConstComparableLValue&, const NonConstComparableLValue&) { return true; }
-};
-
-constexpr bool test() {
-  test_iterators<int*>();
-  test_iterators<const int*>();
-  test_iterators<forward_iterator<int*>, sentinel_wrapper<forward_iterator<int*>>>();
-  test_iterators<bidirectional_iterator<int*>>();
-  test_iterators<forward_iterator<int*>>();
-  test_iterators<random_access_iterator<int*>>();
-  test_iterators<contiguous_iterator<int*>>();
-
+template <template <class> class IteratorT, template <class> class SentinelT>
+constexpr void test_iterator_classes() {
   {
-    // check that projections are used properly and that they are called with the iterator directly
+    using it   = IteratorT<int*>;
+    using sent = SentinelT<it>;
+
     {
-      int a[]  = {1, 2, 3, 4};
-      auto ret = std::ranges::find_last_if_not(
-                     a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; })
-                     .begin();
-      assert(ret == a + 3);
+      int a[] = {1, 2, 3, 4};
+      std::same_as<std::ranges::subrange<it>> auto ret =
+          std::ranges::find_last_if_not(it(a), sent(it(a + 4)), [](int x) { return x != 4; });
+      assert(base(ret.begin()) == a + 3);
+      assert(*ret.begin() == 4);
     }
     {
       int a[] = {1, 2, 3, 4};
-      auto ret =
-          std::ranges::find_last_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; }).begin();
-      assert(ret == a + 3);
+
+      std::same_as<std::ranges::subrange<it>> auto ret =
+          std::ranges::find_last_if_not(make_range<it, sent>(a), [](int x) { return x != 4; });
+      assert(ret.begin() == it(a + 3));
+      assert(*ret.begin() == 4);
     }
   }
 
-  {
-    // check that the last element is returned
+  { // check that an empty range works
+    using it   = IteratorT<std::ranges::iterator_t<std::array<int, 0>&>>;
+    using sent = SentinelT<it>;
+
     {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last_if_not(a, [](int i) { return i != 0; }, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if_not(it(a.data()), sent(it(a.data())), [](auto&&) { return false; }).begin();
+      assert(ret == it(a.data()));
     }
     {
-      struct S {
-        int comp;
-        int other;
-      };
-      S a[]    = {{0, 0}, {0, 2}, {0, 1}};
-      auto ret = std::ranges::find_last_if_not(a, a + 3, [](int i) { return i != 0; }, &S::comp).begin();
-      assert(ret == a + 2);
-      assert(ret->comp == 0);
-      assert(ret->other == 1);
+      std::array<int, 0> a = {};
+
+      auto ret = std::ranges::find_last_if_not(make_range<it, sent>(a), [](auto&&) { return false; }).begin();
+      assert(ret == it(a.begin()));
     }
   }
 
-  {
-    // check that end iterator is returned with no match
+  { // check that last is returned with no match
+    using it   = IteratorT<int*>;
+    using sent = SentinelT<it>;
+
     {
-      int a[]  = {1, 1, 1};
-      auto ret = std::ranges::find_last_if_not(a, a + 3, [](int) { return true; }).begin();
-      assert(ret == a + 3);
+      int a[] = {1, 1, 1};
+
+      auto ret = std::ranges::find_last_if_not(it(a), sent(it(a + 3)), [](auto&&) { return true; }).begin();
+      assert(ret == it(a + 3));
     }
     {
-      int a[]  = {1, 1, 1};
-      auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }).begin();
-      assert(ret == a + 3);
-    }
-  }
-
-  {
-    // check that ranges::dangling is returned
-    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
-        std::ranges::find_last_if_not(std::array{1, 2}, [](int) { return true; });
-  }
+      int a[] = {1, 1, 1};
 
-  {
-    // check that an iterator is returned with a borrowing range
-    int a[] = {1, 2, 3, 4};
-    std::same_as<std::ranges::subrange<int*>> auto ret =
-        std::ranges::find_last_if_not(std::views::all(a), [](int) { return false; });
-    assert(ret.begin() == a + 3);
-    assert(*ret.begin() == 4);
+      auto ret = std::ranges::find_last_if_not(make_range<it, sent>(a), [](auto&&) { return true; }).begin();
+      assert(ret == it(a + 3));
+    }
   }
 
-  {
-    // check that std::invoke is used
+  { // check that the last element is returned
     struct S {
-      int i;
+      int comp;
+      int other;
     };
-    S a[] = {S{1}, S{3}, S{2}};
+    using it   = IteratorT<S*>;
+    using sent = SentinelT<it>;
+
+    S a[] = {{0, 0}, {0, 2}, {0, 1}};
 
-    std::same_as<S*> auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }, &S::i).begin();
-    assert(ret == a + 3);
+    auto ret = std::ranges::find_last_if_not(
+                   it(std::begin(a)), sent(it(std::end(a))), [](int c) { return c != 0; }, &S::comp)
+                   .begin();
+    assert(ret == it(a + 2));
+    assert((*ret).comp == 0);
+    assert((*ret).other == 1);
   }
 
   {
@@ -183,31 +145,14 @@ constexpr bool test() {
       int a[]              = {1, 2, 3, 4};
       int predicate_count  = 0;
       int projection_count = 0;
+
+      using it   = IteratorT<int*>;
+      using sent = SentinelT<it>;
+
       auto ret =
           std::ranges::find_last_if_not(
-              a,
-              a + 4,
-              [&](int i) {
-                ++predicate_count;
-                return i != 2;
-              },
-              [&](int i) {
-                ++projection_count;
-                return i;
-              })
-              .begin();
-      assert(ret == a + 1);
-      assert(*ret == 2);
-      assert(predicate_count == 3);
-      assert(projection_count == 3);
-    }
-    {
-      int a[]              = {1, 2, 3, 4};
-      int predicate_count  = 0;
-      int projection_count = 0;
-      auto ret =
-          std::ranges::find_last_if_not(
-              a,
+              it(a),
+              sent(it(a + 4)),
               [&](int i) {
                 ++predicate_count;
                 return i != 2;
@@ -217,41 +162,90 @@ constexpr bool test() {
                 return i;
               })
               .begin();
-      assert(ret == a + 1);
+      assert(ret == it(a + 1));
       assert(*ret == 2);
-      assert(predicate_count == 3);
-      assert(projection_count == 3);
+
+      if constexpr (std::bidirectional_iterator<it>) {
+        assert(predicate_count == 3);
+        assert(projection_count == 3);
+      } else {
+        assert(predicate_count == 4);
+        assert(projection_count == 4);
+      }
     }
   }
+}
+
+struct NonConstComparable {
+  friend constexpr bool operator!=(const NonConstComparable&, const NonConstComparable&) { return true; }
+  friend constexpr bool operator!=(NonConstComparable&, NonConstComparable&) { return true; }
+  friend constexpr bool operator!=(const NonConstComparable&, NonConstComparable&) { return true; }
+  friend constexpr bool operator!=(NonConstComparable&, const NonConstComparable&) { return false; }
+};
+
+template <class T>
+struct add_const_to_ptr;
+template <class T>
+struct add_const_to_ptr<T*> {
+  using type = const T*;
+};
+template <class T>
+using add_const_to_ptr_t = add_const_to_ptr<T>::type;
+
+constexpr bool test() {
+  test_iterator_classes<std::type_identity_t, std::type_identity_t>();
+  test_iterator_classes<add_const_to_ptr_t, std::type_identity_t>();
+  test_iterator_classes<contiguous_iterator, std::type_identity_t>();
+  test_iterator_classes<random_access_iterator, std::type_identity_t>();
+  test_iterator_classes<bidirectional_iterator, std::type_identity_t>();
+  test_iterator_classes<forward_iterator, std::type_identity_t>();
+  test_iterator_classes<forward_iterator, sentinel_wrapper>();
 
   {
-    // check that the return type of `iter::operator*` doesn't change
+    // check that projections are used properly and that they are called with the iterator directly
     {
-      NonConstComparableLValue a[] = {NonConstComparableLValue{}};
-      auto ret =
-          std::ranges::find_last_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparableLValue{}; }).begin();
-      assert(ret == a);
+      int a[]  = {1, 2, 3, 4};
+      auto ret = std::ranges::find_last_if_not(
+                     a, a + 4, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; })
+                     .begin();
+      assert(ret == a + 3);
     }
     {
-      NonConstComparableLValue a[] = {NonConstComparableLValue{}};
-      auto ret = std::ranges::find_last_if_not(a, [](auto&& e) { return e != NonConstComparableLValue{}; }).begin();
-      assert(ret == a);
+      int a[] = {1, 2, 3, 4};
+      auto ret =
+          std::ranges::find_last_if_not(a, [&](int* i) { return i != a + 3; }, [](int& i) { return &i; }).begin();
+      assert(ret == a + 3);
     }
   }
 
   {
-    // check that an empty range works
+    // check that ranges::dangling is returned
+    [[maybe_unused]] std::same_as<std::ranges::dangling> auto ret =
+        std::ranges::find_last_if_not(std::array{1, 2}, [](int) { return false; });
+  }
+
+  {
+    // check that a subrange is returned with a borrowing range
+    int a[] = {1, 2, 3, 4};
+    std::same_as<std::ranges::subrange<int*>> auto ret =
+        std::ranges::find_last_if_not(std::views::all(a), [](int) { return false; });
+    assert(ret.begin() == a + 3);
+    assert(*ret.begin() == 4);
+  }
+
+  {
+    // check that the return type of `iter::operator*` doesn't change
     {
-      std::array<int, 0> a = {};
+      NonConstComparable a[] = {NonConstComparable{}};
 
-      auto ret = std::ranges::find_last_if_not(a.begin(), a.end(), [](int) { return true; }).begin();
-      assert(ret == a.begin());
+      auto ret = std::ranges::find_last_if_not(a, a + 1, [](auto&& e) { return e != NonConstComparable{}; }).begin();
+      assert(ret == a);
     }
     {
-      std::array<int, 0> a = {};
+      NonConstComparable a[] = {NonConstComparable{}};
 
-      auto ret = std::ranges::find_last_if_not(a, [](int) { return true; }).begin();
-      assert(ret == a.begin());
+      auto ret = std::ranges::find_last_if_not(a, [](auto&& e) { return e != NonConstComparable{}; }).begin();
+      assert(ret == a);
     }
   }
 

>From 89c15f386c50e648b38d4d235beca8a092a18091 Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Thu, 18 Jul 2024 21:12:22 +0200
Subject: [PATCH 8/9] format it

---
 ...anges_robust_against_copying_comparators.pass.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
index 3c1cd606c6bff..6bac7deadfa6e 100644
--- a/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
+++ b/libcxx/test/libcxx/algorithms/ranges_robust_against_copying_comparators.pass.cpp
@@ -120,10 +120,14 @@ constexpr bool all_the_algorithms()
     (void)std::ranges::find_if_not(first, last, UnaryTrue(&copies)); assert(copies == 0);
     (void)std::ranges::find_if_not(a, UnaryTrue(&copies)); assert(copies == 0);
 #if TEST_STD_VER >= 23
-    (void)std::ranges::find_last_if(first, last, UnaryTrue(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if(a, UnaryTrue(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(&copies)); assert(copies == 0);
-    (void)std::ranges::find_last_if_not(a, UnaryTrue(&copies)); assert(copies == 0);
+    (void)std::ranges::find_last_if(first, last, UnaryTrue(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if(a, UnaryTrue(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if_not(first, last, UnaryTrue(&copies));
+    assert(copies == 0);
+    (void)std::ranges::find_last_if_not(a, UnaryTrue(&copies));
+    assert(copies == 0);
 #endif
     (void)std::ranges::for_each(first, last, UnaryVoid(&copies)); assert(copies == 1); copies = 0;
     (void)std::ranges::for_each(a, UnaryVoid(&copies)); assert(copies == 1); copies = 0;

>From d186443a4bfe02d6851dc0b3d92ff507d8808bed Mon Sep 17 00:00:00 2001
From: Nicole Mazzuca <nicole at strega-nil.co>
Date: Thu, 18 Jul 2024 23:34:09 +0200
Subject: [PATCH 9/9] ldionne's recommendations

---
 libcxx/docs/ReleaseNotes/19.rst                           | 2 +-
 libcxx/include/__algorithm/ranges_find_last.h             | 8 ++++----
 libcxx/include/algorithm                                  | 5 +++++
 .../alg.find.last/ranges.find_last.pass.cpp               | 5 ++---
 .../alg.find.last/ranges.find_last_if.pass.cpp            | 8 +-------
 .../alg.find.last/ranges.find_last_if_not.pass.cpp        | 8 +-------
 6 files changed, 14 insertions(+), 22 deletions(-)

diff --git a/libcxx/docs/ReleaseNotes/19.rst b/libcxx/docs/ReleaseNotes/19.rst
index 03a021023aa5e..f19ba15093375 100644
--- a/libcxx/docs/ReleaseNotes/19.rst
+++ b/libcxx/docs/ReleaseNotes/19.rst
@@ -55,7 +55,7 @@ Implemented Papers
 - P2231R1 - Missing ``constexpr`` in ``std::optional`` and ``std::variant``
 - P0019R8 - ``std::atomic_ref``
 - P2389R2 - Alias template ``dims`` for the ``extents`` of ``mdspan``
-- P1223R5 - ``find_last``
+- P1223R5 - ``ranges::find_last()``, ``ranges::find_last_if()``, and ``ranges::find_last_if_not()``
 
 Improvements and New Features
 -----------------------------
diff --git a/libcxx/include/__algorithm/ranges_find_last.h b/libcxx/include/__algorithm/ranges_find_last.h
index e6ca74349081b..95f7e77b8ccbe 100644
--- a/libcxx/include/__algorithm/ranges_find_last.h
+++ b/libcxx/include/__algorithm/ranges_find_last.h
@@ -37,7 +37,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 namespace ranges {
 
 template <class _Iter, class _Sent, class _Pred, class _Proj>
-_LIBCPP_HIDE_FROM_ABI constexpr static subrange<_Iter>
+_LIBCPP_HIDE_FROM_ABI constexpr subrange<_Iter>
 __find_last_impl(_Iter __first, _Sent __last, _Pred __pred, _Proj& __proj) {
   if (__first == __last) {
     return subrange<_Iter>(__first, __first);
@@ -78,7 +78,7 @@ struct __fn {
   struct __op {
     const _Type& __value;
     template <class _Elem>
-    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+    _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Elem&& __elem) const {
       return std::forward<_Elem>(__elem) == __value;
     }
   };
@@ -105,7 +105,7 @@ struct __fn {
   struct __op {
     _Pred& __pred;
     template <class _Elem>
-    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+    _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Elem&& __elem) const {
       return std::invoke(__pred, std::forward<_Elem>(__elem));
     }
   };
@@ -135,7 +135,7 @@ struct __fn {
   struct __op {
     _Pred& __pred;
     template <class _Elem>
-    constexpr decltype(auto) operator()(_Elem&& __elem) const {
+    _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator()(_Elem&& __elem) const {
       return !std::invoke(__pred, std::forward<_Elem>(__elem));
     }
   };
diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index e7f7ee00591b2..698e6f5cb7ad1 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -105,19 +105,24 @@ namespace ranges {
   template<forward_iterator I, sentinel_for<I> S, class T, class Proj = identity>
     requires indirect_binary_predicate<ranges::equal_to, projected<I, Proj>, const T*>
     constexpr subrange<I> find_last(I first, S last, const T& value, Proj proj = {});                     // since C++23
+
   template<forward_range R, class T, class Proj = identity>
     requires
       indirect_binary_predicate<ranges::equal_to, projected<iterator_t<R>, Proj>, const T*>
     constexpr borrowed_subrange_t<R> find_last(R&& r, const T& value, Proj proj = {});                   // since C++23
+
   template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
     constexpr subrange<I> find_last_if(I first, S last, Pred pred, Proj proj = {});                      // since C++23
+
   template<forward_range R, class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
     constexpr borrowed_subrange_t<R> find_last_if(R&& r, Pred pred, Proj proj = {});                     // since C++23
+
   template<forward_iterator I, sentinel_for<I> S, class Proj = identity,
            indirect_unary_predicate<projected<I, Proj>> Pred>
     constexpr subrange<I> find_last_if_not(I first, S last, Pred pred, Proj proj = {});                  // since C++23
+
   template<forward_range R, class Proj = identity,
            indirect_unary_predicate<projected<iterator_t<R>, Proj>> Pred>
     constexpr borrowed_subrange_t<R> find_last_if_not(R&& r, Pred pred, Proj proj = {});                 // since C++23
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
index 499528fab2cbc..bc7b99de82d52 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last.pass.cpp
@@ -25,7 +25,6 @@
 #include <algorithm>
 #include <array>
 #include <cassert>
-#include <deque>
 #include <ranges>
 #include <vector>
 
@@ -154,12 +153,12 @@ class TriviallyComparable {
   ElementT el_;
 
 public:
-  TEST_CONSTEXPR TriviallyComparable(ElementT el) : el_(el) {}
+  constexpr TriviallyComparable(ElementT el) : el_(el) {}
   bool operator==(const TriviallyComparable&) const = default;
 };
 
 constexpr bool test() {
-  types::for_each(types::type_list<char, wchar_t, int, long, TriviallyComparable<char>, TriviallyComparable<wchar_t>>{},
+  types::for_each(types::type_list<char, int, TriviallyComparable<char>>{},
                   []<class T> {
                     types::for_each(types::forward_iterator_list<T*>{}, []<class Iter> {
                       test_iterators<Iter>();
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
index 0ce3cae693038..a15f81bd4e481 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if.pass.cpp
@@ -184,13 +184,7 @@ struct NonConstComparable {
 };
 
 template <class T>
-struct add_const_to_ptr;
-template <class T>
-struct add_const_to_ptr<T*> {
-  using type = const T*;
-};
-template <class T>
-using add_const_to_ptr_t = add_const_to_ptr<T>::type;
+using add_const_to_ptr_t = std::add_pointer_t<std::add_const_t<std::remove_pointer_t<T>>>;
 
 constexpr bool test() {
   test_iterator_classes<std::type_identity_t, std::type_identity_t>();
diff --git a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
index 87538a5acd412..bb0e411acf0fa 100644
--- a/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.nonmodifying/alg.find.last/ranges.find_last_if_not.pass.cpp
@@ -184,13 +184,7 @@ struct NonConstComparable {
 };
 
 template <class T>
-struct add_const_to_ptr;
-template <class T>
-struct add_const_to_ptr<T*> {
-  using type = const T*;
-};
-template <class T>
-using add_const_to_ptr_t = add_const_to_ptr<T>::type;
+using add_const_to_ptr_t = std::add_pointer_t<std::add_const_t<std::remove_pointer_t<T>>>;
 
 constexpr bool test() {
   test_iterator_classes<std::type_identity_t, std::type_identity_t>();



More information about the libcxx-commits mailing list