-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[coroutines] Introduce [[clang::coro_lifetimebound]] #72851
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
164bf1e
Introduce [[clang::coro_lifetimebound]]
usx95 51fe7bc
add docs
usx95 eee67c0
add Release notes
usx95 dc53ef5
fix codeblock formatting
usx95 de7e094
address comments and add negative test
usx95 1f5f39d
fix release notes
usx95 b6e440b
address comments
usx95 5dd43ae
fix CoNoCRT
usx95 01ac8ea
fix spaces
usx95 bab8a70
address comments
usx95 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,123 @@ | ||
// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -std=c++20 -fsyntax-only -verify -Wall -Wextra -Wno-error=unreachable-code -Wno-unused | ||
|
||
#include "Inputs/std-coroutine.h" | ||
|
||
using std::suspend_always; | ||
using std::suspend_never; | ||
|
||
template <typename T> struct [[clang::coro_lifetimebound, clang::coro_return_type]] Co { | ||
struct promise_type { | ||
Co<T> get_return_object() { | ||
return {}; | ||
} | ||
suspend_always initial_suspend(); | ||
suspend_always final_suspend() noexcept; | ||
void unhandled_exception(); | ||
void return_value(const T &t); | ||
|
||
template <typename U> | ||
auto await_transform(const Co<U> &) { | ||
struct awaitable { | ||
bool await_ready() noexcept { return false; } | ||
void await_suspend(std::coroutine_handle<>) noexcept {} | ||
U await_resume() noexcept { return {}; } | ||
}; | ||
return awaitable{}; | ||
} | ||
}; | ||
}; | ||
|
||
Co<int> foo_coro(const int& b) { | ||
if (b > 0) | ||
co_return 1; | ||
co_return 2; | ||
} | ||
|
||
int getInt() { return 0; } | ||
|
||
Co<int> bar_coro(const int &b, int c) { | ||
int x = co_await foo_coro(b); | ||
int y = co_await foo_coro(1); | ||
int z = co_await foo_coro(getInt()); | ||
auto unsafe1 = foo_coro(1); // expected-warning {{temporary whose address is used as value of local variable}} | ||
auto unsafe2 = foo_coro(getInt()); // expected-warning {{temporary whose address is used as value of local variable}} | ||
auto safe1 = foo_coro(b); | ||
auto safe2 = foo_coro(c); | ||
co_return co_await foo_coro(co_await foo_coro(1)); | ||
} | ||
|
||
[[clang::coro_wrapper]] Co<int> plain_return_co(int b) { | ||
return foo_coro(b); // expected-warning {{address of stack memory associated with parameter}} | ||
} | ||
|
||
[[clang::coro_wrapper]] Co<int> safe_forwarding(const int& b) { | ||
return foo_coro(b); | ||
} | ||
|
||
[[clang::coro_wrapper]] Co<int> unsafe_wrapper(int b) { | ||
return safe_forwarding(b); // expected-warning {{address of stack memory associated with parameter}} | ||
} | ||
|
||
[[clang::coro_wrapper]] Co<int> complex_plain_return(int b) { | ||
return b > 0 | ||
? foo_coro(1) // expected-warning {{returning address of local temporary object}} | ||
: bar_coro(0, 1); // expected-warning {{returning address of local temporary object}} | ||
} | ||
|
||
#define CORO_WRAPPER \ | ||
_Pragma("clang diagnostic push") \ | ||
_Pragma("clang diagnostic ignored \"-Wc++23-extensions\"") \ | ||
[[clang::coro_wrapper]] \ | ||
_Pragma("clang diagnostic pop") | ||
|
||
void lambdas() { | ||
auto unsafe_lambda = [] CORO_WRAPPER (int b) { | ||
return foo_coro(b); // expected-warning {{address of stack memory associated with parameter}} | ||
}; | ||
auto coro_lambda = [] (const int&) -> Co<int> { | ||
co_return 0; | ||
}; | ||
auto unsafe_coro_lambda = [&] (const int& b) -> Co<int> { | ||
int x = co_await coro_lambda(b); | ||
auto safe = coro_lambda(b); | ||
auto unsafe1 = coro_lambda(1); // expected-warning {{temporary whose address is used as value of local variable}} | ||
auto unsafe2 = coro_lambda(getInt()); // expected-warning {{temporary whose address is used as value of local variable}} | ||
auto unsafe3 = coro_lambda(co_await coro_lambda(b)); // expected-warning {{temporary whose address is used as value of local variable}} | ||
co_return co_await safe; | ||
}; | ||
auto safe_lambda = [](int b) -> Co<int> { | ||
int x = co_await foo_coro(1); | ||
co_return x + co_await foo_coro(b); | ||
}; | ||
} | ||
// ============================================================================= | ||
// Safe usage when parameters are value | ||
// ============================================================================= | ||
namespace by_value { | ||
Co<int> value_coro(int b) { co_return co_await foo_coro(b); } | ||
[[clang::coro_wrapper]] Co<int> wrapper1(int b) { return value_coro(b); } | ||
[[clang::coro_wrapper]] Co<int> wrapper2(const int& b) { return value_coro(b); } | ||
} | ||
|
||
// ============================================================================= | ||
// Lifetime bound but not a Coroutine Return Type: No analysis. | ||
// ============================================================================= | ||
namespace not_a_crt { | ||
template <typename T> struct [[clang::coro_lifetimebound]] CoNoCRT { | ||
struct promise_type { | ||
CoNoCRT<T> get_return_object() { | ||
return {}; | ||
} | ||
suspend_always initial_suspend(); | ||
suspend_always final_suspend() noexcept; | ||
void unhandled_exception(); | ||
void return_value(const T &t); | ||
}; | ||
}; | ||
|
||
CoNoCRT<int> foo_coro(const int& a) { co_return a; } | ||
CoNoCRT<int> bar(int a) { | ||
auto x = foo_coro(a); | ||
co_return 1; | ||
} | ||
} // namespace not_a_crt |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.