Add option to disable variant narrowing conversion changes.

The paper P0608R3 - "A sane variant converting constructor" disallows
narrowing conversions in variant. It was meant to address this
surprising problem:

  std::variant<std::string, bool> v = "abc";
  assert(v.index() == 1); // constructs a bool.

However, it also disables every potentially narrowing conversion. For
example:

  variant<unsigned> v = 0; // ill-formed
  variant<string, double> v2 = 42; // ill-formed (int -> double narrows)

These latter changes break code. A lot of code. Within Google it broke
on the order of a hundred thousand target with thousands of root causes
responsible for the breakages.

Of the breakages related to the narrowing restrictions, none of them
exposed outstanding bugs. However, the breakages caused by boolean
conversions (~13 root causes), all but one of them were bugs.

For this reasons, I am adding a flag to disable the narrowing conversion
changes but not the boolean conversions one.

One purpose of this flag is to allow users to opt-out of breaking changes
in variant until the offending code can be cleaned up. For non-trivial
variant usages the amount of cleanup may be significant.

This flag is also required to support automated tooling, such as
clang-tidy, that can automatically fix code broken by this change.
In order for clang-tidy to know the correct alternative to construct,
it must know what alternative was being constructed previously, which
means running it over the old version of std::variant.

Because this change breaks so much code, I will be implementing the
aforementioned clang-tidy check in the very near future.

Additionally I'm plan present this new information to the committee so they can
re-consider if this is a breaking change we want to make.

I think libc++ should very seriously consider pulling this change
before the 9.0 release branch is cut. But that's a separate discussion
that I will start on the lists.

For now this is the minimal first step.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@365960 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/variant b/include/variant
index baf1630..420e8c2 100644
--- a/include/variant
+++ b/include/variant
@@ -1103,7 +1103,11 @@
 
   template <class _Up>
   auto operator()(_Tp, _Up&& __t) const
+#ifndef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
       -> decltype(__test({ _VSTD::forward<_Up>(__t) }));
+#else
+      -> __identity<_Tp>;
+#endif
 };
 
 template <class _Base, class _Tp>
diff --git a/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp b/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp
index b2b53d6..ab4ea7e 100644
--- a/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp
+++ b/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp
@@ -136,7 +136,8 @@
   }
   {
     using V = std::variant<std::string, float>;
-    static_assert(!std::is_assignable<V, int>::value, "no matching operator=");
+    static_assert(std::is_assignable<V, int>::value == VariantAllowsNarrowingConversions,
+    "no matching operator=");
   }
   {
     using V = std::variant<std::unique_ptr<int>, bool>;
@@ -187,6 +188,7 @@
     assert(v.index() == 1);
     assert(std::get<1>(v) == 43);
   }
+#ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
   {
     std::variant<unsigned, long> v;
     v = 42;
@@ -196,6 +198,7 @@
     assert(v.index() == 0);
     assert(std::get<0>(v) == 43);
   }
