[libcxx] r300516 - Allow a standard library to implement conditional noexcept for optional and unique_ptr hash functions.

Billy Robert O'Neal III via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 17 17:19:50 PDT 2017


Author: bion
Date: Mon Apr 17 19:19:50 2017
New Revision: 300516

URL: http://llvm.org/viewvc/llvm-project?rev=300516&view=rev
Log:
Allow a standard library to implement conditional noexcept for optional and unique_ptr hash functions.

These tests were unconditionally asserting that optional and unique_ptr declare throwing hashes, but MSVC++ implements conditional noexcept forwarding that of the underlying hash function. As a result we were failing these tests but there's nothing forbidding strengthening noexcept in that way.

Changed the ASSERT_NOT_NOEXCEPT asserts to use types which themselves have non-noexcept hash functions.

Modified:
    libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp
    libcxx/trunk/test/std/utilities/optional/optional.hash/hash.pass.cpp

Modified: libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp?rev=300516&r1=300515&r2=300516&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/memory/util.smartptr/util.smartptr.hash/hash_unique_ptr.pass.cpp Mon Apr 17 19:19:50 2017
@@ -50,7 +50,7 @@ namespace std {
 
 template <class T>
 struct hash<::min_pointer<T, std::integral_constant<size_t, 1>>> {
-  size_t operator()(::min_pointer<T, std::integral_constant<size_t, 1>> p) const noexcept(false) {
+  size_t operator()(::min_pointer<T, std::integral_constant<size_t, 1>> p) const TEST_NOEXCEPT_FALSE {
     if (!p) return 0;
     return std::hash<T*>{}(std::addressof(*p));
   }
@@ -67,12 +67,16 @@ int main()
     int* ptr = new int;
     std::unique_ptr<int> p(ptr);
     std::hash<std::unique_ptr<int> > f;
-    ASSERT_NOT_NOEXCEPT(f(p));
     std::size_t h = f(p);
     assert(h == std::hash<int*>()(ptr));
   }
 #if TEST_STD_VER >= 11
   {
+    std::unique_ptr<int, PointerDeleter<int, 1>> pThrowingHash;
+    std::hash<std::unique_ptr<int, PointerDeleter<int, 1>>> fThrowingHash;
+    ASSERT_NOT_NOEXCEPT(fThrowingHash(pThrowingHash));
+  }
+  {
     test_enabled_with_deleter<int, Deleter<int>>();
     test_enabled_with_deleter<int[], Deleter<int[]>>();
     test_enabled_with_deleter<int, CopyDeleter<int>>();

Modified: libcxx/trunk/test/std/utilities/optional/optional.hash/hash.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/optional/optional.hash/hash.pass.cpp?rev=300516&r1=300515&r2=300516&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/optional/optional.hash/hash.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/optional/optional.hash/hash.pass.cpp Mon Apr 17 19:19:50 2017
@@ -26,7 +26,7 @@ namespace std {
 
 template <>
 struct hash<B> {
-  size_t operator()(B const&) noexcept(false) { return 0; }
+  size_t operator()(B const&) TEST_NOEXCEPT_FALSE { return 0; }
 };
 
 }
@@ -37,10 +37,16 @@ int main()
     const std::size_t nullopt_hash =
         std::hash<optional<double>>{}(optional<double>{});
 
+
+    {
+        optional<B> opt;
+        ASSERT_NOT_NOEXCEPT(std::hash<optional<B>>()(opt));
+        ASSERT_NOT_NOEXCEPT(std::hash<optional<const B>>()(opt));
+    }
+
     {
         typedef int T;
         optional<T> opt;
-        ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
         opt = 2;
         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
@@ -48,7 +54,6 @@ int main()
     {
         typedef std::string T;
         optional<T> opt;
-        ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
         opt = std::string("123");
         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));
@@ -56,7 +61,6 @@ int main()
     {
         typedef std::unique_ptr<int> T;
         optional<T> opt;
-        ASSERT_NOT_NOEXCEPT(std::hash<optional<T>>()(opt));
         assert(std::hash<optional<T>>{}(opt) == nullopt_hash);
         opt = std::unique_ptr<int>(new int(3));
         assert(std::hash<optional<T>>{}(opt) == std::hash<T>{}(*opt));




More information about the cfe-commits mailing list