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 [216.205.24.124]) by sourceware.org (Postfix) with ESMTP id 838D1382C41F for ; Thu, 24 Jun 2021 13:10:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 838D1382C41F 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-99-FqgLgYDGOk-fnU9E79U7IQ-1; Thu, 24 Jun 2021 09:10:05 -0400 X-MC-Unique: FqgLgYDGOk-fnU9E79U7IQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 163FE8042F3; Thu, 24 Jun 2021 13:10:04 +0000 (UTC) Received: from localhost (unknown [10.33.36.175]) by smtp.corp.redhat.com (Postfix) with ESMTP id A3CE15D6D7; Thu, 24 Jun 2021 13:10:03 +0000 (UTC) Date: Thu, 24 Jun 2021 14:10:02 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement LWG 2762 for std::unique_ptr::operator* Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="GYJRSHJbtPl9MRkR" Content-Disposition: inline X-Spam-Status: No, score=-14.0 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: 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: Thu, 24 Jun 2021 13:10:09 -0000 --GYJRSHJbtPl9MRkR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline The LWG issue proposes to add a conditional noexcept-specifier to std::unique_ptr's dereference operator. The issue is currently in Tentatively Ready status, but even if it isn't voted into the draft, we can do it as a conforming extensions. This commit also adds a similar noexcept-specifier to operator[] for the unique_ptr partial specialization. Also ensure that all dereference operators for shared_ptr are noexcept, and adds tests for the std::optional accessors modified by the issue, which were already noexcept in our implementation. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/shared_ptr_base.h (__shared_ptr_access::operator[]): Add noexcept. * include/bits/unique_ptr.h (unique_ptr::operator*): Add conditional noexcept as per LWG 2762. * testsuite/20_util/shared_ptr/observers/array.cc: Check that dereferencing cannot throw. * testsuite/20_util/shared_ptr/observers/get.cc: Likewise. * testsuite/20_util/optional/observers/lwg2762.cc: New test. * testsuite/20_util/unique_ptr/lwg2762.cc: New test. Tested powerpc64le-linux. Committed to trunk. --GYJRSHJbtPl9MRkR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 17bc3848e065c0980523e1a1592f2f03b24b4f1c Author: Jonathan Wakely Date: Thu Jun 24 12:56:20 2021 libstdc++: Implement LWG 2762 for std::unique_ptr::operator* The LWG issue proposes to add a conditional noexcept-specifier to std::unique_ptr's dereference operator. The issue is currently in Tentatively Ready status, but even if it isn't voted into the draft, we can do it as a conforming extensions. This commit also adds a similar noexcept-specifier to operator[] for the unique_ptr partial specialization. Also ensure that all dereference operators for shared_ptr are noexcept, and adds tests for the std::optional accessors modified by the issue, which were already noexcept in our implementation. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: * include/bits/shared_ptr_base.h (__shared_ptr_access::operator[]): Add noexcept. * include/bits/unique_ptr.h (unique_ptr::operator*): Add conditional noexcept as per LWG 2762. * testsuite/20_util/shared_ptr/observers/array.cc: Check that dereferencing cannot throw. * testsuite/20_util/shared_ptr/observers/get.cc: Likewise. * testsuite/20_util/optional/observers/lwg2762.cc: New test. * testsuite/20_util/unique_ptr/lwg2762.cc: New test. diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index eb9ad23ba1e..5be935d174d 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -1035,7 +1035,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif element_type& - operator[](ptrdiff_t __i) const + operator[](ptrdiff_t __i) const noexcept { __glibcxx_assert(_M_get() != nullptr); __glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value); diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h index 6e5537536e8..1781fe15649 100644 --- a/libstdc++-v3/include/bits/unique_ptr.h +++ b/libstdc++-v3/include/bits/unique_ptr.h @@ -402,7 +402,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Dereference the stored pointer. typename add_lvalue_reference::type - operator*() const + operator*() const noexcept(noexcept(*std::declval())) { __glibcxx_assert(get() != pointer()); return *get(); @@ -655,6 +655,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Access an element of owned array. typename std::add_lvalue_reference::type operator[](size_t __i) const + noexcept(noexcept(std::declval()[std::declval()])) { __glibcxx_assert(get() != pointer()); return get()[__i]; diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc b/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc new file mode 100644 index 00000000000..a0cf0bc19a0 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc @@ -0,0 +1,21 @@ +// { dg-do compile { target c++17 } } + +// LWG 2762 adds noexcept to operator-> and operator* +#include + +struct S +{ + void can_throw(); + void cannot_throw() noexcept; +}; + +static_assert( ! noexcept(std::declval&>()->can_throw()) ); +static_assert( noexcept(std::declval&>()->cannot_throw()) ); + +static_assert( noexcept(std::declval&>().operator->()) ); +static_assert( noexcept(std::declval&>().operator->()) ); + +static_assert( noexcept(*std::declval&>()) ); +static_assert( noexcept(*std::declval&>()) ); +static_assert( noexcept(*std::declval&&>()) ); +static_assert( noexcept(*std::declval&&>()) ); diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc index f6acb1fe74a..7fd6c01f9c4 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc @@ -34,6 +34,7 @@ test01() A * const a = new A[2]; const std::shared_ptr p(a); VERIFY( p.get() == a ); + static_assert( noexcept(p.get()), "non-throwing" ); } // get @@ -43,6 +44,7 @@ test02() A * const a = new A[2]; const std::shared_ptr p(a); VERIFY( p.get() == a ); + static_assert( noexcept(p.get()), "non-throwing" ); } // operator[] @@ -52,6 +54,7 @@ test03() A * const a = new A[2]; const std::shared_ptr p(a); VERIFY( &p[0] == a ); + static_assert( noexcept(p[0]), "non-throwing" ); } // operator[] @@ -61,6 +64,7 @@ test04() A * const a = new A[2]; const std::shared_ptr p(a); VERIFY( &p[0] == a ); + static_assert( noexcept(p[0]), "non-throwing" ); } int diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc index cd1282ba20c..6f2cb9fee83 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc @@ -37,6 +37,7 @@ test01() A * const a = new A; const std::shared_ptr p(a); VERIFY( p.get() == a ); + static_assert( noexcept(p.get()), "non-throwing" ); } // operator* @@ -46,6 +47,7 @@ test02() A * const a = new A; const std::shared_ptr p(a); VERIFY( &*p == a ); + static_assert( noexcept(*p), "non-throwing" ); } // operator-> @@ -55,6 +57,7 @@ test03() A * const a = new A; const std::shared_ptr p(a); VERIFY( &p->i == &a->i ); + static_assert( noexcept(p->i), "non-throwing" ); } void @@ -67,7 +70,7 @@ test04() #endif } -int +int main() { test01(); diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc new file mode 100644 index 00000000000..3cc2ea6b87d --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc @@ -0,0 +1,43 @@ +// { dg-do compile { target c++11 } } +#include + +// 2762. unique_ptr operator*() should be noexcept +static_assert( noexcept(*std::declval>()), "LWG 2762" ); + +template +struct deleter +{ + struct pointer + { + int& operator*() && noexcept(B); // this is used by unique_ptr + int& operator*() const& = delete; // this should not be + + int& operator[](std::size_t) && noexcept(B); // this is used by unique_ptr + int& operator[](std::size_t) const& = delete; // should not be used + int& operator[](int) && = delete; // should not be used + int& operator[](double) && = delete; // should not be used + + int* operator->() noexcept(false); // noexcept here doesn't affect anything + + // Needed for NullablePointer requirements + pointer(int* = nullptr); + bool operator==(const pointer&) const noexcept; + bool operator!=(const pointer&) const noexcept; + }; + + void operator()(pointer) const noexcept { } +}; + +template + using UPtr = std::unique_ptr>; + +// noexcept-specifier depends on the pointer type +static_assert( noexcept(*std::declval&>()), "" ); +static_assert( ! noexcept(*std::declval&>()), "" ); + +// This has always been required, even in C++11. +static_assert( noexcept(std::declval&>().operator->()), "" ); + +// This is not required by the standard +static_assert( noexcept(std::declval&>()[0]), "" ); +static_assert( ! noexcept(std::declval&>()[0]), "" ); --GYJRSHJbtPl9MRkR--