From 3c91910f52a724f719c650e05c33ffcad9162ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 14 May 2019 11:53:47 +0200 Subject: [PATCH 1/4] cpp: Add unit test for moving evmc::result --- test/unittests/test_cpp.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/unittests/test_cpp.cpp b/test/unittests/test_cpp.cpp index 798caa9..cb813f5 100644 --- a/test/unittests/test_cpp.cpp +++ b/test/unittests/test_cpp.cpp @@ -161,3 +161,40 @@ TEST(cpp, result_raii) } EXPECT_EQ(release_called, 1); } + +TEST(cpp, result_move) +{ + static auto release_called = 0; + auto release_fn = [](const evmc_result*) noexcept { ++release_called; }; + + release_called = 0; + { + auto raw = evmc_result{}; + raw.gas_left = -1; + raw.release = release_fn; + + auto r0 = evmc::result{raw}; + EXPECT_EQ(r0.gas_left, raw.gas_left); + + auto r1 = std::move(r0); + EXPECT_EQ(r1.gas_left, raw.gas_left); + } + EXPECT_EQ(release_called, 1); + + release_called = 0; + { + auto raw1 = evmc_result{}; + raw1.gas_left = 1; + raw1.release = release_fn; + + auto raw2 = evmc_result{}; + raw2.gas_left = 1; + raw2.release = release_fn; + + auto r1 = evmc::result{raw1}; + auto r2 = evmc::result{raw2}; + + r2 = std::move(r1); + } + EXPECT_EQ(release_called, 2); +} From cea8d3a5b584e609dc406b351a5b290ddba099ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 14 May 2019 12:03:29 +0200 Subject: [PATCH 2/4] cpp: Fix evmc::result's move assignment operator --- include/evmc/evmc.hpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/include/evmc/evmc.hpp b/include/evmc/evmc.hpp index 1ae2d12..b829a15 100644 --- a/include/evmc/evmc.hpp +++ b/include/evmc/evmc.hpp @@ -39,16 +39,22 @@ public: /// Move constructor. result(result&& other) noexcept : evmc_result{other} { - // Disable releaser of the rvalue object. - other.release = nullptr; + other.release = nullptr; // Disable releasing of the rvalue object. } result(result const&) = delete; - /// Move-and-swap assignment operator. - result& operator=(result other) noexcept + /// Move assignment operator. + /// + /// The self-assigment MUST never happen. + /// + /// @param other The other result object. + /// @return The reference to the left-hand side object. + result& operator=(result&& other) noexcept { - std::swap(*this, other); + this->~result(); // Release this object. + static_cast(*this) = other; // Copy data. + other.release = nullptr; // Disable releasing of the rvalue object. return *this; } From 8ee9d4459235084dfba385e87e0f8dfb78b092c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 14 May 2019 12:13:11 +0200 Subject: [PATCH 3/4] cpp: Remove redundant deleted copy constructor of result --- include/evmc/evmc.hpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/include/evmc/evmc.hpp b/include/evmc/evmc.hpp index b829a15..24ee34a 100644 --- a/include/evmc/evmc.hpp +++ b/include/evmc/evmc.hpp @@ -42,8 +42,6 @@ public: other.release = nullptr; // Disable releasing of the rvalue object. } - result(result const&) = delete; - /// Move assignment operator. /// /// The self-assigment MUST never happen. From d9a6a8b0a9d5a467566ef2901ba043d52b91103b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Tue, 14 May 2019 12:07:17 +0200 Subject: [PATCH 4/4] changelog: Add entry about fixed evmc::result --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37123dd..b57d400 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ ## [6.2.2] - unreleased - Fixed: [[#281](https://github.com/ethereum/evmc/pull/281)] - Compilation error of evmc::result::raw() in Visual Studio fixed. + Compilation error of `evmc::result::raw()` in Visual Studio fixed. +- Fixed: [[#282](https://github.com/ethereum/evmc/pull/282)] + The `evmc::result`'s move assignment operator fixed. ## [6.2.1] - 2019-04-29