From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id CF8A1385DC33; Thu, 24 Jun 2021 13:09:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CF8A1385DC33 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 r12-1778] libstdc++: Implement LWG 2762 for std::unique_ptr::operator* X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: a21dc9d1529b8a8071e36b22b6e8492fc2ce7d5a X-Git-Newrev: 17bc3848e065c0980523e1a1592f2f03b24b4f1c Message-Id: <20210624130952.CF8A1385DC33@sourceware.org> Date: Thu, 24 Jun 2021 13:09:52 +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: Thu, 24 Jun 2021 13:09:52 -0000 https://gcc.gnu.org/g:17bc3848e065c0980523e1a1592f2f03b24b4f1c commit r12-1778-g17bc3848e065c0980523e1a1592f2f03b24b4f1c Author: Jonathan Wakely Date: Thu Jun 24 12:56:20 2021 +0100 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: --- libstdc++-v3/include/bits/shared_ptr_base.h | 2 +- libstdc++-v3/include/bits/unique_ptr.h | 3 +- .../20_util/optional/observers/lwg2762.cc | 21 +++++++++++ .../20_util/shared_ptr/observers/array.cc | 4 ++ .../testsuite/20_util/shared_ptr/observers/get.cc | 5 ++- .../testsuite/20_util/unique_ptr/lwg2762.cc | 43 ++++++++++++++++++++++ 6 files changed, 75 insertions(+), 3 deletions(-) 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]), "" );