[2/4] libstdc++: Implement LWG 3569 changes to join_view::_Iterator

Message ID 20220912164531.1742034-2-ppalka@redhat.com
State New, archived
Headers
Series [1/4] libstdc++: Add already-accepted <ranges> testcase [PR106320] |

Commit Message

Patrick Palka Sept. 12, 2022, 4:45 p.m. UTC
  Tested on x86_64-pc-linux-gnu, does this look OK for trunk only?

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Iterator::_M_satisfy):
	Adjust resetting _M_inner as per LWG 3569.
	(join_view::_Iterator::_M_inner): Wrap in std::optional
	as per LWG 3569.
	(join_view::_Iterator::_Iterator): Relax constraints as
	per LWG 3569.
	(join_view::_Iterator::operator*): Adjust as per LWG 3569.
	(join_view::_Iterator::operator->): Likewise.
	(join_view::_Iterator::operator++): Likewise.
	(join_view::_Iterator::operator--): Likewise.
	(join_view::_Iterator::iter_move): Likewise.
	(join_view::_Iterator::iter_swap): Likewise.
	* testsuite/std/ranges/adaptor/join.cc (test14): New test.
---
 libstdc++-v3/include/std/ranges               | 28 +++++++++----------
 .../testsuite/std/ranges/adaptors/join.cc     | 17 +++++++++++
 2 files changed, 30 insertions(+), 15 deletions(-)
  

Comments

Jonathan Wakely Sept. 13, 2022, 10:57 a.m. UTC | #1
On Mon, 12 Sept 2022 at 17:46, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk only?

I briefly wondered whether we could just use a union there and provide
the special members to init/copy/destroy it properly, but we already
use <optional> in <ranges> so it's probably not worth it.

OK for trunk.