+#endif
   {
     std::variant<std::string, bool> v = true;
     v = "bar";
diff --git a/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp b/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp
deleted file mode 100644
index d5f370d..0000000
--- a/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++98, c++03, c++11, c++14
-
-// <variant>
-
-// template <class ...Types> class variant;
-
-// template <class T>
-// variant& operator=(T&&) noexcept(see below);
-
-#include <variant>
-#include <string>
-#include <memory>
-
-int main(int, char**)
-{
-  std::variant<int, int> v1;
-  std::variant<long, long long> v2;
-  std::variant<char> v3;
-  v1 = 1; // expected-error {{no viable overloaded '='}}
-  v2 = 1; // expected-error {{no viable overloaded '='}}
-  v3 = 1; // expected-error {{no viable overloaded '='}}
-
-  std::variant<std::string, float> v4;
-  std::variant<std::string, double> v5;
-  std::variant<std::string, bool> v6;
-  v4 = 1; // expected-error {{no viable overloaded '='}}
-  v5 = 1; // expected-error {{no viable overloaded '='}}
-  v6 = 1; // expected-error {{no viable overloaded '='}}
-
-  std::variant<int, bool> v7;
-  std::variant<int, bool const> v8;
-  std::variant<int, bool volatile> v9;
-  v7 = "meow"; // expected-error {{no viable overloaded '='}}
-  v8 = "meow"; // expected-error {{no viable overloaded '='}}
-  v9 = "meow"; // expected-error {{no viable overloaded '='}}
-
-  std::variant<bool> v10;
-  std::variant<bool> v11;
-  std::variant<bool> v12;
-  v10 = std::true_type(); // expected-error {{no viable overloaded '='}}
-  v11 = std::unique_ptr<char>(); // expected-error {{no viable overloaded '='}}
-  v12 = nullptr; // expected-error {{no viable overloaded '='}}
-}
diff --git a/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp b/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp
new file mode 100644
index 0000000..b16cf2c
--- /dev/null
+++ b/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp
@@ -0,0 +1,43 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <variant>
+
+// template <class ...Types> class variant;
+
+// template <class T>
+// variant& operator=(T&&) noexcept(see below);
+
+#include <variant>
+#include <string>
+#include <memory>
+
+#include "variant_test_helpers.hpp"
+
+int main(int, char**)
+{
+  static_assert(!std::is_assignable<std::variant<int, int>, int>::value, "");
+  static_assert(!std::is_assignable<std::variant<long, long long>, int>::value, "");
+  static_assert(std::is_assignable<std::variant<char>, int>::value == VariantAllowsNarrowingConversions, "");
+
+  static_assert(std::is_assignable<std::variant<std::string, float>, int>::value == VariantAllowsNarrowingConversions, "");
+  static_assert(std::is_assignable<std::variant<std::string, double>, int>::value == VariantAllowsNarrowingConversions, "");
+  static_assert(!std::is_assignable<std::variant<std::string, bool>, int>::value, "");
+
+  static_assert(!std::is_assignable<std::variant<int, bool>, decltype("meow")>::value, "");
+  static_assert(!std::is_assignable<std::variant<int, const bool>, decltype("meow")>::value, "");
+  static_assert(!std::is_assignable<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
+
+  static_assert(!std::is_assignable<std::variant<bool>, std::true_type>::value, "");
+  static_assert(!std::is_assignable<std::variant<bool>, std::unique_ptr<char> >::value, "");
+  static_assert(!std::is_assignable<std::variant<bool>, decltype(nullptr)>::value, "");
+
+}
diff --git a/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp b/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
index 40fa20b..55f8d11 100644
--- a/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
+++ b/test/std/utilities/variant/variant.variant/variant.ctor/T.pass.cpp
@@ -69,7 +69,7 @@
   }
   {
     using V = std::variant<std::string, float>;
-    static_assert(!std::is_constructible<V, int>::value,
+    static_assert(std::is_constructible<V, int>::value == VariantAllowsNarrowingConversions,
                   "no matching constructor");
   }
   {
@@ -127,11 +127,13 @@
     static_assert(v.index() == 1, "");
     static_assert(std::get<1>(v) == 42, "");
   }
+#ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
   {
     constexpr std::variant<unsigned, long> v(42);
     static_assert(v.index() == 1, "");
     static_assert(std::get<1>(v) == 42, "");
   }
+#endif
   {
     std::variant<std::string, bool const> v = "foo";
     assert(v.index() == 0);
diff --git a/test/std/utilities/variant/variant.variant/variant.ctor/conv.fail.cpp b/test/std/utilities/variant/variant.variant/variant.ctor/conv.fail.cpp
deleted file mode 100644
index 76b42ac..0000000
--- a/test/std/utilities/variant/variant.variant/variant.ctor/conv.fail.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-// -*- C++ -*-
-//===----------------------------------------------------------------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++98, c++03, c++11, c++14
-
-// <variant>
-
-// template <class ...Types> class variant;
-
-// template <class T> constexpr variant(T&&) noexcept(see below);
-
-#include <variant>
-#include <string>
-#include <memory>
-
-int main(int, char**)
-{
-  std::variant<int, int> v1 = 1; // expected-error {{no viable conversion}}
-  std::variant<long, long long> v2 = 1; // expected-error {{no viable conversion}}
-  std::variant<char> v3 = 1; // expected-error {{no viable conversion}}
-
-  std::variant<std::string, float> v4 = 1; // expected-error {{no viable conversion}}
-  std::variant<std::string, double> v5 = 1; // expected-error {{no viable conversion}}
-  std::variant<std::string, bool> v6 = 1; // expected-error {{no viable conversion}}
-
-  std::variant<int, bool> v7 = "meow"; // expected-error {{no viable conversion}}
-  std::variant<int, bool const> v8 = "meow"; // expected-error {{no viable conversion}}
-  std::variant<int, bool volatile> v9 = "meow"; // expected-error {{no viable conversion}}
-
-  std::variant<bool> v10 = std::true_type(); // expected-error {{no viable conversion}}
-  std::variant<bool> v11 = std::unique_ptr<char>(); // expected-error {{no viable conversion}}
-  std::variant<bool> v12 = nullptr; // expected-error {{no viable conversion}}
-}
diff --git a/test/std/utilities/variant/variant.variant/variant.ctor/conv.pass.cpp b/test/std/utilities/variant/variant.variant/variant.ctor/conv.pass.cpp
new file mode 100644
index 0000000..4799123
--- /dev/null
+++ b/test/std/utilities/variant/variant.variant/variant.ctor/conv.pass.cpp
@@ -0,0 +1,42 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++98, c++03, c++11, c++14
+
+// <variant>
+
+// template <class ...Types> class variant;
+
+// template <class T> constexpr variant(T&&) noexcept(see below);
+
+#include <variant>
+#include <string>
+#include <memory>
+
+#include "variant_test_helpers.hpp"
+
+int main(int, char**)
+{
+  static_assert(!std::is_constructible<std::variant<int, int>, int>::value, "");
+  static_assert(!std::is_constructible<std::variant<long, long long>, int>::value, "");
+  static_assert(std::is_constructible<std::variant<char>, int>::value == VariantAllowsNarrowingConversions, "");
+
+  static_assert(std::is_constructible<std::variant<std::string, float>, int>::value == VariantAllowsNarrowingConversions, "");
+  static_assert(std::is_constructible<std::variant<std::string, double>, int>::value == VariantAllowsNarrowingConversions, "");
+  static_assert(!std::is_constructible<std::variant<std::string, bool>, int>::value, "");
+
+  static_assert(!std::is_constructible<std::variant<int, bool>, decltype("meow")>::value, "");
+  static_assert(!std::is_constructible<std::variant<int, const bool>, decltype("meow")>::value, "");
+  static_assert(!std::is_constructible<std::variant<int, const volatile bool>, decltype("meow")>::value, "");
+
+  static_assert(!std::is_constructible<std::variant<bool>, std::true_type>::value, "");
+  static_assert(!std::is_constructible<std::variant<bool>, std::unique_ptr<char> >::value, "");
+  static_assert(!std::is_constructible<std::variant<bool>, decltype(nullptr)>::value, "");
+  
+}
diff --git a/test/support/variant_test_helpers.hpp b/test/support/variant_test_helpers.hpp
index 59988b5..9d4ceec 100644
--- a/test/support/variant_test_helpers.hpp
+++ b/test/support/variant_test_helpers.hpp
@@ -21,6 +21,15 @@
 
 // FIXME: Currently the variant<T&> tests are disabled using this macro.
 #define TEST_VARIANT_HAS_NO_REFERENCES
+#ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
+# define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
+#endif
+
+#ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
+constexpr bool VariantAllowsNarrowingConversions = true;
+#else
+constexpr bool VariantAllowsNarrowingConversions = false;
+#endif
 
 #ifndef TEST_HAS_NO_EXCEPTIONS
 struct CopyThrows {