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

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 16 15:52:00 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);
----------------
dwblaikie wrote:

Testing looks great - thanks for that!

Yeah, could you split out the alignment problem and we'll discuss that separately - I'm not quite following it/would like to understand it better.

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


More information about the llvm-commits mailing list