From c1b1d7f0bb5e6a30ab7b8bfa1b0f009aaf8db0e4 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Fri, 3 Mar 2017 22:35:58 +0000 Subject: [PATCH] Fix hash requirements check in __hash_table. r296565 attempted to add better diagnostics when an unordered container is instantiated with a hash that doesn't meet the Hash requirements. However I mistakenly checked the wrong set of requirements. Specifically it checked if the hash met the requirements for specializations of std::hash. However these requirements are stricter than the generic Hash requirements. This patch fixes the assertions to only check the Hash requirements. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@296919 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/__hash_table | 7 ++-- include/utility | 11 ++++-- .../missing_hash_specialization.fail.cpp | 38 ++++++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/include/__hash_table b/include/__hash_table index 9d9a29055..216149371 100644 --- a/include/__hash_table +++ b/include/__hash_table @@ -871,16 +871,15 @@ public: template struct __diagnose_hash_table_helper { static constexpr bool __trigger_diagnostics() - _LIBCPP_DIAGNOSE_WARNING(__has_enabled_hash<_Key, _Hash>::value + _LIBCPP_DIAGNOSE_WARNING(__check_hash_requirements<_Key, _Hash>::value && !__invokable<_Hash const&, _Key const&>::value, "the specified hash functor does not provide a const call operator") _LIBCPP_DIAGNOSE_WARNING(is_copy_constructible<_Equal>::value && !__invokable<_Equal const&, _Key const&, _Key const&>::value, "the specified comparator type does not provide a const call operator") { - static_assert(__has_enabled_hash<_Key, _Hash>::value, - "the specified hash functor does not meet the requirements for an " - "enabled hash"); + static_assert(__check_hash_requirements<_Key, _Hash>::value, + "the specified hash does not meet the Hash requirements"); static_assert(is_copy_constructible<_Equal>::value, "the specified comparator is required to be copy constructible"); return true; diff --git a/include/utility b/include/utility index 3452fe119..1f41c0771 100644 --- a/include/utility +++ b/include/utility @@ -1560,14 +1560,19 @@ struct _LIBCPP_TEMPLATE_VIS hash #endif #ifndef _LIBCPP_CXX03_LANG -template > -using __has_enabled_hash = integral_constant::value && +template +using __check_hash_requirements = integral_constant::value && is_move_constructible<_Hash>::value && __invokable_r::value >; +template > +using __has_enabled_hash = integral_constant::value && + is_default_constructible<_Hash>::value +>; + #if _LIBCPP_STD_VER > 14 template using __enable_hash_helper_imp = _Type; diff --git a/test/libcxx/containers/unord/unord.set/missing_hash_specialization.fail.cpp b/test/libcxx/containers/unord/unord.set/missing_hash_specialization.fail.cpp index e182a0322..d682e7453 100644 --- a/test/libcxx/containers/unord/unord.set/missing_hash_specialization.fail.cpp +++ b/test/libcxx/containers/unord/unord.set/missing_hash_specialization.fail.cpp @@ -23,14 +23,48 @@ #include using VT = std::pair; -using Set = std::unordered_set; + +struct BadHashNoCopy { + BadHashNoCopy() = default; + BadHashNoCopy(BadHashNoCopy const&) = delete; + + template + size_t operator()(T const&) const { return 0; } +}; + +struct BadHashNoCall { + +}; + + +struct GoodHashNoDefault { + explicit GoodHashNoDefault(void*) {} + template + size_t operator()(T const&) const { return 0; } +}; int main() { - Set s; // expected-error@__hash_table:* {{the specified hash functor does not meet the requirements for an enabled hash}} + { + using Set = std::unordered_set; + Set s; // expected-error@__hash_table:* {{the specified hash does not meet the Hash requirements}} + // FIXME: It would be great to suppress the below diagnostic all together. // but for now it's sufficient that it appears last. However there is // currently no way to test the order diagnostics are issued. // expected-error@memory:* {{call to implicitly-deleted default constructor of 'std::__1::hash >'}} + } + { + using Set = std::unordered_set; + Set s; // expected-error@__hash_table:* {{the specified hash does not meet the Hash requirements}} + } + { + using Set = std::unordered_set; + Set s; // expected-error@__hash_table:* {{the specified hash does not meet the Hash requirements}} + } + { + using Set = std::unordered_set; + Set s(/*bucketcount*/42, GoodHashNoDefault(nullptr)); + } }