From 128af315957eb1516c3b7ffd389774a0287ba300 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Fri, 12 Jul 2019 21:32:11 +0000 Subject: 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 v = "abc"; assert(v.index() == 1); // constructs a bool. However, it also disables every potentially narrowing conversion. For example: variant v = 0; // ill-formed variant 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. llvm-svn: 365960 --- .../variant.variant/variant.assign/T.pass.cpp | 5 ++- .../variant.variant/variant.assign/conv.fail.cpp | 52 ---------------------- .../variant.variant/variant.assign/conv.pass.cpp | 43 ++++++++++++++++++ 3 files changed, 47 insertions(+), 53 deletions(-) delete mode 100644 libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp create mode 100644 libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp (limited to 'libcxx/test/std/utilities/variant/variant.variant/variant.assign') diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp index b2b53d6c6ea..ab4ea7e4cf2 100644 --- a/libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp +++ b/libcxx/test/std/utilities/variant/variant.variant/variant.assign/T.pass.cpp @@ -136,7 +136,8 @@ void test_T_assignment_sfinae() { } { using V = std::variant; - static_assert(!std::is_assignable::value, "no matching operator="); + static_assert(std::is_assignable::value == VariantAllowsNarrowingConversions, + "no matching operator="); } { using V = std::variant, bool>; @@ -187,6 +188,7 @@ void test_T_assignment_basic() { assert(v.index() == 1); assert(std::get<1>(v) == 43); } +#ifndef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS { std::variant v; v = 42; @@ -196,6 +198,7 @@ void test_T_assignment_basic() { assert(v.index() == 0); assert(std::get<0>(v) == 43); } +#endif { std::variant v = true; v = "bar"; diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.fail.cpp deleted file mode 100644 index d5f370d2720..00000000000 --- a/libcxx/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 - -// - -// template class variant; - -// template -// variant& operator=(T&&) noexcept(see below); - -#include -#include -#include - -int main(int, char**) -{ - std::variant v1; - std::variant v2; - std::variant v3; - v1 = 1; // expected-error {{no viable overloaded '='}} - v2 = 1; // expected-error {{no viable overloaded '='}} - v3 = 1; // expected-error {{no viable overloaded '='}} - - std::variant v4; - std::variant v5; - std::variant v6; - v4 = 1; // expected-error {{no viable overloaded '='}} - v5 = 1; // expected-error {{no viable overloaded '='}} - v6 = 1; // expected-error {{no viable overloaded '='}} - - std::variant v7; - std::variant v8; - std::variant v9; - v7 = "meow"; // expected-error {{no viable overloaded '='}} - v8 = "meow"; // expected-error {{no viable overloaded '='}} - v9 = "meow"; // expected-error {{no viable overloaded '='}} - - std::variant v10; - std::variant v11; - std::variant v12; - v10 = std::true_type(); // expected-error {{no viable overloaded '='}} - v11 = std::unique_ptr(); // expected-error {{no viable overloaded '='}} - v12 = nullptr; // expected-error {{no viable overloaded '='}} -} diff --git a/libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp b/libcxx/test/std/utilities/variant/variant.variant/variant.assign/conv.pass.cpp new file mode 100644 index 00000000000..b16cf2cdd58 --- /dev/null +++ b/libcxx/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 + +// + +// template class variant; + +// template +// variant& operator=(T&&) noexcept(see below); + +#include +#include +#include + +#include "variant_test_helpers.hpp" + +int main(int, char**) +{ + static_assert(!std::is_assignable, int>::value, ""); + static_assert(!std::is_assignable, int>::value, ""); + static_assert(std::is_assignable, int>::value == VariantAllowsNarrowingConversions, ""); + + static_assert(std::is_assignable, int>::value == VariantAllowsNarrowingConversions, ""); + static_assert(std::is_assignable, int>::value == VariantAllowsNarrowingConversions, ""); + static_assert(!std::is_assignable, int>::value, ""); + + static_assert(!std::is_assignable, decltype("meow")>::value, ""); + static_assert(!std::is_assignable, decltype("meow")>::value, ""); + static_assert(!std::is_assignable, decltype("meow")>::value, ""); + + static_assert(!std::is_assignable, std::true_type>::value, ""); + static_assert(!std::is_assignable, std::unique_ptr >::value, ""); + static_assert(!std::is_assignable, decltype(nullptr)>::value, ""); + +} -- cgit v1.2.3