On Tue, 7 Mar 2023, Patrick Palka wrote:

> ranges::begin() isn't guaranteed to be equality-preserving for
> non-forward ranges, so in cartesian_product_view::end we need to be
> careful about calling begin() on the first range (which could be
> non-forward) in the (non-degenerate) case where __empty_tail is false.
> 
> Since we're already using a variadic lambda to compute __empty_tail, we
> might as well use that same lambda to build up the tuple of iterators
> instead of doing it via __tuple_transform.
> 
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?
> 
>       PR libstdc++/107572
> 
> libstdc++-v3/ChangeLog:
> 
>       * include/std/ranges (cartesian_product_view::end): When
>       building the tuple of iterators, avoid calling ranges::begin on
>       the first range if __empty_tail is false.
>       * testsuite/std/ranges/cartesian_product/1.cc (test07): New test.
> ---
>  libstdc++-v3/include/std/ranges               | 36 +++++++++++++------
>  .../std/ranges/cartesian_product/1.cc         | 22 ++++++++++++
>  2 files changed, 48 insertions(+), 10 deletions(-)
> 
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index e0cac15a64f..0de7bdef504 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -8078,26 +8078,42 @@ namespace views::__adaptor
>      end() requires ((!__detail::__simple_view<_First> || ... || 
> !__detail::__simple_view<_Vs>)
>                   && __detail::__cartesian_product_is_common<_First, _Vs...>)
>      {
> -      bool __empty_tail = [this]<size_t... _Is>(index_sequence<_Is...>) {
> -     return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
> +      auto __it = [this]<size_t... _Is>(index_sequence<_Is...>) {
> +     bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
> +     auto& __first = std::get<0>(_M_bases);
> +     auto __first_it = __empty_tail
> +       ? ranges::begin(__first)
> +       : __detail::__cartesian_common_arg_end(__first);
> +     // N.B. When implementing P2165R4 this should be changed to always 
> return tuple.
> +     if constexpr (sizeof...(_Is) == 1)
> +       return std::make_pair(std::move(__first_it),
> +                              ranges::begin(std::get<1 + _Is>(_M_bases))...);
> +     else
> +       return std::make_tuple(std::move(__first_it),
> +                              ranges::begin(std::get<1 + _Is>(_M_bases))...);

On second thought, it might be better to use __tuple_or_pair_t here
instead of manually determining whether to use a pair or tuple, so that
we don't forget to adjust this site when implementing P2165R4 (which
removes __tuple_or_pair_t):

-- >8 --

Subject: [PATCH] libstdc++: extraneous begin in cartesian_product_view::end
 [PR107572]

ranges::begin() isn't guaranteed to be equality-preserving for
non-forward ranges, so in cartesian_product_view::end we need to avoid
calling begin() on the first range (which could be non-forward) in the
(non-degenerate) case where __empty_tail is false.

Since we're already using a variadic lambda to compute __empty_tail, we
might as well use that same lambda to build up the tuple of iterators
instead of doing it separately via __tuple_transform.

Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

        PR libstdc++/107572

libstdc++-v3/ChangeLog:

        * include/std/ranges (cartesian_product_view::end): When
        building the tuple of iterators, avoid calling ranges::begin on
        the first range if __empty_tail is false.
        * testsuite/std/ranges/cartesian_product/1.cc (test07): New test.
---
 libstdc++-v3/include/std/ranges               | 28 ++++++++++++-------
 .../std/ranges/cartesian_product/1.cc         | 25 +++++++++++++++++
 2 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index e0cac15a64f..d2ab79179ca 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -8078,26 +8078,34 @@ namespace views::__adaptor
     end() requires ((!__detail::__simple_view<_First> || ... || 
!__detail::__simple_view<_Vs>)
                    && __detail::__cartesian_product_is_common<_First, _Vs...>)
     {
-      bool __empty_tail = [this]<size_t... _Is>(index_sequence<_Is...>) {
-       return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+      auto __it = [this]<size_t... _Is>(index_sequence<_Is...>) {
+       using _Ret = __detail::__tuple_or_pair_t<iterator_t<_First>,
+                                                iterator_t<_Vs>...>;
+       bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+       auto& __first = std::get<0>(_M_bases);
+       return _Ret{(__empty_tail
+                    ? ranges::begin(__first)
+                    : __detail::__cartesian_common_arg_end(__first)),
+                   ranges::begin(std::get<1 + _Is>(_M_bases))...};
       }(make_index_sequence<sizeof...(_Vs)>{});
 
-      auto __it = __detail::__tuple_transform(ranges::begin, _M_bases);
-      if (!__empty_tail)
-       std::get<0>(__it) = 
__detail::__cartesian_common_arg_end(std::get<0>(_M_bases));
       return _Iterator<false>{*this, std::move(__it)};
     }
 
     constexpr _Iterator<true>
     end() const requires __detail::__cartesian_product_is_common<const _First, 
const _Vs...>
     {
-      bool __empty_tail = [this]<size_t... _Is>(index_sequence<_Is...>) {
-       return (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+      auto __it = [this]<size_t... _Is>(index_sequence<_Is...>) {
+       using _Ret = __detail::__tuple_or_pair_t<iterator_t<const _First>,
+                                                iterator_t<const _Vs>...>;
+       bool __empty_tail = (ranges::empty(std::get<1 + _Is>(_M_bases)) || ...);
+       auto& __first = std::get<0>(_M_bases);
+       return _Ret{(__empty_tail
+                    ? ranges::begin(__first)
+                    : __detail::__cartesian_common_arg_end(__first)),
+                   ranges::begin(std::get<1 + _Is>(_M_bases))...};
       }(make_index_sequence<sizeof...(_Vs)>{});
 
-      auto __it = __detail::__tuple_transform(ranges::begin, _M_bases);
-      if (!__empty_tail)
-       std::get<0>(__it) = 
__detail::__cartesian_common_arg_end(std::get<0>(_M_bases));
       return _Iterator<true>{*this, std::move(__it)};
     }
 
diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc 
b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
index 1ec4422e6f3..8069eb0ab3c 100644
--- a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
@@ -3,6 +3,7 @@
 
 #include <ranges>
 #include <algorithm>
+#include <sstream>
 #include <testsuite_hooks.h>
 #include <testsuite_iterators.h>
 
@@ -178,6 +179,29 @@ test06()
   return true;
 }
 
+void
+test07()
+{
+  // PR libstdc++/107572
+  static std::istringstream ints("0 1 2 3 4");
+  struct istream_range
+  {
+    auto begin() { return std::istream_iterator<int>{ints}; }
+    auto end() { return std::istream_iterator<int>{}; }
+    using iterator_concept = std::input_iterator_tag;
+  };
+  static_assert(!ranges::forward_range<istream_range>
+               && ranges::common_range<istream_range>);
+  istream_range r;
+  int i = 0;
+  for (auto [v] : views::cartesian_product(r))
+    {
+      VERIFY( v == i );
+      ++i;
+    };
+  VERIFY( i == 5 );
+}
+
 int
 main()
 {
@@ -187,4 +211,5 @@ main()
   test04();
   test05();
   static_assert(test06());
+  test07();
 }
-- 
2.40.0.rc0.57.g454dfcbddf

Reply via email to