Skip to content

[ADT] Add a missing call to a unique_function destructor after move #98747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions llvm/include/llvm/ADT/FunctionExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
// Non-trivial move, so dispatch to a type-erased implementation.
getNonTrivialCallbacks()->MovePtr(getInlineStorage(),
RHS.getInlineStorage());
getNonTrivialCallbacks()->DestroyPtr(RHS.getInlineStorage());
}

// Clear the old callback and inline flag to get back to as-if-null.
Expand Down
2 changes: 2 additions & 0 deletions llvm/unittests/ADT/CountCopyAndMove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

using namespace llvm;

int CountCopyAndMove::DefaultConstructions = 0;
int CountCopyAndMove::ValueConstructions = 0;
int CountCopyAndMove::CopyConstructions = 0;
int CountCopyAndMove::CopyAssignments = 0;
int CountCopyAndMove::MoveConstructions = 0;
Expand Down
13 changes: 11 additions & 2 deletions llvm/unittests/ADT/CountCopyAndMove.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
namespace llvm {

struct CountCopyAndMove {
static int DefaultConstructions;
static int ValueConstructions;
static int CopyConstructions;
static int CopyAssignments;
static int MoveConstructions;
static int MoveAssignments;
static int Destructions;
int val;

CountCopyAndMove() = default;
explicit CountCopyAndMove(int val) : val(val) {}
CountCopyAndMove() { ++DefaultConstructions; }
explicit CountCopyAndMove(int val) : val(val) { ++ValueConstructions; }
CountCopyAndMove(const CountCopyAndMove &other) : val(other.val) {
++CopyConstructions;
}
Expand All @@ -40,13 +42,20 @@ struct CountCopyAndMove {
~CountCopyAndMove() { ++Destructions; }

static void ResetCounts() {
DefaultConstructions = 0;
ValueConstructions = 0;
CopyConstructions = 0;
CopyAssignments = 0;
MoveConstructions = 0;
MoveAssignments = 0;
Destructions = 0;
}

static int TotalConstructions() {
return DefaultConstructions + ValueConstructions + MoveConstructions +
CopyConstructions;
}

static int TotalCopies() { return CopyConstructions + CopyAssignments; }

static int TotalMoves() { return MoveConstructions + MoveAssignments; }
Expand Down
15 changes: 15 additions & 0 deletions llvm/unittests/ADT/FunctionExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/ADT/FunctionExtras.h"
#include "CountCopyAndMove.h"
#include "gtest/gtest.h"

#include <memory>
Expand Down Expand Up @@ -329,4 +330,18 @@ TEST(UniqueFunctionTest, InlineStorageWorks) {
UniqueFunctionWithInlineStorage(&UniqueFunctionWithInlineStorage);
}

// Check that the moved-from captured state is properly destroyed during
// move construction/assignment.
TEST(UniqueFunctionTest, MovedFromStateIsDestroyedCorrectly) {
CountCopyAndMove::ResetCounts();
{
unique_function<void()> CapturingFunction{
[Counter = CountCopyAndMove{}] {}};
unique_function<void()> CapturingFunctionMoved{
std::move(CapturingFunction)};
}
EXPECT_EQ(CountCopyAndMove::TotalConstructions(),
CountCopyAndMove::Destructions);
}

} // anonymous namespace
Loading