[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
Mon Jul 15 16:08:27 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:
Might be ore obvious/clear to have the default ctor count too - maybe count all the ctor calls together, then check that the dtor calls == ctor calls.
But also, might be worth checking - I think we have a reusable special member counting type utility used in other tests that could be reused here? llvm/unittests/ADT/CountCopyAndMove.h - though, admittedly, we haven't been great about using that everywhere, and various data structure tests certainly have their own versions, but maybe we can try to stem the tide of more of them?
https://github.com/llvm/llvm-project/pull/98747
More information about the llvm-commits
mailing list