libstdc++: Implement LWG 2762 for std::unique_ptr::operator*
authorJonathan Wakely <jwakely@redhat.com>
Thu, 24 Jun 2021 11:56:20 +0000 (12:56 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 24 Jun 2021 13:04:02 +0000 (14:04 +0100)
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.

libstdc++-v3/include/bits/shared_ptr_base.h
libstdc++-v3/include/bits/unique_ptr.h
libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc
libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc [new file with mode: 0644]

index eb9ad23..5be935d 100644 (file)
@@ -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);
index 6e55375..1781fe1 100644 (file)
@@ -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 (file)
index 0000000..a0cf0bc
--- /dev/null
@@ -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>&&>()) );
index f6acb1f..7fd6c01 100644 (file)
@@ -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
index cd1282b..6f2cb9f 100644 (file)
@@ -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 (file)
index 0000000..3cc2ea6
--- /dev/null
@@ -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]), "" );