[llvm] [orc-rt] Tighten move_only_function ShouldCopyInitialize test. (PR #226483)
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 25 06:20:05 PDT 2026
https://github.com/lhames created https://github.com/llvm/llvm-project/pull/226483
ShouldCopyInitialize only checked that constructing a move_only_function from an lvalue didn't move the callable, so it passed when the callable was held by reference and never copied at all.
This commit replaces the moved flag with copy and move counters, and checks that construction performs exactly one copy and no moves.
>From 7dcdcbcd0d16ffe8108f174184fe3e77c99c6095 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Fri, 25 Sep 2026 23:03:24 +1000
Subject: [PATCH] [orc-rt] Tighten move_only_function ShouldCopyInitialize
test.
ShouldCopyInitialize only checked that constructing a move_only_function
from an lvalue didn't move the callable, so it passed when the callable was
held by reference and never copied at all.
This commit replaces the moved flag with copy and move counters, and checks
that construction performs exactly one copy and no moves.
---
.../unit/support/move_only_function-test.cpp | 25 +++++++++++++------
1 file changed, 17 insertions(+), 8 deletions(-)
diff --git a/orc-rt/test/unit/support/move_only_function-test.cpp b/orc-rt/test/unit/support/move_only_function-test.cpp
index 73e317586e195..bd934d3531587 100644
--- a/orc-rt/test/unit/support/move_only_function-test.cpp
+++ b/orc-rt/test/unit/support/move_only_function-test.cpp
@@ -229,21 +229,30 @@ TEST(MoveOnlyFunctionTest, Constness) {
}
TEST(MoveOnlyFunctionTest, ShouldCopyInitialize) {
- // Check that we don't accidentally move-initialize move_only_functions.
+ // Check that move_only_functions constructed from lvalues copy-initialize
+ // exactly once (rather than move-initializing, or not initializing at all).
class ShouldCopy {
public:
- ShouldCopy(bool &Moved) : Moved(Moved) {}
- ShouldCopy(const ShouldCopy &) = default;
- ShouldCopy(ShouldCopy &&Other) : Moved(Other.Moved) { Moved = true; }
+ ShouldCopy(size_t &Copies, size_t &Moves) : Copies(Copies), Moves(Moves) {}
+ ShouldCopy(const ShouldCopy &Other)
+ : Copies(Other.Copies), Moves(Other.Moves) {
+ ++Copies;
+ }
+ ShouldCopy(ShouldCopy &&Other) : Copies(Other.Copies), Moves(Other.Moves) {
+ ++Moves;
+ }
void operator()() {}
private:
- bool &Moved;
+ size_t &Copies;
+ size_t &Moves;
};
- bool DidMove = false;
- ShouldCopy SC(DidMove);
+ size_t Copies = 0;
+ size_t Moves = 0;
+ ShouldCopy SC(Copies, Moves);
move_only_function<void()> F(SC);
- EXPECT_FALSE(DidMove);
+ EXPECT_EQ(Copies, 1U);
+ EXPECT_EQ(Moves, 0U);
}
TEST(MoveOnlyFunctionTest, LValueCallableIsCopiedNotReferenced) {
More information about the llvm-commits
mailing list