-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Fix std::function's handling of blocks under Objc ARC
Previously, some uses of std::function with blocks would crash when ARC was enabled. rdar://100907096 Differential Revision: https://reviews.llvm.org/D135706
- Loading branch information
Showing
3 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
libcxx/test/libcxx/utilities/function.objects/func.blocks.arc.pass.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// std::function support for "blocks" when ARC is enabled | ||
|
||
// UNSUPPORTED: c++03 | ||
|
||
// This test requires the Blocks runtime, which is (only?) available on Darwin | ||
// out-of-the-box. | ||
// REQUIRES: has-fblocks && darwin | ||
|
||
// ADDITIONAL_COMPILE_FLAGS: -fblocks -fobjc-arc | ||
|
||
#include <functional> | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <string> | ||
|
||
struct Foo { | ||
Foo() = default; | ||
Foo(std::size_t (^bl)()) : f(bl) {} | ||
|
||
std::function<int()> f; | ||
}; | ||
|
||
Foo Factory(std::size_t (^bl)()) { | ||
Foo result(bl); | ||
return result; | ||
} | ||
|
||
Foo Factory2() { | ||
auto hello = std::string("Hello world"); | ||
return Factory(^() { | ||
return hello.size(); | ||
}); | ||
} | ||
|
||
Foo AssignmentFactory(std::size_t (^bl)()) { | ||
Foo result; | ||
result.f = bl; | ||
return result; | ||
} | ||
|
||
Foo AssignmentFactory2() { | ||
auto hello = std::string("Hello world"); | ||
return AssignmentFactory(^() { | ||
return hello.size(); | ||
}); | ||
} | ||
|
||
int main(int, char **) { | ||
// Case 1, works | ||
{ | ||
auto hello = std::string("Hello world"); | ||
auto f = AssignmentFactory(^() { | ||
return hello.size(); | ||
}); | ||
assert(f.f() == 11); | ||
} | ||
|
||
// Case 2, works | ||
{ | ||
auto f = AssignmentFactory2(); | ||
assert(f.f() == 11); | ||
} | ||
|
||
// Case 3, works | ||
{ | ||
auto hello = std::string("Hello world"); | ||
auto f = Factory(^() { | ||
return hello.size(); | ||
}); | ||
assert(f.f() == 11); | ||
} | ||
|
||
// Case 4, used to crash under ARC | ||
{ | ||
auto f = Factory2(); | ||
assert(f.f() == 11); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters