From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A3CBA3858C00 for ; Wed, 20 Jul 2022 23:00:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A3CBA3858C00 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-378-rTPGJltiMNuwABMy2nQm_A-1; Wed, 20 Jul 2022 19:00:12 -0400 X-MC-Unique: rTPGJltiMNuwABMy2nQm_A-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 1A393185A7B2; Wed, 20 Jul 2022 23:00:12 +0000 (UTC) Received: from localhost (unknown [10.33.36.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA52C404754B; Wed, 20 Jul 2022 23:00:11 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed 1/3] libstdc++: Fix minor bugs in std::common_iterator Date: Thu, 21 Jul 2022 00:00:09 +0100 Message-Id: <20220720230011.2732391-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 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_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libstdc++@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++ mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jul 2022 23:00:22 -0000 Tested powerpc64le-linux, pushed to trunk. -- >8 -- The noexcept-specifier for some std::common_iterator constructors was incorrectly using an rvalue as the first argument of std::is_nothrow_assignable_v. This gave the wrong answer for some types, e.g. std::common_iterator, because an rvalue of scalar type cannot be assigned to. Also fix the friend declaration to use the same constraints as on the definition of the class template. G++ fails to diagnose this error, due to PR c++/96830. Finally, the copy constructor was using std::move for its argument in some cases, which should be removed. libstdc++-v3/ChangeLog: * include/bits/stl_iterator.h (common_iterator): Fix incorrect uses of is_nothrow_assignable_v. Fix inconsistent constraints on friend declaration. Do not move argument in copy constructor. * testsuite/24_iterators/common_iterator/1.cc: Check for noexcept constructibnle/assignable. --- libstdc++-v3/include/bits/stl_iterator.h | 11 +++++---- .../24_iterators/common_iterator/1.cc | 23 ++++++++++++++++++- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h index 049cb02a4c4..fd0ae3aa771 100644 --- a/libstdc++-v3/include/bits/stl_iterator.h +++ b/libstdc++-v3/include/bits/stl_iterator.h @@ -1838,7 +1838,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _S_noexcept1() { if constexpr (is_trivially_default_constructible_v<_Tp>) - return is_nothrow_assignable_v<_Tp, _Up>; + return is_nothrow_assignable_v<_Tp&, _Up>; else return is_nothrow_constructible_v<_Tp, _Up>; } @@ -1932,14 +1932,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (_M_index == 0) { if constexpr (is_trivially_default_constructible_v<_It>) - _M_it = std::move(__x._M_it); + _M_it = __x._M_it; else std::construct_at(std::__addressof(_M_it), __x._M_it); } else if (_M_index == 1) { if constexpr (is_trivially_default_constructible_v<_Sent>) - _M_sent = std::move(__x._M_sent); + _M_sent = __x._M_sent; else std::construct_at(std::__addressof(_M_sent), __x._M_sent); } @@ -1964,8 +1964,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator=(const common_iterator<_It2, _Sent2>& __x) noexcept(is_nothrow_constructible_v<_It, const _It2&> && is_nothrow_constructible_v<_Sent, const _Sent2&> - && is_nothrow_assignable_v<_It, const _It2&> - && is_nothrow_assignable_v<_Sent, const _Sent2&>) + && is_nothrow_assignable_v<_It&, const _It2&> + && is_nothrow_assignable_v<_Sent&, const _Sent2&>) { switch(_M_index << 2 | __x._M_index) { @@ -2164,6 +2164,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION private: template _Sent2> + requires (!same_as<_It2, _Sent2>) && copyable<_It2> friend class common_iterator; constexpr bool _M_has_value() const noexcept { return _M_index < 2; } diff --git a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc index 365ee89c02e..ec4a86c862a 100644 --- a/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc +++ b/libstdc++-v3/testsuite/24_iterators/common_iterator/1.cc @@ -27,15 +27,30 @@ test01() using I = std::common_iterator; static_assert( std::is_default_constructible_v ); static_assert( std::is_copy_constructible_v ); + static_assert( std::is_move_constructible_v ); static_assert( std::is_copy_assignable_v ); + static_assert( std::is_move_assignable_v ); static_assert( std::is_constructible_v ); static_assert( std::is_constructible_v ); - struct sentinel { operator int*() const { return nullptr; } }; + static_assert( std::is_nothrow_copy_constructible_v ); // GCC extension + static_assert( std::is_nothrow_move_constructible_v ); // GCC extension + static_assert( std::is_nothrow_copy_assignable_v ); // GCC extension + static_assert( std::is_nothrow_move_assignable_v ); // GCC extension + + struct sentinel { operator int*() const noexcept { return nullptr; } }; using K = std::common_iterator; static_assert( std::is_constructible_v ); static_assert( std::is_assignable_v ); + static_assert( std::is_nothrow_assignable_v ); // GCC extension + + struct sentinel_throwing { operator int*() const { return nullptr; } }; + using K_throwing = std::common_iterator; + // Conversion is noexcept(false) + static_assert( ! std::is_nothrow_assignable_v ); + + struct sentinel2 { const int* p; @@ -46,6 +61,12 @@ test01() using J = std::common_iterator; static_assert( std::is_constructible_v ); static_assert( std::is_convertible_v ); + + static_assert( std::is_constructible_v ); + static_assert( std::is_convertible_v ); + + // Constructor is noexcept(false) + static_assert( ! std::is_nothrow_constructible_v ); } void -- 2.34.3