diff --git a/AK/Variant.h b/AK/Variant.h index afe116d989..b4802f2200 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -266,6 +266,9 @@ public: requires(!(IsTriviallyCopyConstructible && ...) || !(IsTriviallyDestructible && ...)) #endif { + if constexpr (!(IsTriviallyDestructible && ...)) { + Helper::delete_(m_index, m_data); + } m_index = other.m_index; Helper::copy_(other.m_index, other.m_data, m_data); return *this; @@ -276,6 +279,9 @@ public: requires(!(IsTriviallyMoveConstructible && ...) || !(IsTriviallyDestructible && ...)) #endif { + if constexpr (!(IsTriviallyDestructible && ...)) { + Helper::delete_(m_index, m_data); + } m_index = other.m_index; Helper::move_(other.m_index, other.m_data, m_data); return *this; diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index 4825c6bb0a..8175b695da 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -57,6 +57,12 @@ TEST_CASE(destructor) Variant test_variant { DestructionChecker { was_destroyed } }; } EXPECT(was_destroyed); + + bool was_destroyed_when_assigned_to = false; + Variant original { DestructionChecker { was_destroyed_when_assigned_to } }; + Variant other { 42 }; + original = other; + EXPECT(was_destroyed_when_assigned_to); } TEST_CASE(move_moves)