From f8136d08c6b1e1d7b922d12b202736f38e21f33c Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Wed, 14 Dec 2016 22:22:38 +0000 Subject: [PATCH] [libcxx] Fix tuple construction/assignment from types derived from tuple/pair/array. Summary: The standard requires tuple have the following constructors: ``` tuple(tuple const&); tuple(tuple &&); tuple(pair const&); tuple(pair &&); tuple(array const&); tuple(array &&); ``` However libc++ implements these as a single constructor with the signature: ``` template ::value>> tuple(TupleLike&&); ``` This causes the constructor to reject types derived from tuple-like types; Unlike if we had all of the concrete overloads, because they cause the derived->base conversion in the signature. This patch fixes this issue by detecting derived types and the tuple-like base they are derived from. It does this by creating an overloaded function with signatures for each of tuple/pair/array and checking if the possibly derived type can convert to any of them. This patch fixes [PR17550]( https://llvm.org/bugs/show_bug.cgi?id=17550) This patch Reviewers: mclow.lists, K-ballo, mpark, EricWF Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D27606 llvm-svn: 289727 --- libcxx/include/__tuple | 64 +++++++-- libcxx/include/tuple | 63 +++++---- libcxx/include/type_traits | 59 ++++++++ .../tuple.assign/derived_from_tuple_like.pass.cpp | 113 ++++++++++++++++ .../tuple.cnstr/derived_from_tuple_like.pass.cpp | 150 +++++++++++++++++++++ libcxx/test/support/propagate_value_category.hpp | 145 ++++++++++++++++++++ 6 files changed, 558 insertions(+), 36 deletions(-) create mode 100644 libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/derived_from_tuple_like.pass.cpp create mode 100644 libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/derived_from_tuple_like.pass.cpp create mode 100644 libcxx/test/support/propagate_value_category.hpp diff --git a/libcxx/include/__tuple b/libcxx/include/__tuple index 775d734..8e6d2bd 100644 --- a/libcxx/include/__tuple +++ b/libcxx/include/__tuple @@ -453,19 +453,57 @@ using tuple_element_t = typename tuple_element <_Ip, _Tp...>::type; #endif // _LIBCPP_HAS_NO_VARIADICS #ifndef _LIBCPP_CXX03_LANG -template -struct __tuple_like_with_size_imp : false_type {}; - -template -struct __tuple_like_with_size_imp - : integral_constant {}; - -template ::type> -using __tuple_like_with_size = __tuple_like_with_size_imp< - __tuple_like<_RawTuple>::value, - tuple_size<_RawTuple>, _ExpectedSize - >; + +template +struct __lookup_result { + using type = _Tp; + static constexpr bool _Success = _Good; + static constexpr size_t _Size = _TSize; +}; +using __lookup_failure = __lookup_result; + +template +auto __deduce_tuple_type_ovl(tuple<_Args...>&) + -> __lookup_result, sizeof...(_Args)>; + +template +auto __deduce_tuple_type_ovl(pair<_T1, _T2>&) + -> __lookup_result, 2>; + +template +auto __deduce_tuple_type_ovl(array<_Tp, _Size>&) + -> __lookup_result, _Size>; + +template +auto __deduce_tuple_type_imp(int) + -> decltype(__deduce_tuple_type_ovl(_VSTD::declval<__uncvref_t<_Tp>&>())); +template __lookup_failure __deduce_tuple_type_imp(...); + +// __deduce_tuple_like - Given a type determine if it is, or is derived from, +// a tuple-like type. This trait is used to support constructing and assigning +// to std::tuple from user-types derived from a tuple-like type. +template (0)), + bool _Good = _Result::_Success> +struct __deduce_tuple_like { + static_assert(_Good, "incorrect specialization choosen"); + static constexpr bool _Success = true; + static constexpr size_t _Size = _Result::_Size; + using _RawType = typename _Result::type; + using _QualType = + typename __propagate_value_category<_TupleLike>::template __apply<_RawType>; +}; + +template +struct __deduce_tuple_like<_TupleLike, _Result, /*_Good=*/false> { + static constexpr bool _Success = false; + static constexpr size_t _Size = (size_t)-1; +}; + +template > +using __tuple_like_with_size = integral_constant; struct _LIBCPP_TYPE_VIS __check_tuple_constructor_fail { template diff --git a/libcxx/include/tuple b/libcxx/include/tuple index 553d8e5..c9146fd 100644 --- a/libcxx/include/tuple +++ b/libcxx/include/tuple @@ -555,13 +555,19 @@ class _LIBCPP_TYPE_VIS_ONLY tuple { template static constexpr bool __enable_implicit() { - return __tuple_convertible<_Tuple, tuple>::value; + using _Deduced = __deduce_tuple_like<_Tuple>; + using _QualType = typename _Deduced::_QualType; + static_assert(__tuple_like::value, ""); + return __tuple_convertible<_QualType, tuple>::value; } template static constexpr bool __enable_explicit() { - return __tuple_constructible<_Tuple, tuple>::value - && !__tuple_convertible<_Tuple, tuple>::value; + using _Deduced = __deduce_tuple_like<_Tuple>; + using _QualType = typename _Deduced::_QualType; + static_assert(__tuple_like::value, ""); + return __tuple_constructible<_QualType, tuple>::value + && !__tuple_convertible<_QualType, tuple>::value; } }; @@ -814,66 +820,74 @@ public: _VSTD::forward<_Up>(__u)...) {} template , + class _TupBase = typename _Deduced::_QualType, typename enable_if < _CheckTupleLikeConstructor< - __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value - && !_PackExpandsToThisTuple<_Tuple>::value - >::template __enable_implicit<_Tuple>(), + _Deduced::_Size == sizeof...(_Tp) + && !_PackExpandsToThisTuple<_TupBase>::value + >::template __enable_implicit<_TupBase>(), bool >::type = false > _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 - tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible::value)) - : base_(_VSTD::forward<_Tuple>(__t)) {} + tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible::value)) + : base_(_VSTD::forward<_TupBase>(__t)) {} template , + class _TupBase = typename _Deduced::_QualType, typename enable_if < _CheckTupleLikeConstructor< - __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value - && !_PackExpandsToThisTuple<_Tuple>::value - >::template __enable_explicit<_Tuple>(), + _Deduced::_Size == sizeof...(_Tp) + && !_PackExpandsToThisTuple<_TupBase>::value + >::template __enable_explicit<_TupBase>(), bool >::type = false > _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit - tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible::value)) - : base_(_VSTD::forward<_Tuple>(__t)) {} + tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible::value)) + : base_(_VSTD::forward<_TupBase>(__t)) {} template , + class _TupBase = typename _Deduced::_QualType, typename enable_if < _CheckTupleLikeConstructor< - __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value - >::template __enable_implicit<_Tuple>(), + _Deduced::_Size == sizeof...(_Tp) + >::template __enable_implicit<_TupBase>(), bool >::type = false > _LIBCPP_INLINE_VISIBILITY tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t) - : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {} + : base_(allocator_arg_t(), __a, _VSTD::forward<_TupBase>(__t)) {} template , + class _TupBase = typename _Deduced::_QualType, typename enable_if < _CheckTupleLikeConstructor< - __tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value - >::template __enable_explicit<_Tuple>(), + _Deduced::_Size == sizeof...(_Tp) + >::template __enable_explicit<_TupBase>(), bool >::type = false > _LIBCPP_INLINE_VISIBILITY explicit tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t) - : base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {} + : base_(allocator_arg_t(), __a, _VSTD::forward<_TupBase>(__t)) {} using _CanCopyAssign = __all::value...>; using _CanMoveAssign = __all::value...>; _LIBCPP_INLINE_VISIBILITY - tuple& operator=(typename conditional<_CanCopyAssign::value, tuple, __nat>::type const& __t) + tuple& operator=(typename conditional<_CanCopyAssign::value, tuple const&, __nat>::type const& __t) _NOEXCEPT_((__all::value...>::value)) { base_.operator=(__t.base_); @@ -889,16 +903,19 @@ public: } template , + class _TupBase = typename _Deduced::_QualType, class = typename enable_if < - __tuple_assignable<_Tuple, tuple>::value + __tuple_assignable<_TupBase, tuple>::value >::type > _LIBCPP_INLINE_VISIBILITY tuple& - operator=(_Tuple&& __t) _NOEXCEPT_((is_nothrow_assignable::value)) + operator=(_Tuple&& __t) + _NOEXCEPT_((is_nothrow_assignable::value)) { - base_.operator=(_VSTD::forward<_Tuple>(__t)); + base_.operator=(_VSTD::forward<_TupBase>(__t)); return *this; } diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index fbfc9d3..c2b90ea 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -4696,6 +4696,65 @@ struct __can_extract_map_key<_ValTy, _Key, _Key, _RawValTy> #endif +#ifndef _LIBCPP_CXX03_LANG + +// __propagate_value_category -- A utility for detecting the value category +// of a given type and applying that value-category to another type. +// For example applying the cv-ref quals of a derived type to form the +// correctly cv-qualified base type. +template +struct __propagate_value_category { + template struct __checked_apply { + static_assert(!is_reference<_ToType>::value, "must be unqualified"); + static_assert(!is_const<_ToType>::value + && !is_volatile<_ToType>::value, "must be unqualified"); + using type = _ToType; + }; + template + using __apply = typename __checked_apply<_ToType>::type; +}; + +template +struct __propagate_value_category<_FromType&> { + template + using __apply = typename add_lvalue_reference< + typename __propagate_value_category<_FromType>::template __apply<_ToType> + >::type; +}; + +template +struct __propagate_value_category<_FromType&&> { + template + using __apply = typename add_rvalue_reference< + typename __propagate_value_category<_FromType>::template __apply<_ToType> + >::type; +}; + +template +struct __propagate_value_category<_FromType const> { + template + using __apply = typename add_const< + typename __propagate_value_category<_FromType>::template __apply<_ToType> + >::type; +}; + +template +struct __propagate_value_category<_FromType volatile> { + template + using __apply = typename add_volatile< + typename __propagate_value_category<_FromType>::template __apply<_ToType> + >::type; +}; + +template +struct __propagate_value_category<_FromType const volatile> { + template + using __apply = typename add_cv< + typename __propagate_value_category<_FromType>::template __apply<_ToType> + >::type; +}; +#endif + _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_TYPE_TRAITS diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/derived_from_tuple_like.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/derived_from_tuple_like.pass.cpp new file mode 100644 index 0000000..82bb898 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.assign/derived_from_tuple_like.pass.cpp @@ -0,0 +1,113 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// tuple& operator=(const tuple& u); + +// UNSUPPORTED: c++98, c++03 + +#include +#include +#include +#include +#include + +#include "propagate_value_category.hpp" + +struct TracksIntQuals { + TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {} + + template ::type, TracksIntQuals>::value>::type> + TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory()), assigned(false) { + static_assert(std::is_same, int>::value, ""); + } + + template ::type, TracksIntQuals>::value>::type> + TracksIntQuals &operator=(Tp &&x) { + static_assert(std::is_same, int>::value, ""); + value = x; + value_category = getValueCategory(); + assigned = true; + return *this; + } + + void reset() { + value = -1; + value_category = VC_None; + assigned = false; + } + + bool checkConstruct(int expect, ValueCategory expect_vc) const { + return value != 1 && value == expect && value_category == expect_vc && + assigned == false; + } + + bool checkAssign(int expect, ValueCategory expect_vc) const { + return value != 1 && value == expect && value_category == expect_vc && + assigned == true; + } + + int value; + ValueCategory value_category; + bool assigned; +}; + +template +struct DerivedFromTup : Tup { + using Tup::Tup; +}; + +template +void do_derived_assign_test() { + using Tup1 = std::tuple; + Tup1 t; + auto reset = [&]() { + std::get<0>(t) = -1; + std::get<1>(t).reset(); + }; + { + DerivedFromTup> d(42, 101); + t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } + reset(); + { + DerivedFromTup> d(42, 101); + t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } + reset(); + { + DerivedFromTup> d = {{{42, 101}}}; + t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } +} + +int main() { + do_derived_assign_test(); + do_derived_assign_test(); +#if defined(_LIBCPP_VERSION) + // Non-const copy assign and const move assign are libc++ extensions. + do_derived_assign_test(); + do_derived_assign_test(); +#endif +} diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/derived_from_tuple_like.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/derived_from_tuple_like.pass.cpp new file mode 100644 index 0000000..233a308 --- /dev/null +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/derived_from_tuple_like.pass.cpp @@ -0,0 +1,150 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// + +// template class tuple; + +// template +// tuple& operator=(const tuple& u); + +// UNSUPPORTED: c++98, c++03 + +#include +#include +#include +#include +#include + +#include "propagate_value_category.hpp" + +template +struct TracksIntQuals { + TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {} + + template < + class Tp, + typename std::enable_if::type, + TracksIntQuals>::value, + bool>::type = false> + explicit TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory()), assigned(false) { + static_assert(std::is_same, int>::value, ""); + } + + template < + class Tp, + typename std::enable_if::type, + TracksIntQuals>::value, + bool>::type = false> + TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory()), assigned(false) { + static_assert(std::is_same, int>::value, ""); + } + + template ::type, TracksIntQuals>::value>::type> + TracksIntQuals &operator=(Tp &&x) { + static_assert(std::is_same, int>::value, ""); + value = x; + value_category = getValueCategory(); + assigned = true; + return *this; + } + + void reset() { + value = -1; + value_category = VC_None; + assigned = false; + } + + bool checkConstruct(int expect, ValueCategory expect_vc) const { + return value != 1 && value == expect && value_category == expect_vc && + assigned == false; + } + + bool checkAssign(int expect, ValueCategory expect_vc) const { + return value != 1 && value == expect && value_category == expect_vc && + assigned == true; + } + + int value; + ValueCategory value_category; + bool assigned; +}; + +template +struct DerivedFromTup : Tup { + using Tup::Tup; +}; + +template +void do_derived_construct_test() { + using Tup1 = std::tuple>; + { + DerivedFromTup> d(42, 101); + Tup1 t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + DerivedFromTup> d(42, 101); + Tup1 t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + DerivedFromTup> d = {{{42, 101}}}; + Tup1 t = ValueCategoryCast(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + + using Tup2 = std::tuple>; + { + using D = DerivedFromTup>; + static_assert(!std::is_convertible, Tup2>::value, + ""); + D d(42, 101); + Tup2 t(ValueCategoryCast(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + using D = DerivedFromTup>; + static_assert(!std::is_convertible, Tup2>::value, + ""); + D d(42, 101); + Tup2 t(ValueCategoryCast(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + using D = DerivedFromTup>; + static_assert(!std::is_convertible, Tup2>::value, + ""); + D d = {{{42, 101}}}; + Tup2 t(ValueCategoryCast(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } +} + +int main() { + do_derived_construct_test(); + do_derived_construct_test(); +#if defined(_LIBCPP_VERSION) + // Supporting non-const copy and const move are libc++ extensions + do_derived_construct_test(); + do_derived_construct_test(); +#endif +} diff --git a/libcxx/test/support/propagate_value_category.hpp b/libcxx/test/support/propagate_value_category.hpp new file mode 100644 index 0000000..fc5443c --- /dev/null +++ b/libcxx/test/support/propagate_value_category.hpp @@ -0,0 +1,145 @@ +#ifndef TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY +#define TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY + +#include "test_macros.h" +#include + +#if TEST_STD_VER < 11 +#error this header may only be used in C++11 +#endif + +using UnderlyingVCType = unsigned; +enum ValueCategory : UnderlyingVCType { + VC_None = 0, + VC_LVal = 1 << 0, + VC_RVal = 1 << 1, + VC_Const = 1 << 2, + VC_Volatile = 1 << 3, + VC_ConstVolatile = VC_Const | VC_Volatile +}; + +inline constexpr ValueCategory operator&(ValueCategory LHS, ValueCategory RHS) { + return ValueCategory(LHS & (UnderlyingVCType)RHS); +} + +inline constexpr ValueCategory operator|(ValueCategory LHS, ValueCategory RHS) { + return ValueCategory(LHS | (UnderlyingVCType)RHS); +} + +inline constexpr ValueCategory operator^(ValueCategory LHS, ValueCategory RHS) { + return ValueCategory(LHS ^ (UnderlyingVCType)RHS); +} + +inline constexpr bool isValidValueCategory(ValueCategory VC) { + return (VC & (VC_LVal | VC_RVal)) != (VC_LVal | VC_RVal); +} + +inline constexpr bool hasValueCategory(ValueCategory Arg, ValueCategory Key) { + return Arg == Key || ((Arg & Key) == Key); +} + +template +using UnCVRef = + typename std::remove_cv::type>::type; + +template +constexpr ValueCategory getReferenceQuals() { + return std::is_lvalue_reference::value + ? VC_LVal + : (std::is_rvalue_reference::value ? VC_RVal : VC_None); +} +static_assert(getReferenceQuals() == VC_None, ""); +static_assert(getReferenceQuals() == VC_LVal, ""); +static_assert(getReferenceQuals() == VC_RVal, ""); + +template +constexpr ValueCategory getCVQuals() { + using Vp = typename std::remove_reference::type; + return std::is_const::value && std::is_volatile::value + ? VC_ConstVolatile + : (std::is_const::value + ? VC_Const + : (std::is_volatile::value ? VC_Volatile : VC_None)); +} +static_assert(getCVQuals() == VC_None, ""); +static_assert(getCVQuals() == VC_Const, ""); +static_assert(getCVQuals() == VC_Volatile, ""); +static_assert(getCVQuals() == VC_ConstVolatile, ""); +static_assert(getCVQuals() == VC_None, ""); +static_assert(getCVQuals() == VC_Const, ""); + +template +inline constexpr ValueCategory getValueCategory() { + return getReferenceQuals() | getCVQuals(); +} +static_assert(getValueCategory() == VC_None, ""); +static_assert(getValueCategory() == (VC_LVal | VC_Const), ""); +static_assert(getValueCategory() == + (VC_RVal | VC_ConstVolatile), + ""); + +template +struct ApplyValueCategory { +private: + static_assert(isValidValueCategory(VC), ""); + + template + using CondT = typename std::conditional::type; + +public: + template > + using ApplyCVQuals = CondT< + hasValueCategory(VC, VC_ConstVolatile), typename std::add_cv::type, + CondT::type, + CondT::type, Tp>>>; + + template ::type> + using ApplyReferenceQuals = + CondT::type, + CondT::type, Vp>>; + + template + using Apply = ApplyReferenceQuals>>; + + template ::type = true> + static Apply> cast(Tp &&t) { + using ToType = Apply>; + return static_cast(t); + } + + template ::type = true> + static Apply> cast(Tp &&t) { + using ToType = Apply>; + return static_cast(std::move(t)); + } + + template < + class Tp, bool Dummy = true, + typename std::enable_if::type = true> + static Apply> cast(Tp &&t) { + return t; + } +}; + +template +using ApplyValueCategoryT = typename ApplyValueCategory::template Apply; + +template +using PropagateValueCategory = ApplyValueCategory()>; + +template +using PropagateValueCategoryT = + typename ApplyValueCategory()>::template Apply; + +template +typename ApplyValueCategory::template Apply ValueCategoryCast(Tp &&t) { + return ApplyValueCategory::cast(std::forward(t)); +}; + +#endif // TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY -- 2.7.4