]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: invalid default init in _CachedPosition [PR101231]
authorPatrick Palka <ppalka@redhat.com>
Fri, 16 Jul 2021 13:44:42 +0000 (09:44 -0400)
committerPatrick Palka <ppalka@redhat.com>
Tue, 31 May 2022 18:38:14 +0000 (14:38 -0400)
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.

PR libstdc++/103904
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.

(cherry picked from commit 1af937eb6246ad7f63ebff03590e9eede33aca81)

libstdc++-v3/include/std/ranges
libstdc++-v3/testsuite/std/ranges/istream_view.cc

index 4c08de39c1d677bf55e265126c8c48099f2f6206..17018a5616c96ceb25a54d0b58bd10d2176b1cbc 100644 (file)
@@ -1356,7 +1356,7 @@ namespace views
        _M_get(const _Range&) const
        {
          __glibcxx_assert(false);
-         return {};
+         __builtin_unreachable();
        }
 
        constexpr void
index 77df686aa4a7dc931bbb19864b3a2da1b2773f26..a43a4c851f8c7c0115744bff520fae017f0529e7 100644 (file)
@@ -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)
+    ;
+}
+
 void
 test06()
 {
@@ -99,5 +110,6 @@ main()
   test02();
   test03();
   test04();
+  test05();
   test06();
 }