public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Implement LWG 3820/3849 changes to cartesian_product_view
@ 2023-03-08 15:11 Patrick Palka
  2023-03-09 18:00 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-03-08 15:11 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

The LWG 3820 testcase revealed a bug in _M_advance, which this patch
also fixes.

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

libstdc++-v3/ChangeLog:

	* include/std/ranges
	(cartesian_product_view::_Iterator::_Iterator): Remove
	constraint on default constructor as per LWG 3849.
	(cartesian_product_view::_Iterator::_M_prev): Adjust position
	of _Nm > 0 test as per LWG 3820.
	(cartesian_product_view::_Iterator::_M_advance): Perform bound
	checking only on sized cartesian products.
	* testsuite/std/ranges/cartesian_product/1.cc (test08): New test.
---
 libstdc++-v3/include/std/ranges               | 23 +++++++++++--------
 .../std/ranges/cartesian_product/1.cc         |  9 ++++++++
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 67566c6ebcf..14f38727198 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -8225,7 +8225,7 @@ namespace views::__adaptor
 				    range_reference_t<__maybe_const_t<_Const, _Vs>>...>;
     using difference_type = decltype(cartesian_product_view::_S_difference_type());
 
-    _Iterator() requires forward_range<__maybe_const_t<_Const, _First>> = default;
+    _Iterator() = default;
 
     constexpr
     _Iterator(_Iterator<!_Const> __i)
@@ -8390,12 +8390,12 @@ namespace views::__adaptor
     _M_prev()
     {
       auto& __it = std::get<_Nm>(_M_current);
-      if (__it == ranges::begin(std::get<_Nm>(_M_parent->_M_bases)))
-	{
-	  __it = __detail::__cartesian_common_arg_end(std::get<_Nm>(_M_parent->_M_bases));
-	  if constexpr (_Nm > 0)
+      if constexpr (_Nm > 0)
+	if (__it == ranges::begin(std::get<_Nm>(_M_parent->_M_bases)))
+	  {
+	    __it = __detail::__cartesian_common_arg_end(std::get<_Nm>(_M_parent->_M_bases));
 	    _M_prev<_Nm - 1>();
-	}
+	  }
       --__it;
     }
 
@@ -8416,10 +8416,13 @@ namespace views::__adaptor
 	  if constexpr (_Nm == 0)
 	    {
 #ifdef _GLIBCXX_ASSERTIONS
-	      auto __size = ranges::ssize(__r);
-	      auto __begin = ranges::begin(__r);
-	      auto __offset = __it - __begin;
-	      __glibcxx_assert(__offset + __x >= 0 && __offset + __x <= __size);
+	      if constexpr (sized_range<__maybe_const_t<_Const, _First>>)
+		{
+		  auto __size = ranges::ssize(__r);
+		  auto __begin = ranges::begin(__r);
+		  auto __offset = __it - __begin;
+		  __glibcxx_assert(__offset + __x >= 0 && __offset + __x <= __size);
+		}
 #endif
 	      __it += __x;
 	    }
diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
index f52c2b96d58..56ff3d152c6 100644
--- a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
+++ b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
@@ -201,6 +201,14 @@ test07()
   VERIFY( i == 5 );
 }
 
+void
+test08()
+{
+  // LWG 3820
+  auto r = std::views::cartesian_product(std::views::iota(0));
+  r.begin() += 3; // hard error
+}
+
 int
 main()
 {
@@ -211,4 +219,5 @@ main()
   test05();
   static_assert(test06());
   test07();
+  test08();
 }
-- 
2.40.0.rc0.57.g454dfcbddf


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] libstdc++: Implement LWG 3820/3849 changes to cartesian_product_view
  2023-03-08 15:11 [PATCH] libstdc++: Implement LWG 3820/3849 changes to cartesian_product_view Patrick Palka
@ 2023-03-09 18:00 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-03-09 18:00 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches, libstdc++

On Wed, 8 Mar 2023 at 15:13, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> The LWG 3820 testcase revealed a bug in _M_advance, which this patch
> also fixes.
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

OK

>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges
>         (cartesian_product_view::_Iterator::_Iterator): Remove
>         constraint on default constructor as per LWG 3849.
>         (cartesian_product_view::_Iterator::_M_prev): Adjust position
>         of _Nm > 0 test as per LWG 3820.
>         (cartesian_product_view::_Iterator::_M_advance): Perform bound
>         checking only on sized cartesian products.
>         * testsuite/std/ranges/cartesian_product/1.cc (test08): New test.
> ---
>  libstdc++-v3/include/std/ranges               | 23 +++++++++++--------
>  .../std/ranges/cartesian_product/1.cc         |  9 ++++++++
>  2 files changed, 22 insertions(+), 10 deletions(-)
>
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index 67566c6ebcf..14f38727198 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -8225,7 +8225,7 @@ namespace views::__adaptor
>                                     range_reference_t<__maybe_const_t<_Const, _Vs>>...>;
>      using difference_type = decltype(cartesian_product_view::_S_difference_type());
>
> -    _Iterator() requires forward_range<__maybe_const_t<_Const, _First>> = default;
> +    _Iterator() = default;
>
>      constexpr
>      _Iterator(_Iterator<!_Const> __i)
> @@ -8390,12 +8390,12 @@ namespace views::__adaptor
>      _M_prev()
>      {
>        auto& __it = std::get<_Nm>(_M_current);
> -      if (__it == ranges::begin(std::get<_Nm>(_M_parent->_M_bases)))
> -       {
> -         __it = __detail::__cartesian_common_arg_end(std::get<_Nm>(_M_parent->_M_bases));
> -         if constexpr (_Nm > 0)
> +      if constexpr (_Nm > 0)
> +       if (__it == ranges::begin(std::get<_Nm>(_M_parent->_M_bases)))
> +         {
> +           __it = __detail::__cartesian_common_arg_end(std::get<_Nm>(_M_parent->_M_bases));
>             _M_prev<_Nm - 1>();
> -       }
> +         }
>        --__it;
>      }
>
> @@ -8416,10 +8416,13 @@ namespace views::__adaptor
>           if constexpr (_Nm == 0)
>             {
>  #ifdef _GLIBCXX_ASSERTIONS
> -             auto __size = ranges::ssize(__r);
> -             auto __begin = ranges::begin(__r);
> -             auto __offset = __it - __begin;
> -             __glibcxx_assert(__offset + __x >= 0 && __offset + __x <= __size);
> +             if constexpr (sized_range<__maybe_const_t<_Const, _First>>)
> +               {
> +                 auto __size = ranges::ssize(__r);
> +                 auto __begin = ranges::begin(__r);
> +                 auto __offset = __it - __begin;
> +                 __glibcxx_assert(__offset + __x >= 0 && __offset + __x <= __size);
> +               }
>  #endif
>               __it += __x;
>             }
> diff --git a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
> index f52c2b96d58..56ff3d152c6 100644
> --- a/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/cartesian_product/1.cc
> @@ -201,6 +201,14 @@ test07()
>    VERIFY( i == 5 );
>  }
>
> +void
> +test08()
> +{
> +  // LWG 3820
> +  auto r = std::views::cartesian_product(std::views::iota(0));
> +  r.begin() += 3; // hard error
> +}
> +
>  int
>  main()
>  {
> @@ -211,4 +219,5 @@ main()
>    test05();
>    static_assert(test06());
>    test07();
> +  test08();
>  }
> --
> 2.40.0.rc0.57.g454dfcbddf
>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-09 18:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-08 15:11 [PATCH] libstdc++: Implement LWG 3820/3849 changes to cartesian_product_view Patrick Palka
2023-03-09 18:00 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).