]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Remove precondition checks from ranges::subrange
authorJonathan Wakely <jwakely@redhat.com>
Tue, 15 Jun 2021 14:07:25 +0000 (15:07 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Wed, 3 Aug 2022 10:34:30 +0000 (11:34 +0100)
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 <jwakely@redhat.com>
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)

libstdc++-v3/include/std/ranges
libstdc++-v3/testsuite/std/ranges/subrange/constexpr.cc [new file with mode: 0644]

index faa55ebceb725f7177a8cf636dcdde46cf2ab2e5..2ff8dbd53ed9a7d70327aacb0d5d6fa6ac93aab0 100644 (file)
@@ -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 (file)
index 0000000..f5bc52b
--- /dev/null
@@ -0,0 +1,26 @@
+// { dg-options "-std=gnu++20" }
+// { dg-do compile { target c++20 } }
+
+#include <ranges>
+
+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<iterator, iterator, subrange_kind::sized> s(begin, end, 1);