From bc37f8d93dc614ce0ca1c0c2bb9c42edfea13445 Mon Sep 17 00:00:00 2001 From: Marshall Clow Date: Mon, 12 Jun 2017 16:13:17 +0000 Subject: [PATCH] Make tuple_element static_assert in pair if the index is out of range. Also, add a message to variant_alternative<> in the similar case (it already asserted). Add tests for this git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@305196 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/utility | 6 ++++ include/variant | 2 +- .../pairs.pair/pair.tuple_element.fail.cpp | 25 ++++++++++++++ .../variant_alternative.fail.cpp | 34 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp create mode 100644 test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp diff --git a/include/utility b/include/utility index be7320713..958378b8b 100644 --- a/include/utility +++ b/include/utility @@ -653,6 +653,12 @@ template class _LIBCPP_TEMPLATE_VIS tuple_size > : public integral_constant {}; +template +class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> > +{ + static_assert(_Ip < 2, "Index out of bounds in std::tuple_element>"); +}; + template class _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> > { diff --git a/include/variant b/include/variant index 8505f3262..8711ef6eb 100644 --- a/include/variant +++ b/include/variant @@ -278,7 +278,7 @@ struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const volatile _Tp> template struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> { - static_assert(_Ip < sizeof...(_Types)); + static_assert(_Ip < sizeof...(_Types), "Index out of bounds in std::variant_alternative<>"); using type = __type_pack_element<_Ip, _Types...>; }; diff --git a/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp b/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp new file mode 100644 index 000000000..2a92240a6 --- /dev/null +++ b/test/libcxx/utilities/utility/pairs/pairs.pair/pair.tuple_element.fail.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// 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 struct pair + +// tuple_element >::type + +#include + +int main() +{ + { + typedef std::pair P; + std::tuple_element<2, P>::type foo; // expected-note {{requested here}} + // expected-error@utility:* {{static_assert failed "Index out of bounds in std::tuple_element>"}} + } +} diff --git a/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp b/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp new file mode 100644 index 000000000..1c81b67e6 --- /dev/null +++ b/test/libcxx/utilities/variant/variant.variant/variant.helper/variant_alternative.fail.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 struct variant_alternative; // undefined +// template struct variant_alternative; +// template struct variant_alternative; +// template struct variant_alternative; +// template +// using variant_alternative_t = typename variant_alternative::type; +// +// template +// struct variant_alternative>; + + +#include +#include + + +int main() +{ + { + typedef std::variant T; + std::variant_alternative<2, T>::type foo; // expected-note {{requested here}} + // expected-error@variant:* {{static_assert failed "Index out of bounds in std::variant_alternative<>"}} + } +}