public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Implement LWG 2762 for std::unique_ptr::operator*
@ 2021-06-24 13:10 Jonathan Wakely
  2021-06-24 18:53 ` Patrick Palka
  0 siblings, 1 reply; 5+ messages in thread
From: Jonathan Wakely @ 2021-06-24 13:10 UTC (permalink / raw)
  To: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 1128 bytes --]

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<T[], D> 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 <jwakely@redhat.com>

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.


[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 7748 bytes --]

commit 17bc3848e065c0980523e1a1592f2f03b24b4f1c
Author: Jonathan Wakely <jwakely@redhat.com>
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<T[], D> 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 <jwakely@redhat.com>
    
    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<element_type>::type
-      operator*() const
+      operator*() const noexcept(noexcept(*std::declval<pointer>()))
       {
 	__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<element_type>::type
       operator[](size_t __i) const
+      noexcept(noexcept(std::declval<pointer>()[std::declval<size_t&>()]))
       {
 	__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 <optional>
+
+struct S
+{
+  void can_throw();
+  void cannot_throw() noexcept;
+};
+
+static_assert( ! noexcept(std::declval<std::optional<S>&>()->can_throw()) );
+static_assert( noexcept(std::declval<std::optional<S>&>()->cannot_throw()) );
+
+static_assert( noexcept(std::declval<std::optional<S>&>().operator->()) );
+static_assert( noexcept(std::declval<std::optional<int>&>().operator->()) );
+
+static_assert( noexcept(*std::declval<std::optional<int>&>()) );
+static_assert( noexcept(*std::declval<const std::optional<int>&>()) );
+static_assert( noexcept(*std::declval<std::optional<int>&&>()) );
+static_assert( noexcept(*std::declval<const std::optional<int>&&>()) );
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<A[2]> 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<A[]> 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<A[2]> 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<A[]> 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<A> 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<A> 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<A> 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 <memory>
+
+// 2762. unique_ptr operator*() should be noexcept
+static_assert( noexcept(*std::declval<std::unique_ptr<long>>()), "LWG 2762" );
+
+template<bool B>
+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<typename T, bool Nothrow>
+  using UPtr = std::unique_ptr<T, deleter<Nothrow>>;
+
+// noexcept-specifier depends on the pointer type
+static_assert( noexcept(*std::declval<UPtr<int, true>&>()), "" );
+static_assert( ! noexcept(*std::declval<UPtr<int, false>&>()), "" );
+
+// This has always been required, even in C++11.
+static_assert( noexcept(std::declval<UPtr<int, false>&>().operator->()), "" );
+
+// This is not required by the standard
+static_assert( noexcept(std::declval<UPtr<int[], true>&>()[0]), "" );
+static_assert( ! noexcept(std::declval<UPtr<int[], false>&>()[0]), "" );

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-06-28 14:19 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-24 13:10 [committed] libstdc++: Implement LWG 2762 for std::unique_ptr::operator* Jonathan Wakely
2021-06-24 18:53 ` Patrick Palka
2021-06-24 19:29   ` Jonathan Wakely
2021-06-24 21:10   ` Tim Song
2021-06-28 14:19     ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).