[llvm] [ADT] Add a missing call to a unique_function destructor after move (PR #98747)

Dmitry Yanovsky via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 17 16:23:50 PDT 2024


================
@@ -310,4 +310,25 @@ class Incomplete {};
 Incomplete incompleteFunction() { return {}; }
 const Incomplete incompleteFunctionConst() { return {}; }
 
+// Check that the moved-from captured state is properly destroyed during
+// move construction/assignment.
+TEST(UniqueFunctionTest, MovedFromStateIsDestroyedCorrectly) {
+  static int NumOfMovesCalled = 0;
+  static int NumOfDestructorsCalled = 0;
+  struct State {
+    State() = default;
+    State(const State &) = delete;
+    State(State &&) { ++NumOfMovesCalled; }
+    State &operator=(const State &) = delete;
+    State &operator=(State &&Rhs) { return *new (this) State{std::move(Rhs)}; }
+    ~State() { ++NumOfDestructorsCalled; }
+  };
+  {
+    unique_function<void()> CapturingFunction{[state = State{}] {}};
+    unique_function<void()> CapturingFunctionMoved{
+        std::move(CapturingFunction)};
+  }
+  EXPECT_EQ(NumOfDestructorsCalled, 1 + NumOfMovesCalled);
----------------
kerambyte wrote:

Sure thing, removed the alignment-related code from this PR. Opened a separate one here - https://github.com/llvm/llvm-project/pull/99403

https://github.com/llvm/llvm-project/pull/98747


More information about the llvm-commits mailing list