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

llvm-svn: 305196
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: 0c69d6e9bbb8f833daf75297a8b7fab724b33bc1
diff --git a/include/utility b/include/utility
index be73207..958378b 100644
--- a/include/utility
+++ b/include/utility
@@ -653,6 +653,12 @@
   class _LIBCPP_TEMPLATE_VIS tuple_size<pair<_T1, _T2> >
     : public integral_constant<size_t, 2> {};
 
+template <size_t _Ip, class _T1, class _T2>
+class _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, pair<_T1, _T2> >
+{
+    static_assert(_Ip < 2, "Index out of bounds in std::tuple_element<std::pair<T1, T2>>");
+};
+
 template <class _T1, class _T2>
 class _LIBCPP_TEMPLATE_VIS tuple_element<0, pair<_T1, _T2> >
 {
diff --git a/include/variant b/include/variant
index 8505f32..8711ef6 100644
--- a/include/variant
+++ b/include/variant
@@ -278,7 +278,7 @@
 
 template <size_t _Ip, class... _Types>
 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 0000000..2a92240
--- /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.
+//
+//===----------------------------------------------------------------------===//
+
+// <utility>
+
+// template <class T1, class T2> struct pair
+
+// tuple_element<I, pair<T1, T2> >::type
+
+#include <utility>
+
+int main()
+{
+    {
+    typedef std::pair<int, double> 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<std::pair<T1, T2>>"}}
+    }
+}
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 0000000..1c81b67
--- /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.
+//
+//===----------------------------------------------------------------------===//
+
+// <array>
+
+// template <size_t I, class T> struct variant_alternative; // undefined
+// template <size_t I, class T> struct variant_alternative<I, const T>;
+// template <size_t I, class T> struct variant_alternative<I, volatile T>;
+// template <size_t I, class T> struct variant_alternative<I, const volatile T>;
+// template <size_t I, class T>
+//   using variant_alternative_t = typename variant_alternative<I, T>::type;
+//
+// template <size_t I, class... Types>
+//    struct variant_alternative<I, variant<Types...>>;
+
+
+#include <variant>
+#include <cassert>
+
+
+int main()
+{
+    {
+        typedef std::variant<int, double> 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<>"}}
+    }
+}