diff options
| author | Eric Fiselier <eric@efcs.ca> | 2016-12-14 22:22:38 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2016-12-14 22:22:38 +0000 |
| commit | f8136d08c6b1e1d7b922d12b202736f38e21f33c (patch) | |
| tree | 759c7cce2a61ed6b0535890912b74d76de1b104c /libcxx/test | |
| parent | b677fe00fb3d5e9d384ad12030494aa9bf9b3fb7 (diff) | |
| download | bcm5719-llvm-f8136d08c6b1e1d7b922d12b202736f38e21f33c.tar.gz bcm5719-llvm-f8136d08c6b1e1d7b922d12b202736f38e21f33c.zip | |
[libcxx] Fix tuple construction/assignment from types derived from tuple/pair/array.
Summary:
The standard requires tuple have the following constructors:
```
tuple(tuple<OtherTypes...> const&);
tuple(tuple<OtherTypes...> &&);
tuple(pair<T1, T2> const&);
tuple(pair<T1, T2> &&);
tuple(array<T, N> const&);
tuple(array<T, N> &&);
```
However libc++ implements these as a single constructor with the signature:
```
template <class TupleLike, enable_if_t<__is_tuple_like<TupleLike>::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
Diffstat (limited to 'libcxx/test')
3 files changed, 408 insertions, 0 deletions
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 00000000000..82bb8984321 --- /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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <class... UTypes> +// tuple& operator=(const tuple<UTypes...>& u); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <array> +#include <string> +#include <utility> +#include <cassert> + +#include "propagate_value_category.hpp" + +struct TracksIntQuals { + TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {} + + template <class Tp, + class = typename std::enable_if<!std::is_same< + typename std::decay<Tp>::type, TracksIntQuals>::value>::type> + TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory<Tp &&>()), assigned(false) { + static_assert(std::is_same<UnCVRef<Tp>, int>::value, ""); + } + + template <class Tp, + class = typename std::enable_if<!std::is_same< + typename std::decay<Tp>::type, TracksIntQuals>::value>::type> + TracksIntQuals &operator=(Tp &&x) { + static_assert(std::is_same<UnCVRef<Tp>, int>::value, ""); + value = x; + value_category = getValueCategory<Tp &&>(); + 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 <class Tup> +struct DerivedFromTup : Tup { + using Tup::Tup; +}; + +template <ValueCategory VC> +void do_derived_assign_test() { + using Tup1 = std::tuple<long, TracksIntQuals>; + Tup1 t; + auto reset = [&]() { + std::get<0>(t) = -1; + std::get<1>(t).reset(); + }; + { + DerivedFromTup<std::tuple<int, int>> d(42, 101); + t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } + reset(); + { + DerivedFromTup<std::pair<int, int>> d(42, 101); + t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } + reset(); + { + DerivedFromTup<std::array<int, 2>> d = {{{42, 101}}}; + t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkAssign(101, VC)); + } +} + +int main() { + do_derived_assign_test<VC_LVal | VC_Const>(); + do_derived_assign_test<VC_RVal>(); +#if defined(_LIBCPP_VERSION) + // Non-const copy assign and const move assign are libc++ extensions. + do_derived_assign_test<VC_LVal>(); + do_derived_assign_test<VC_RVal | VC_Const>(); +#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 00000000000..233a3088119 --- /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. +// +//===----------------------------------------------------------------------===// + +// <tuple> + +// template <class... Types> class tuple; + +// template <class... UTypes> +// tuple& operator=(const tuple<UTypes...>& u); + +// UNSUPPORTED: c++98, c++03 + +#include <tuple> +#include <array> +#include <string> +#include <utility> +#include <cassert> + +#include "propagate_value_category.hpp" + +template <bool Explicit = false> +struct TracksIntQuals { + TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {} + + template < + class Tp, + typename std::enable_if<Explicit && + !std::is_same<typename std::decay<Tp>::type, + TracksIntQuals>::value, + bool>::type = false> + explicit TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory<Tp &&>()), assigned(false) { + static_assert(std::is_same<UnCVRef<Tp>, int>::value, ""); + } + + template < + class Tp, + typename std::enable_if<!Explicit && + !std::is_same<typename std::decay<Tp>::type, + TracksIntQuals>::value, + bool>::type = false> + TracksIntQuals(Tp &&x) + : value(x), value_category(getValueCategory<Tp &&>()), assigned(false) { + static_assert(std::is_same<UnCVRef<Tp>, int>::value, ""); + } + + template <class Tp, + class = typename std::enable_if<!std::is_same< + typename std::decay<Tp>::type, TracksIntQuals>::value>::type> + TracksIntQuals &operator=(Tp &&x) { + static_assert(std::is_same<UnCVRef<Tp>, int>::value, ""); + value = x; + value_category = getValueCategory<Tp &&>(); + 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 <class Tup> +struct DerivedFromTup : Tup { + using Tup::Tup; +}; + +template <ValueCategory VC> +void do_derived_construct_test() { + using Tup1 = std::tuple<long, TracksIntQuals</*Explicit*/ false>>; + { + DerivedFromTup<std::tuple<int, int>> d(42, 101); + Tup1 t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + DerivedFromTup<std::pair<int, int>> d(42, 101); + Tup1 t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + DerivedFromTup<std::array<int, 2>> d = {{{42, 101}}}; + Tup1 t = ValueCategoryCast<VC>(d); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + + using Tup2 = std::tuple<long, TracksIntQuals</*Explicit*/ true>>; + { + using D = DerivedFromTup<std::tuple<int, int>>; + static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value, + ""); + D d(42, 101); + Tup2 t(ValueCategoryCast<VC>(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + using D = DerivedFromTup<std::pair<int, int>>; + static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value, + ""); + D d(42, 101); + Tup2 t(ValueCategoryCast<VC>(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } + { + using D = DerivedFromTup<std::array<int, 2>>; + static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value, + ""); + D d = {{{42, 101}}}; + Tup2 t(ValueCategoryCast<VC>(d)); + assert(std::get<0>(t) == 42); + assert(std::get<1>(t).checkConstruct(101, VC)); + } +} + +int main() { + do_derived_construct_test<VC_LVal | VC_Const>(); + do_derived_construct_test<VC_RVal>(); +#if defined(_LIBCPP_VERSION) + // Supporting non-const copy and const move are libc++ extensions + do_derived_construct_test<VC_LVal>(); + do_derived_construct_test<VC_RVal | VC_Const>(); +#endif +} diff --git a/libcxx/test/support/propagate_value_category.hpp b/libcxx/test/support/propagate_value_category.hpp new file mode 100644 index 00000000000..fc5443cdaa8 --- /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 <type_traits> + +#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 <class Tp> +using UnCVRef = + typename std::remove_cv<typename std::remove_reference<Tp>::type>::type; + +template <class Tp> +constexpr ValueCategory getReferenceQuals() { + return std::is_lvalue_reference<Tp>::value + ? VC_LVal + : (std::is_rvalue_reference<Tp>::value ? VC_RVal : VC_None); +} +static_assert(getReferenceQuals<int>() == VC_None, ""); +static_assert(getReferenceQuals<int &>() == VC_LVal, ""); +static_assert(getReferenceQuals<int &&>() == VC_RVal, ""); + +template <class Tp> +constexpr ValueCategory getCVQuals() { + using Vp = typename std::remove_reference<Tp>::type; + return std::is_const<Vp>::value && std::is_volatile<Vp>::value + ? VC_ConstVolatile + : (std::is_const<Vp>::value + ? VC_Const + : (std::is_volatile<Vp>::value ? VC_Volatile : VC_None)); +} +static_assert(getCVQuals<int>() == VC_None, ""); +static_assert(getCVQuals<int const>() == VC_Const, ""); +static_assert(getCVQuals<int volatile>() == VC_Volatile, ""); +static_assert(getCVQuals<int const volatile>() == VC_ConstVolatile, ""); +static_assert(getCVQuals<int &>() == VC_None, ""); +static_assert(getCVQuals<int const &>() == VC_Const, ""); + +template <class Tp> +inline constexpr ValueCategory getValueCategory() { + return getReferenceQuals<Tp>() | getCVQuals<Tp>(); +} +static_assert(getValueCategory<int>() == VC_None, ""); +static_assert(getValueCategory<int const &>() == (VC_LVal | VC_Const), ""); +static_assert(getValueCategory<int const volatile &&>() == + (VC_RVal | VC_ConstVolatile), + ""); + +template <ValueCategory VC> +struct ApplyValueCategory { +private: + static_assert(isValidValueCategory(VC), ""); + + template <bool Pred, class Then, class Else> + using CondT = typename std::conditional<Pred, Then, Else>::type; + +public: + template <class Tp, class Vp = UnCVRef<Tp>> + using ApplyCVQuals = CondT< + hasValueCategory(VC, VC_ConstVolatile), typename std::add_cv<Vp>::type, + CondT<hasValueCategory(VC, VC_Const), typename std::add_const<Vp>::type, + CondT<hasValueCategory(VC, VC_Volatile), + typename std::add_volatile<Vp>::type, Tp>>>; + + template <class Tp, class Vp = typename std::remove_reference<Tp>::type> + using ApplyReferenceQuals = + CondT<hasValueCategory(VC, VC_LVal), + typename std::add_lvalue_reference<Vp>::type, + CondT<hasValueCategory(VC, VC_RVal), + typename std::add_rvalue_reference<Vp>::type, Vp>>; + + template <class Tp> + using Apply = ApplyReferenceQuals<ApplyCVQuals<UnCVRef<Tp>>>; + + template <class Tp, bool Dummy = true, + typename std::enable_if<Dummy && (VC & VC_LVal), bool>::type = true> + static Apply<UnCVRef<Tp>> cast(Tp &&t) { + using ToType = Apply<UnCVRef<Tp>>; + return static_cast<ToType>(t); + } + + template <class Tp, bool Dummy = true, + typename std::enable_if<Dummy && (VC & VC_RVal), bool>::type = true> + static Apply<UnCVRef<Tp>> cast(Tp &&t) { + using ToType = Apply<UnCVRef<Tp>>; + return static_cast<ToType>(std::move(t)); + } + + template < + class Tp, bool Dummy = true, + typename std::enable_if<Dummy && ((VC & (VC_LVal | VC_RVal)) == VC_None), + bool>::type = true> + static Apply<UnCVRef<Tp>> cast(Tp &&t) { + return t; + } +}; + +template <ValueCategory VC, class Tp> +using ApplyValueCategoryT = typename ApplyValueCategory<VC>::template Apply<Tp>; + +template <class Tp> +using PropagateValueCategory = ApplyValueCategory<getValueCategory<Tp>()>; + +template <class Tp, class Up> +using PropagateValueCategoryT = + typename ApplyValueCategory<getValueCategory<Tp>()>::template Apply<Up>; + +template <ValueCategory VC, class Tp> +typename ApplyValueCategory<VC>::template Apply<Tp> ValueCategoryCast(Tp &&t) { + return ApplyValueCategory<VC>::cast(std::forward<Tp>(t)); +}; + +#endif // TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY |

