Skip to content

Commit 4fde4f5

Browse files
committed
Removed unsafe optimizations in makeBlock
1 parent c29a953 commit 4fde4f5

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
- Boxed values now actually formally conform to relevant protocols (as reported by `confromsToProtocol:`)
1010

1111
### Fixed
12+
- Removed some optimizations in `makeBlock` that could cause unexpected block move
13+
instead of copy when reusing block pointers
1214
- Added `<exception>` header include to XCTestUtil.h that is now required on newer Xcodes
1315
- Making `BoxUtil.h` not rely on `std::filesystem` which is not available on older Apple
1416
platforms.

include/objc-helpers/BlockUtil.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,11 @@ namespace BlockUtil
224224
operator BlockType() const & noexcept
225225
{ return castToBlockType();}
226226

227-
operator BlockType() && noexcept {
228-
this->copyCanBeMove = true;
229-
return castToBlockType();
230-
}
231227

232228
auto get() const & noexcept -> BlockType {
233229
return castToBlockType();
234230
}
235231

236-
auto get() && noexcept -> BlockType {
237-
this->copyCanBeMove = true;
238-
return castToBlockType();
239-
}
240-
241232
auto operator()(Args... args) -> Ret {
242233
auto & callable = *reinterpret_cast<TypeToCall *>(this->callableBuf);
243234
return callable(std::forward<Args>(args)...);
@@ -249,7 +240,8 @@ namespace BlockUtil
249240
return dummy;
250241
}
251242
friend auto copy(BlockWithCallable && obj) -> BlockType {
252-
auto dummy = (BlockType)std::move(obj);
243+
obj.copyCanBeMove = true;
244+
auto dummy = (BlockType)obj;
253245
return dummy;
254246
}
255247
#else

test/BlockUtilTest.mm

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ int operator()(int i) const {
9595
})) == 3);
9696
}
9797

98+
//TODO: these would ideally have two moves rather than movae+copy but doing so seems impossible
9899
{
99100
foo::record.clear();
100101
int (^block)(int) = makeBlock(foo{});
101102

102103
CHECK(block(6) == 6);
103104
}
104-
CHECK(foo::record == "dmm~~o~");
105+
CHECK(foo::record == "dmc~~o~");
105106

106107
{
107108
foo::record.clear();
@@ -110,7 +111,8 @@ int operator()(int i) const {
110111

111112
CHECK(block2(6) == 6);
112113
}
113-
CHECK(foo::record == "dmm~~o~");
114+
CHECK(foo::record == "dmc~~o~");
115+
//TODO: end
114116

115117
{
116118
foo::record.clear();
@@ -153,6 +155,29 @@ int operator()(int i) const {
153155
CHECK(foo::record == "dc~co~~");
154156
}
155157

158+
159+
static void inner(void (^bl1)(), void (^bl2)()) {
160+
bl1();
161+
bl2();
162+
}
163+
164+
165+
static void outer(void (^bl)()) {
166+
inner(makeBlock([bl] {
167+
bl();
168+
}), makeBlock([bl] {
169+
bl();
170+
}));
171+
}
172+
173+
TEST_CASE("makeBlock nested") {
174+
175+
auto p = std::make_shared<int>(1);
176+
outer(makeBlock([p] {
177+
CHECK(p);
178+
}));
179+
}
180+
156181
@interface MakeStrongCheck : NSObject {
157182
@public int i;
158183
}

0 commit comments

Comments
 (0)