From 986d63466e50445b3d6df119da50532472f36219 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Sat, 25 Dec 2021 02:12:54 +0330 Subject: [PATCH] AK: Remove Variant::operator Variant() This is an interface to downcast(), which degrades errors into runtime errors, and allows seemingly-correct-but-not-quite constructs like the following to appear to compile, but fail at runtime: Variant, U> foo = ...; Variant, U> bar = foo; The expectation here is that `foo` is converted to a RefPtr if it contains one, and remains a U otherwise, but in reality, the NonnullRefPtr variant is simply dropped on the floor, and the resulting variant becomes invalid, failing the assertion in downcast(). This commit adds a Variant(Variant) constructor that ensures that no alternative can be left out at compiletime, for the users that were using this interface for merely increasing the number of alternatives (for instance, LibSQL's Value class). --- AK/Variant.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/AK/Variant.h b/AK/Variant.h index 883fb2e526..ad73d7bd23 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -207,6 +207,18 @@ public: return index_of() != invalid_index; } + template + Variant(Variant&& old) requires((can_contain() && ...)) + : Variant(move(old).template downcast()) + { + } + + template + Variant(const Variant& old) requires((can_contain() && ...)) + : Variant(old.template downcast()) + { + } + template friend struct Variant; @@ -389,18 +401,6 @@ public: return instance; } - template - explicit operator Variant() && - { - return downcast(); - } - - template - explicit operator Variant() const& - { - return downcast(); - } - private: static constexpr auto data_size = Detail::integer_sequence_generate_array(0, IntegerSequence()).max(); static constexpr auto data_alignment = Detail::integer_sequence_generate_array(0, IntegerSequence()).max();