public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] libstdc++: invalid default init in _CachedPosition [PR101231]
@ 2021-07-13 19:07 Patrick Palka
  2021-07-15 19:58 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2021-07-13 19:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: libstdc++, Patrick Palka

The primary template for _CachedPosition is a dummy implementation for
non-forward ranges, the iterators for which generally can't be cached.
Because this implementation doesn't actually cache anything, _M_has_value
is defined to be false and so calls to _M_get (which are always guarded
by _M_has_value) are unreachable.

Still, to suppress a "control reaches end of non-void function" warning
I made _M_get return {}, but after P2325 input iterators are no longer
necessarily default constructible so this workaround now breaks valid
programs.

This patch fixes this by instead using __builtin_unreachable to squelch
the warning.

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

	PR libstdc++/101231

libstdc++-v3/ChangeLog:

	* include/std/ranges (_CachedPosition::_M_get): For non-forward
	ranges, just call __builtin_unreachable.
	* testsuite/std/ranges/istream_view.cc (test05): New test.
---
 libstdc++-v3/include/std/ranges                   |  2 +-
 libstdc++-v3/testsuite/std/ranges/istream_view.cc | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index df74ac9dc19..d791e15d096 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -1232,7 +1232,7 @@ namespace views::__adaptor
 	_M_get(const _Range&) const
 	{
 	  __glibcxx_assert(false);
-	  return {};
+	  __builtin_unreachable();
 	}
 
 	constexpr void
diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
index 369790e89e5..2f15f787250 100644
--- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
@@ -83,6 +83,17 @@ test04()
   static_assert(!std::forward_iterator<It>);
 }
 
+void
+test05()
+{
+  // PR libstdc++/101231
+  auto words = std::istringstream{"42"};
+  auto is = ranges::istream_view<int>(words);
+  auto r = is | views::filter([](auto) { return true; });
+  for (auto x : r)
+    ;
+}
+
 int
 main()
 {
@@ -90,4 +101,5 @@ main()
   test02();
   test03();
   test04();
+  test05();
 }
-- 
2.32.0.170.gd486ca60a5


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

* Re: [PATCH] libstdc++: invalid default init in _CachedPosition [PR101231]
  2021-07-13 19:07 [PATCH] libstdc++: invalid default init in _CachedPosition [PR101231] Patrick Palka
@ 2021-07-15 19:58 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-07-15 19:58 UTC (permalink / raw)
  To: Patrick Palka; +Cc: gcc-patches, libstdc++

On Tue, 13 Jul 2021 at 20:09, Patrick Palka via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> The primary template for _CachedPosition is a dummy implementation for
> non-forward ranges, the iterators for which generally can't be cached.
> Because this implementation doesn't actually cache anything, _M_has_value
> is defined to be false and so calls to _M_get (which are always guarded
> by _M_has_value) are unreachable.
>
> Still, to suppress a "control reaches end of non-void function" warning
> I made _M_get return {}, but after P2325 input iterators are no longer
> necessarily default constructible so this workaround now breaks valid
> programs.
>
> This patch fixes this by instead using __builtin_unreachable to squelch
> the warning.
>
> Tested on x86_64-pc-linux-gnu, does this look OK for trunk?

Yes, thanks.


>
>         PR libstdc++/101231
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ranges (_CachedPosition::_M_get): For non-forward
>         ranges, just call __builtin_unreachable.
>         * testsuite/std/ranges/istream_view.cc (test05): New test.
> ---
>  libstdc++-v3/include/std/ranges                   |  2 +-
>  libstdc++-v3/testsuite/std/ranges/istream_view.cc | 12 ++++++++++++
>  2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
> index df74ac9dc19..d791e15d096 100644
> --- a/libstdc++-v3/include/std/ranges
> +++ b/libstdc++-v3/include/std/ranges
> @@ -1232,7 +1232,7 @@ namespace views::__adaptor
>         _M_get(const _Range&) const
>         {
>           __glibcxx_assert(false);
> -         return {};
> +         __builtin_unreachable();
>         }
>
>         constexpr void
> diff --git a/libstdc++-v3/testsuite/std/ranges/istream_view.cc b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> index 369790e89e5..2f15f787250 100644
> --- a/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> +++ b/libstdc++-v3/testsuite/std/ranges/istream_view.cc
> @@ -83,6 +83,17 @@ test04()
>    static_assert(!std::forward_iterator<It>);
>  }
>
> +void
> +test05()
> +{
> +  // PR libstdc++/101231
> +  auto words = std::istringstream{"42"};
> +  auto is = ranges::istream_view<int>(words);
> +  auto r = is | views::filter([](auto) { return true; });
> +  for (auto x : r)
> +    ;
> +}
> +
>  int
>  main()
>  {
> @@ -90,4 +101,5 @@ main()
>    test02();
>    test03();
>    test04();
> +  test05();
>  }
> --
> 2.32.0.170.gd486ca60a5
>

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

end of thread, other threads:[~2021-07-15 19:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-13 19:07 [PATCH] libstdc++: invalid default init in _CachedPosition [PR101231] Patrick Palka
2021-07-15 19:58 ` 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).