From patchwork Tue Jun 15 18:27:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1492410 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=K5s6Nz/E; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G4H1t6ZWjz9sWX for ; Wed, 16 Jun 2021 04:30:42 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B1943839C56 for ; Tue, 15 Jun 2021 18:30:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B1943839C56 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1623781839; bh=EGza4Rq+fbl9xoQEqJuM6ZogrEOgLTcW3Xi50zlu424=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=K5s6Nz/EWNex2orHjlrDEW68ZRjYYYoFiUz1MAX9YS4v38lAFE0jQvfiHMv+oYoDH VxO7dfSHswT/GTS6lTzq63mbZv3gnxKITXgyvqbYI5hJYDmobEqfNUNp3HiMqk1SI/ 7mBnsUoVLtRqUS2QTxCj8t7MU5W48Y7wnUqsEKvw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 6C397383B80E for ; Tue, 15 Jun 2021 18:28:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6C397383B80E Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-600-Z9ZWDbWSO16Af-CHQ-e4fg-1; Tue, 15 Jun 2021 14:27:58 -0400 X-MC-Unique: Z9ZWDbWSO16Af-CHQ-e4fg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7D760107ACF6; Tue, 15 Jun 2021 18:27:57 +0000 (UTC) Received: from localhost (unknown [10.33.36.175]) by smtp.corp.redhat.com (Postfix) with ESMTP id 23FDA19C66; Tue, 15 Jun 2021 18:27:57 +0000 (UTC) Date: Tue, 15 Jun 2021 19:27:56 +0100 To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Remove precondition checks from ranges::subrange Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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. Additionally, a new typedef is introduced to simplify the declarations using __make_unsigned_like_t on the iterator's difference type. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/ranges_util.h (subrange): Add __size_type typedef and use it to simplify declarations. (subrange(i, s, n)): Remove assertion. * testsuite/std/ranges/subrange/constexpr.cc: New test. Tested powerpc64le-linux. Committed to trunk. commit a88fc03ba7e52d9a072f25d42bb9619fedb7892e Author: Jonathan Wakely Date: Tue Jun 15 15:07:25 2021 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. Additionally, a new typedef is introduced to simplify the declarations using __make_unsigned_like_t on the iterator's difference type. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/ranges_util.h (subrange): Add __size_type typedef and use it to simplify declarations. (subrange(i, s, n)): Remove assertion. * testsuite/std/ranges/subrange/constexpr.cc: New test. diff --git a/libstdc++-v3/include/bits/ranges_util.h b/libstdc++-v3/include/bits/ranges_util.h index b73fc121e0f..02561ee63f9 100644 --- a/libstdc++-v3/include/bits/ranges_util.h +++ b/libstdc++-v3/include/bits/ranges_util.h @@ -205,15 +205,18 @@ namespace ranges _It _M_begin = _It(); [[no_unique_address]] _Sent _M_end = _Sent(); + using __size_type + = __detail::__make_unsigned_like_t>; + template struct _Size { }; template struct _Size<_Tp, true> - { __detail::__make_unsigned_like_t<_Tp> _M_size; }; + { _Tp _M_size; }; - [[no_unique_address]] _Size> _M_size = {}; + [[no_unique_address]] _Size<__size_type> _M_size = {}; public: subrange() = default; @@ -226,12 +229,10 @@ namespace ranges constexpr subrange(__detail::__convertible_to_non_slicing<_It> auto __i, _Sent __s, - __detail::__make_unsigned_like_t> __n) + __size_type __n) 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; } @@ -258,8 +259,7 @@ namespace ranges requires __detail::__convertible_to_non_slicing, _It> && convertible_to, _Sent> constexpr - subrange(_Rng&& __r, - __detail::__make_unsigned_like_t> __n) + subrange(_Rng&& __r, __size_type __n) requires (_Kind == subrange_kind::sized) : subrange{ranges::begin(__r), ranges::end(__r), __n} { } @@ -267,9 +267,9 @@ namespace ranges template<__detail::__not_same_as _PairLike> requires __detail::__pair_like_convertible_from<_PairLike, const _It&, const _Sent&> - constexpr - operator _PairLike() const - { return _PairLike(_M_begin, _M_end); } + constexpr + operator _PairLike() const + { return _PairLike(_M_begin, _M_end); } constexpr _It begin() const requires copyable<_It> @@ -283,7 +283,7 @@ namespace ranges constexpr bool empty() const { return _M_begin == _M_end; } - constexpr __detail::__make_unsigned_like_t> + constexpr __size_type size() const requires (_Kind == subrange_kind::sized) { if constexpr (_S_store_size) 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);