public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: Add workaround to std::ranges::subrange [PR111948]
@ 2023-11-30 15:50 Jonathan Wakely
  2023-12-05 23:35 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2023-11-30 15:50 UTC (permalink / raw)
  To: libstdc++, gcc-patches

I think I'll push this to work around the compiler bug. We can revert it
later if the front end gets fixed.

Tested x86_64-linux. Needed on trunk and gcc-13.

-- >8 --

libstdc++-v3/ChangeLog:

	PR libstdc++/111948
	* include/bits/ranges_util.h (subrange): Add constructor to
	_Size to aoid setting member in constructor.
	* testsuite/std/ranges/subrange/111948.cc: New test.
---
 libstdc++-v3/include/bits/ranges_util.h       | 21 ++++++++++++-------
 .../testsuite/std/ranges/subrange/111948.cc   |  8 +++++++
 2 files changed, 21 insertions(+), 8 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/111948.cc

diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h
index ab6c69c57d0..185e46ec7a9 100644
--- a/libstdc++-v3/include/bits/ranges_util.h
+++ b/libstdc++-v3/include/bits/ranges_util.h
@@ -267,13 +267,21 @@ namespace ranges
       using __size_type
 	= __detail::__make_unsigned_like_t<iter_difference_t<_It>>;
 
-      template<typename, bool = _S_store_size>
+      template<typename _Tp, bool = _S_store_size>
 	struct _Size
-	{ };
+	{
+	  [[__gnu__::__always_inline__]]
+	  constexpr _Size(_Tp = {}) { }
+	};
 
       template<typename _Tp>
 	struct _Size<_Tp, true>
-	{ _Tp _M_size; };
+	{
+	  [[__gnu__::__always_inline__]]
+	  constexpr _Size(_Tp __s = {}) : _M_size(__s) { }
+
+	  _Tp _M_size;
+	};
 
       [[no_unique_address]] _Size<__size_type> _M_size = {};
 
@@ -294,11 +302,8 @@ namespace ranges
       noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
 	       && is_nothrow_constructible_v<_Sent, _Sent&>)
 	requires (_Kind == subrange_kind::sized)
-      : _M_begin(std::move(__i)), _M_end(__s)
-      {
-	if constexpr (_S_store_size)
-	  _M_size._M_size = __n;
-      }
+      : _M_begin(std::move(__i)), _M_end(__s), _M_size(__n)
+      { }
 
       template<__detail::__different_from<subrange> _Rng>
 	requires borrowed_range<_Rng>
diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc
new file mode 100644
index 00000000000..dcc64b56def
--- /dev/null
+++ b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc
@@ -0,0 +1,8 @@
+// { dg-do compile { target c++20 } }
+
+#include <ranges>
+
+// Bug libstdc++/111948 - subrange modifies a const size object
+
+constexpr auto r = std::ranges::subrange(std::views::iota(0), 5);
+static_assert(std::ranges::distance(r));
-- 
2.43.0


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

* Re: [PATCH] libstdc++: Add workaround to std::ranges::subrange [PR111948]
  2023-11-30 15:50 [PATCH] libstdc++: Add workaround to std::ranges::subrange [PR111948] Jonathan Wakely
@ 2023-12-05 23:35 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-12-05 23:35 UTC (permalink / raw)
  To: libstdc++, gcc-patches

On Thu, 30 Nov 2023 at 15:52, Jonathan Wakely <jwakely@redhat.com> wrote:
>
> I think I'll push this to work around the compiler bug. We can revert it
> later if the front end gets fixed.
>
> Tested x86_64-linux. Needed on trunk and gcc-13.

Pushed to trunk now.

>
> -- >8 --
>
> libstdc++-v3/ChangeLog:
>
>         PR libstdc++/111948
>         * include/bits/ranges_util.h (subrange): Add constructor to
>         _Size to aoid setting member in constructor.
>         * testsuite/std/ranges/subrange/111948.cc: New test.
> ---
>  libstdc++-v3/include/bits/ranges_util.h       | 21 ++++++++++++-------
>  .../testsuite/std/ranges/subrange/111948.cc   |  8 +++++++
>  2 files changed, 21 insertions(+), 8 deletions(-)
>  create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/111948.cc
>
> diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h
> index ab6c69c57d0..185e46ec7a9 100644
> --- a/libstdc++-v3/include/bits/ranges_util.h
> +++ b/libstdc++-v3/include/bits/ranges_util.h
> @@ -267,13 +267,21 @@ namespace ranges
>        using __size_type
>         = __detail::__make_unsigned_like_t<iter_difference_t<_It>>;
>
> -      template<typename, bool = _S_store_size>
> +      template<typename _Tp, bool = _S_store_size>
>         struct _Size
> -       { };
> +       {
> +         [[__gnu__::__always_inline__]]
> +         constexpr _Size(_Tp = {}) { }
> +       };
>
>        template<typename _Tp>
>         struct _Size<_Tp, true>
> -       { _Tp _M_size; };
> +       {
> +         [[__gnu__::__always_inline__]]
> +         constexpr _Size(_Tp __s = {}) : _M_size(__s) { }
> +
> +         _Tp _M_size;
> +       };
>
>        [[no_unique_address]] _Size<__size_type> _M_size = {};
>
> @@ -294,11 +302,8 @@ namespace ranges
>        noexcept(is_nothrow_constructible_v<_It, decltype(__i)>
>                && is_nothrow_constructible_v<_Sent, _Sent&>)
>         requires (_Kind == subrange_kind::sized)
> -      : _M_begin(std::move(__i)), _M_end(__s)
> -      {
> -       if constexpr (_S_store_size)
> -         _M_size._M_size = __n;
> -      }
> +      : _M_begin(std::move(__i)), _M_end(__s), _M_size(__n)
> +      { }
>
>        template<__detail::__different_from<subrange> _Rng>
>         requires borrowed_range<_Rng>
> diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc
> new file mode 100644
> index 00000000000..dcc64b56def
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/std/ranges/subrange/111948.cc
> @@ -0,0 +1,8 @@
> +// { dg-do compile { target c++20 } }
> +
> +#include <ranges>
> +
> +// Bug libstdc++/111948 - subrange modifies a const size object
> +
> +constexpr auto r = std::ranges::subrange(std::views::iota(0), 5);
> +static_assert(std::ranges::distance(r));
> --
> 2.43.0
>


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

end of thread, other threads:[~2023-12-05 23:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-30 15:50 [PATCH] libstdc++: Add workaround to std::ranges::subrange [PR111948] Jonathan Wakely
2023-12-05 23:35 ` 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).