public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Avoid hard error in ranges::unique_copy [PR100770]
Date: Wed, 26 May 2021 12:45:41 -0400	[thread overview]
Message-ID: <20210526164541.1701738-1-ppalka@redhat.com> (raw)

When input_iterator<_Out> isn't satisfied, we need to avoid substituting
into iter_value_t<_Out> because the latter isn't necessarily
well-formed in that case.  To that end, this patch rewrites the
problematic condition in ranges::unique_copy into a nested requirement
which has the correct short-circuiting behavior.

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

libstdc++-v3/ChangeLog:

	* include/bits/ranges_algo.h (__unique_copy_fn::operator()):
	Rewrite the constexpr if conjunction into a nested requirement
	for sake of short-circuiting.
	* testsuite/25_algorithms/unique_copy/100770.cc: New test.
---
 libstdc++-v3/include/bits/ranges_algo.h       |  5 +--
 .../25_algorithms/unique_copy/100770.cc       | 33 +++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc

diff --git a/libstdc++-v3/include/bits/ranges_algo.h b/libstdc++-v3/include/bits/ranges_algo.h
index cda3042c11f..ce4719d5642 100644
--- a/libstdc++-v3/include/bits/ranges_algo.h
+++ b/libstdc++-v3/include/bits/ranges_algo.h
@@ -1432,8 +1432,9 @@ namespace ranges
 		}
 	    return {__next, std::move(++__result)};
 	  }
-	else if constexpr (input_iterator<_Out>
-			   && same_as<iter_value_t<_Iter>, iter_value_t<_Out>>)
+	else if constexpr (requires { requires (input_iterator<_Out>
+						&& same_as<iter_value_t<_Iter>,
+							   iter_value_t<_Out>>); })
 	  {
 	    *__result = *__first;
 	    while (++__first != __last)
diff --git a/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc b/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc
new file mode 100644
index 00000000000..ea49eb46a18
--- /dev/null
+++ b/libstdc++-v3/testsuite/25_algorithms/unique_copy/100770.cc
@@ -0,0 +1,33 @@
+// Copyright (C) 2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+
+// PR libstdc++/100770
+
+#include <algorithm>
+#include <sstream>
+
+int main() {
+  std::istringstream str("42 42 42");
+  extern std::ostream out;
+  std::ranges::unique_copy(
+    std::istream_iterator<int>(str),
+    std::istream_iterator<int>(),
+    std::ostream_iterator<int>(out, " "));
+}
-- 
2.32.0.rc0


             reply	other threads:[~2021-05-26 16:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-26 16:45 Patrick Palka [this message]
2021-05-26 17:27 ` Tim Song
2021-05-26 18:43   ` Patrick Palka
2021-05-26 19:07     ` Tim Song
2021-05-27 13:50       ` Patrick Palka
2021-06-03 11:54         ` Jonathan Wakely
2021-05-26 19:55     ` Jonathan Wakely
2021-05-27  0:38       ` Tim Song
2021-05-26 17:28 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210526164541.1701738-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).