From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 3D8BE385828D; Wed, 3 Aug 2022 12:30:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3D8BE385828D MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r10-10929] libstdc++: Remove precondition checks from ranges::subrange X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-10 X-Git-Oldrev: 18eecb8c4a97716d4bc4890b05c91f172fadc7b3 X-Git-Newrev: 45a31caac6dce9c08fcbdb2436d8ec6dca4e5f3c Message-Id: <20220803123042.3D8BE385828D@sourceware.org> Date: Wed, 3 Aug 2022 12:30:42 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Aug 2022 12:30:42 -0000 https://gcc.gnu.org/g:45a31caac6dce9c08fcbdb2436d8ec6dca4e5f3c commit r10-10929-g45a31caac6dce9c08fcbdb2436d8ec6dca4e5f3c Author: Jonathan Wakely Date: Tue Jun 15 15:07:25 2021 +0100 libstdc++: Remove precondition checks from ranges::subrange The assertion in the subrange constructor causes semantic changes, because the call to ranges::distance performs additional operations that are not part of the constructor's specification. That will fail to compile if the iterator is move-only, because the argument to ranges::distance is passed by value. It will modify the subrange if the iterator is not a forward iterator, because incrementing the copy also affects the _M_begin member. Those problems could be prevented by using if-constexpr to only do the assertion for copyable forward iterators, but the call to ranges::distance can also prevent the constructor being usable in constant expressions. If the member initializers are usable in constant expressions, but iterator increments of equality comparisons are not, then the checks done by __glibcxx_assert might make constant evaluation fail. This change removes the assertion. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/std/ranges (subrange(i, s, n)): Remove assertion. * testsuite/std/ranges/subrange/constexpr.cc: New test. (cherry picked from commit a88fc03ba7e52d9a072f25d42bb9619fedb7892e) Diff: --- libstdc++-v3/include/std/ranges | 2 -- .../testsuite/std/ranges/subrange/constexpr.cc | 26 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index faa55ebceb7..2ff8dbd53ed 100644 --- a/libstdc++-v3/include/std/ranges +++ b/libstdc++-v3/include/std/ranges @@ -264,8 +264,6 @@ namespace ranges requires (_Kind == subrange_kind::sized) : _M_begin(std::move(__i)), _M_end(__s) { - using __detail::__to_unsigned_like; - __glibcxx_assert(__n == __to_unsigned_like(ranges::distance(__i, __s))); if constexpr (_S_store_size) _M_size._M_size = __n; } diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc b/libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc new file mode 100644 index 00000000000..f5bc52bef84 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc @@ -0,0 +1,26 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +#include + +struct iterator +{ + using difference_type = int; + + int i; + + int operator*() const { return i; } + + // These are intentionally not constexpr: + iterator& operator++() { ++i; return *this; } + iterator operator++(int) { return {i++}; } + bool operator==(const iterator& it) const { return i == it.i; } +}; + +constexpr iterator begin(1), end(2); + +using std::ranges::subrange; +using std::ranges::subrange_kind; + +// This used to fail due to using operator++ and operator== in an assertion: +constexpr subrange s(begin, end, 1);