Diagnose non-const-callable hash functions and comparators

llvm-svn: 291969
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 04333f9bda707d53a3ddf582f477baaaaeed016b
diff --git a/include/__hash_table b/include/__hash_table
index e082e6c..6d6a6a1 100644
--- a/include/__hash_table
+++ b/include/__hash_table
@@ -18,6 +18,7 @@
 #include <algorithm>
 #include <cmath>
 #include <utility>
+#include <type_traits>
 
 #include <__undef_min_max>
 
@@ -38,6 +39,15 @@
 struct __hash_value_type;
 #endif
 
+template <class _Key, class _Cp, class _Hash,
+          bool =  is_empty<_Hash>::value && !__libcpp_is_final<_Hash>::value>
+class __unordered_map_hasher;
+
+template <class _Key, class _Cp, class _Pred,
+          bool = is_empty<_Pred>::value && !__libcpp_is_final<_Pred>::value
+         >
+class __unordered_map_equal;
+
 #ifndef _LIBCPP_CXX03_LANG
 template <class _Tp>
 struct __is_hash_value_type_imp : false_type {};
@@ -856,6 +866,29 @@
     template <class> friend class __hash_map_node_destructor;
 };
 
+
+#ifndef _LIBCPP_CXX03_LANG
+template <class _Key, class _Hash, class _Equal, class _Alloc>
+struct __diagnose_hash_table_helper {
+  static constexpr bool __trigger_diagnostics()
+  _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Hash const&, _Key const&>::value,
+    "the specified hash functor does not provide a const call operator")
+  _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Equal const&, _Key const&, _Key const&>::value,
+    "the specified comparator type does not provide a const call operator")
+  { return true; }
+};
+
+template <class _Key, class _Value, class _Hash, class _Equal, class _Alloc>
+struct __diagnose_hash_table_helper<
+  __hash_value_type<_Key, _Value>,
+  __unordered_map_hasher<_Key, __hash_value_type<_Key, _Value>, _Hash>,
+  __unordered_map_equal<_Key, __hash_value_type<_Key, _Value>, _Equal>,
+  _Alloc>
+: __diagnose_hash_table_helper<_Key, _Hash, _Equal, _Alloc>
+{
+};
+#endif // _LIBCPP_CXX03_LANG
+
 template <class _Tp, class _Hash, class _Equal, class _Alloc>
 class __hash_table
 {
@@ -1453,6 +1486,9 @@
                  "Predicate must be copy-constructible.");
     static_assert((is_copy_constructible<hasher>::value),
                  "Hasher must be copy-constructible.");
+#ifndef _LIBCPP_CXX03_LANG
+    static_assert(__diagnose_hash_table_helper<_Tp, _Hash, _Equal, _Alloc>::__trigger_diagnostics(), "");
+#endif
     __deallocate_node(__p1_.first().__next_);
 #if _LIBCPP_DEBUG_LEVEL >= 2
     __get_db()->__erase_c(this);
diff --git a/include/__tree b/include/__tree
index 3bc0207..170d4c0 100644
--- a/include/__tree
+++ b/include/__tree
@@ -963,7 +963,7 @@
 template <class _Tp, class _Compare, class _Allocator>
 struct __diagnose_tree_helper {
   static constexpr bool __trigger_diagnostics()
-      _LIBCPP_DIAGNOSE_WARNING(!__is_const_comparable<_Compare, _Tp>::value,
+      _LIBCPP_DIAGNOSE_WARNING(!__invokable<_Compare const&, _Tp const&, _Tp const&>::value,
             "the specified comparator type does not provide a const call operator")
   { return true; }
 };
@@ -973,15 +973,10 @@
     __value_type<_Key, _Value>,
     __map_value_compare<_Key, __value_type<_Key, _Value>, _KeyComp>,
     _Alloc
->
+> : __diagnose_tree_helper<_Key, _KeyComp, _Alloc>
 {
-  static constexpr bool __trigger_diagnostics()
-      _LIBCPP_DIAGNOSE_WARNING(!__is_const_comparable<_KeyComp, _Key>::value,
-            "the specified comparator type does not provide a const call operator")
-  { return true; }
 };
-
-#endif
+#endif // !_LIBCPP_CXX03_LANG
 
 template <class _Tp, class _Compare, class _Allocator>
 class __tree
diff --git a/include/type_traits b/include/type_traits
index 8812d3d..7862955 100644
--- a/include/type_traits
+++ b/include/type_traits
@@ -4715,15 +4715,6 @@
 
 #endif
 
-template <class _Comp, class _ValueType, class = void>
-struct __is_const_comparable : false_type {};
-
-template <class _Comp, class _ValueType>
-struct __is_const_comparable<_Comp, _ValueType, typename __void_t<
-    decltype(_VSTD::declval<_Comp const&>()(_VSTD::declval<_ValueType const&>(),
-                                            _VSTD::declval<_ValueType const&>()))
-  >::type> : true_type {};
-
 _LIBCPP_END_NAMESPACE_STD
 
 #endif  // _LIBCPP_TYPE_TRAITS
diff --git a/include/unordered_map b/include/unordered_map
index 7baf638..3f3808c 100644
--- a/include/unordered_map
+++ b/include/unordered_map
@@ -379,9 +379,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class _Key, class _Cp, class _Hash,
-          bool = is_empty<_Hash>::value && !__libcpp_is_final<_Hash>::value
-         >
+template <class _Key, class _Cp, class _Hash, bool _IsEmpty>
 class __unordered_map_hasher
     : private _Hash
 {
@@ -449,9 +447,7 @@
     __x.swap(__y);
 }
 
-template <class _Key, class _Cp, class _Pred,
-          bool = is_empty<_Pred>::value && !__libcpp_is_final<_Pred>::value
-         >
+template <class _Key, class _Cp, class _Pred, bool _IsEmpty>
 class __unordered_map_equal
     : private _Pred
 {