>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges (join_view::_Iterator::_M_satisfy):
>         Adjust resetting _M_inner as per LWG 3569.
>         (join_view::_Iterator::_M_inner): Wrap in std::optional
>         as per LWG 3569.
>         (join_view::_Iterator::_Iterator): Relax constraints as
>         per LWG 3569.
>         (join_view::_Iterator::operator*): Adjust as per LWG 3569.
>         (join_view::_Iterator::operator->): Likewise.
>         (join_view::_Iterator::operator++): Likewise.
>         (join_view::_Iterator::operator--): Likewise.
>         (join_view::_Iterator::iter_move): Likewise.
>         (join_view::_Iterator::iter_swap): Likewise.
>         * testsuite/std/ranges/adaptor/join.cc (test14): New test.
> ---
>  libstdc++-v3/include/std/ranges               | 28 +++++++++----------
>  .../testsuite/std/ranges/adaptors/join.cc     | 17 +++++++++++
>  2 files changed, 30 insertions(+), 15 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index 20eb4e82ac8..6297ce7cee3 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -2746,7 +2746,7 @@ namespace views::__adaptor
>               }
>
>             if constexpr (_S_ref_is_glvalue)
> -             _M_inner = _Inner_iter();
> +             _M_inner.reset();
>           }
>
>           static constexpr auto
> @@ -2769,7 +2769,7 @@ namespace views::__adaptor
>           using _Inner_iter = join_view::_Inner_iter<_Const>;
>
>           _Outer_iter _M_outer = _Outer_iter();
> -         _Inner_iter _M_inner = _Inner_iter();
> +         optional<_Inner_iter> _M_inner;
>           _Parent* _M_parent = nullptr;
>
>         public:
> @@ -2780,9 +2780,7 @@ namespace views::__adaptor
>             = common_type_t<range_difference_t<_Base>,
>                             range_difference_t<range_reference_t<_Base>>>;
>
> -         _Iterator() requires (default_initializable<_Outer_iter>
> -                               && default_initializable<_Inner_iter>)
> -           = default;
> +         _Iterator() requires default_initializable<_Outer_iter> = default;
>
>           constexpr
>           _Iterator(_Parent* __parent, _Outer_iter __outer)
> @@ -2801,7 +2799,7 @@ namespace views::__adaptor
>
>           constexpr decltype(auto)
>           operator*() const
> -         { return *_M_inner; }
> +         { return **_M_inner; }
>
>           // _GLIBCXX_RESOLVE_LIB_DEFECTS
>           // 3500. join_view::iterator::operator->() is bogus
> @@ -2809,7 +2807,7 @@ namespace views::__adaptor
>           operator->() const
>             requires __detail::__has_arrow<_Inner_iter>
>               && copyable<_Inner_iter>
> -         { return _M_inner; }
> +         { return *_M_inner; }
>
>           constexpr _Iterator&
>           operator++()
> @@ -2820,7 +2818,7 @@ namespace views::__adaptor
>               else
>                 return *_M_parent->_M_inner;
>             }();
> -           if (++_M_inner == ranges::end(__inner_range))
> +           if (++*_M_inner == ranges::end(__inner_range))
>               {
>                 ++_M_outer;
>                 _M_satisfy();
> @@ -2850,9 +2848,9 @@ namespace views::__adaptor
>           {
>             if (_M_outer == ranges::end(_M_parent->_M_base))
>               _M_inner = ranges::end(*--_M_outer);
> -           while (_M_inner == ranges::begin(*_M_outer))
> -             _M_inner = ranges::end(*--_M_outer);
> -           --_M_inner;
> +           while (*_M_inner == ranges::begin(*_M_outer))
> +             *_M_inner = ranges::end(*--_M_outer);
> +           --*_M_inner;
>             return *this;
>           }
>
> @@ -2879,14 +2877,14 @@ namespace views::__adaptor
>
>           friend constexpr decltype(auto)
>           iter_move(const _Iterator& __i)
> -         noexcept(noexcept(ranges::iter_move(__i._M_inner)))
> -         { return ranges::iter_move(__i._M_inner); }
> +         noexcept(noexcept(ranges::iter_move(*__i._M_inner)))
> +         { return ranges::iter_move(*__i._M_inner); }
>
>           friend constexpr void
>           iter_swap(const _Iterator& __x, const _Iterator& __y)
> -           noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
> +           noexcept(noexcept(ranges::iter_swap(*__x._M_inner, *__y._M_inner)))
>             requires indirectly_swappable<_Inner_iter>
> -         { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
> +         { return ranges::iter_swap(*__x._M_inner, *__y._M_inner); }
>
>           friend _Iterator<!_Const>;
>           template<bool> friend struct _Sentinel;
> diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
> index 530ab6663b5..afc11d4bd7a 100644
> --- a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
> @@ -21,6 +21,7 @@
>  #include <algorithm>
>  #include <array>
>  #include <ranges>
> +#include <sstream>
>  #include <string>
>  #include <string_view>
>  #include <vector>
> @@ -217,6 +218,21 @@ test13()
>    std::vector<std::vector<int>> v{{5, 6, 7}};
>    v | l | std::views::join;
>  }
> +
> +void
> +test14()
> +{
> +  // LWG 3569: join_view fails to support ranges of ranges with
> +  // non-default_initializable iterators
> +  auto ss = std::istringstream{"1 2 3"};
> +  auto v = views::single(views::istream<int>(ss));
> +  using inner = ranges::range_reference_t<decltype(v)>;
> +  static_assert(ranges::input_range<inner>
> +               && !ranges::forward_range<inner>
> +               && !std::default_initializable<ranges::iterator_t<inner>>);
> +  VERIFY( ranges::equal(v | views::join, (int[]){1, 2, 3}) );
> +}
> +
>  int
>  main()
>  {
> @@ -233,4 +249,5 @@ main()
>    test11();
>    test12();
>    test13();
> +  test14();
>  }
> --
> 2.37.3.542.gdd3f6c4cae
>
  

Patch

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 20eb4e82ac8..6297ce7cee3 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -2746,7 +2746,7 @@  namespace views::__adaptor
 	      }
 
 	    if constexpr (_S_ref_is_glvalue)
-	      _M_inner = _Inner_iter();
+	      _M_inner.reset();
 	  }
 
 	  static constexpr auto
@@ -2769,7 +2769,7 @@  namespace views::__adaptor
 	  using _Inner_iter = join_view::_Inner_iter<_Const>;
 
 	  _Outer_iter _M_outer = _Outer_iter();
-	  _Inner_iter _M_inner = _Inner_iter();
+	  optional<_Inner_iter> _M_inner;
 	  _Parent* _M_parent = nullptr;
 
 	public:
@@ -2780,9 +2780,7 @@  namespace views::__adaptor
 	    = common_type_t<range_difference_t<_Base>,
 			    range_difference_t<range_reference_t<_Base>>>;
 
