Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7dbfe40

Browse files
philnik777tru
authored andcommitted
[libc++] Disable cv-qualified arithmetic hash specializations (#155786)
#140407 accidentally enabled `hash` for cv-qualified types. This patch disables these specializations again. (cherry picked from commit 8a65c4f)
1 parent ef3a6bd commit 7dbfe40

File tree

7 files changed

+67
-4
lines changed

7 files changed

+67
-4
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ set(files
874874
__type_traits/is_trivially_relocatable.h
875875
__type_traits/is_unbounded_array.h
876876
__type_traits/is_union.h
877+
__type_traits/is_unqualified.h
877878
__type_traits/is_unsigned.h
878879
__type_traits/is_valid_expansion.h
879880
__type_traits/is_void.h

libcxx/include/__functional/hash.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <__type_traits/is_enum.h>
2222
#include <__type_traits/is_floating_point.h>
2323
#include <__type_traits/is_integral.h>
24+
#include <__type_traits/is_unqualified.h>
2425
#include <__type_traits/underlying_type.h>
2526
#include <__utility/pair.h>
2627
#include <__utility/swap.h>
@@ -355,25 +356,30 @@ struct __hash_impl {
355356
};
356357

357358
template <class _Tp>
358-
struct __hash_impl<_Tp, __enable_if_t<is_enum<_Tp>::value> > : __unary_function<_Tp, size_t> {
359+
struct __hash_impl<_Tp, __enable_if_t<is_enum<_Tp>::value && __is_unqualified_v<_Tp> > >
360+
: __unary_function<_Tp, size_t> {
359361
_LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
360362
using type = __underlying_type_t<_Tp>;
361363
return hash<type>()(static_cast<type>(__v));
362364
}
363365
};
364366

365367
template <class _Tp>
366-
struct __hash_impl<_Tp, __enable_if_t<is_integral<_Tp>::value && (sizeof(_Tp) <= sizeof(size_t))> >
368+
struct __hash_impl<
369+
_Tp,
370+
__enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) <= sizeof(size_t))> >
367371
: __unary_function<_Tp, size_t> {
368372
_LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT { return static_cast<size_t>(__v); }
369373
};
370374

371375
template <class _Tp>
372-
struct __hash_impl<_Tp, __enable_if_t<is_integral<_Tp>::value && (sizeof(_Tp) > sizeof(size_t))> >
376+
struct __hash_impl<_Tp,
377+
__enable_if_t<is_integral<_Tp>::value && __is_unqualified_v<_Tp> && (sizeof(_Tp) > sizeof(size_t))> >
373378
: __scalar_hash<_Tp> {};
374379

375380
template <class _Tp>
376-
struct __hash_impl<_Tp, __enable_if_t<is_floating_point<_Tp>::value> > : __scalar_hash<_Tp> {
381+
struct __hash_impl<_Tp, __enable_if_t<is_floating_point<_Tp>::value && __is_unqualified_v<_Tp> > >
382+
: __scalar_hash<_Tp> {
377383
_LIBCPP_HIDE_FROM_ABI size_t operator()(_Tp __v) const _NOEXCEPT {
378384
// -0.0 and 0.0 should return same hash
379385
if (__v == 0.0f)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___TYPE_TRAITS_IS_UNQUALIFIED_H
10+
#define _LIBCPP___TYPE_TRAITS_IS_UNQUALIFIED_H
11+
12+
#include <__config>
13+
14+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
15+
# pragma GCC system_header
16+
#endif
17+
18+
_LIBCPP_BEGIN_NAMESPACE_STD
19+
20+
template <class _Tp>
21+
inline const bool __is_unqualified_v = __is_same(_Tp, __remove_cvref(_Tp));
22+
23+
_LIBCPP_END_NAMESPACE_STD
24+
25+
#endif // _LIBCPP___TYPE_TRAITS_IS_UNQUALIFIED_H

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ module std_core [system] {
336336
header "__type_traits/is_union.h"
337337
export std_core.type_traits.integral_constant
338338
}
339+
module is_unqualified { header "__type_traits/is_unqualified.h" }
339340
module is_unsigned {
340341
header "__type_traits/is_unsigned.h"
341342
export std_core.type_traits.integral_constant

libcxx/test/std/utilities/function.objects/unord.hash/enum.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
#include "test_macros.h"
2323

24+
#if TEST_STD_VER >= 11
25+
# include "poisoned_hash_helper.h"
26+
#endif
27+
2428
enum class Colors { red, orange, yellow, green, blue, indigo, violet };
2529
enum class Cardinals { zero, one, two, three, five=5 };
2630
enum class LongColors : short { red, orange, yellow, green, blue, indigo, violet };
@@ -33,6 +37,12 @@ template <class T>
3337
void
3438
test()
3539
{
40+
#if TEST_STD_VER >= 11
41+
test_hash_disabled<const T>();
42+
test_hash_disabled<volatile T>();
43+
test_hash_disabled<const volatile T>();
44+
#endif
45+
3646
typedef std::hash<T> H;
3747
#if TEST_STD_VER <= 17
3848
static_assert((std::is_same<typename H::argument_type, T>::value), "");

libcxx/test/std/utilities/function.objects/unord.hash/floating.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@
2727

2828
#include "test_macros.h"
2929

30+
#if TEST_STD_VER >= 11
31+
# include "poisoned_hash_helper.h"
32+
#endif
33+
3034
template <class T>
3135
void
3236
test()
3337
{
38+
#if TEST_STD_VER >= 11
39+
test_hash_disabled<const T>();
40+
test_hash_disabled<volatile T>();
41+
test_hash_disabled<const volatile T>();
42+
#endif
43+
3444
typedef std::hash<T> H;
3545
#if TEST_STD_VER <= 17
3646
static_assert((std::is_same<typename H::argument_type, T>::value), "");

libcxx/test/std/utilities/function.objects/unord.hash/integral.pass.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,20 @@
2626

2727
#include "test_macros.h"
2828

29+
#if TEST_STD_VER >= 11
30+
# include "poisoned_hash_helper.h"
31+
#endif
32+
2933
template <class T>
3034
void
3135
test()
3236
{
37+
#if TEST_STD_VER >= 11
38+
test_hash_disabled<const T>();
39+
test_hash_disabled<volatile T>();
40+
test_hash_disabled<const volatile T>();
41+
#endif
42+
3343
typedef std::hash<T> H;
3444
#if TEST_STD_VER <= 17
3545
static_assert((std::is_same<typename H::argument_type, T>::value), "");

0 commit comments

Comments
 (0)