-	  _Iterator() requires (default_initializable<_Outer_iter>
-				&& default_initializable<_Inner_iter>)
-	    = default;
+	  _Iterator() requires default_initializable<_Outer_iter> = default;
 
 	  constexpr
 	  _Iterator(_Parent* __parent, _Outer_iter __outer)
@@ -2801,7 +2799,7 @@  namespace views::__adaptor
 
 	  constexpr decltype(auto)
 	  operator*() const
-	  { return *_M_inner; }
+	  { return **_M_inner; }
 
 	  // _GLIBCXX_RESOLVE_LIB_DEFECTS
 	  // 3500. join_view::iterator::operator->() is bogus
@@ -2809,7 +2807,7 @@  namespace views::__adaptor
 	  operator->() const
 	    requires __detail::__has_arrow<_Inner_iter>
 	      && copyable<_Inner_iter>
-	  { return _M_inner; }
+	  { return *_M_inner; }
 
 	  constexpr _Iterator&
 	  operator++()
@@ -2820,7 +2818,7 @@  namespace views::__adaptor
 	      else
 		return *_M_parent->_M_inner;
 	    }();
-	    if (++_M_inner == ranges::end(__inner_range))
+	    if (++*_M_inner == ranges::end(__inner_range))
 	      {
 		++_M_outer;
 		_M_satisfy();
@@ -2850,9 +2848,9 @@  namespace views::__adaptor
 	  {
 	    if (_M_outer == ranges::end(_M_parent->_M_base))
 	      _M_inner = ranges::end(*--_M_outer);
-	    while (_M_inner == ranges::begin(*_M_outer))
-	      _M_inner = ranges::end(*--_M_outer);
-	    --_M_inner;
+	    while (*_M_inner == ranges::begin(*_M_outer))
+	      *_M_inner = ranges::end(*--_M_outer);
+	    --*_M_inner;
 	    return *this;
 	  }
 
@@ -2879,14 +2877,14 @@  namespace views::__adaptor
 
 	  friend constexpr decltype(auto)
 	  iter_move(const _Iterator& __i)
-	  noexcept(noexcept(ranges::iter_move(__i._M_inner)))
-	  { return ranges::iter_move(__i._M_inner); }
+	  noexcept(noexcept(ranges::iter_move(*__i._M_inner)))
+	  { return ranges::iter_move(*__i._M_inner); }
 
 	  friend constexpr void
 	  iter_swap(const _Iterator& __x, const _Iterator& __y)
-	    noexcept(noexcept(ranges::iter_swap(__x._M_inner, __y._M_inner)))
+	    noexcept(noexcept(ranges::iter_swap(*__x._M_inner, *__y._M_inner)))
 	    requires indirectly_swappable<_Inner_iter>
-	  { return ranges::iter_swap(__x._M_inner, __y._M_inner); }
+	  { return ranges::iter_swap(*__x._M_inner, *__y._M_inner); }
 
 	  friend _Iterator<!_Const>;
 	  template<bool> friend struct _Sentinel;
diff --git a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
index 530ab6663b5..afc11d4bd7a 100644
--- a/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
+++ b/libstdc++-v3/testsuite/std/ranges/adaptors/join.cc
@@ -21,6 +21,7 @@ 
 #include <algorithm>
 #include <array>
 #include <ranges>
+#include <sstream>
 #include <string>
 #include <string_view>
 #include <vector>
@@ -217,6 +218,21 @@  test13()
   std::vector<std::vector<int>> v{{5, 6, 7}};
   v | l | std::views::join;
 }
+
+void
+test14()
+{
+  // LWG 3569: join_view fails to support ranges of ranges with
+  // non-default_initializable iterators
+  auto ss = std::istringstream{"1 2 3"};
+  auto v = views::single(views::istream<int>(ss));
+  using inner = ranges::range_reference_t<decltype(v)>;
+  static_assert(ranges::input_range<inner>
+		&& !ranges::forward_range<inner>
+		&& !std::default_initializable<ranges::iterator_t<inner>>);
+  VERIFY( ranges::equal(v | views::join, (int[]){1, 2, 3}) );
+}
+
 int
 main()
 {
@@ -233,4 +249,5 @@  main()
   test11();
   test12();
   test13();
+  test14();
 }