-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[coroutines] Introduce [[clang::coro_return_type]] and [[clang::coro_wrapper]] #71945
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
14 commits
Select commit
Hold shift + click to select a range
89a2d60
[coroutines] Introduce [[clang::coro_return_type]] and [[clang::co
usx95 78bce90
fix promise_type::get_return_object
usx95 fce73d7
add AttrDocs.td
usx95 03acdd2
trim AttrDocs.td
usx95 643bea1
remove trailing whitespace
usx95 5c73847
release notes and address comments
usx95 dd34d4c
update docs
usx95 7dbc5c9
Apply suggestions from code review
usx95 e328a3a
remove space
usx95 504f2c3
add lambda in wrapper test
usx95 f2daa69
lambda coroutine wrapper test
usx95 236ac48
moved getCurFunction out of checkCoroutineWrapper. updated doc
usx95 f4c5207
doc updates
usx95 27730f9
fix doc format
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
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,134 @@ | ||
// RUN: %clang_cc1 -triple x86_64-apple-darwin9 %s -std=c++20 -fsyntax-only -verify -Wall -Wextra | ||
#include "Inputs/std-coroutine.h" | ||
|
||
using std::suspend_always; | ||
using std::suspend_never; | ||
|
||
|
||
template <typename T> struct [[clang::coro_return_type]] Gen { | ||
struct promise_type { | ||
usx95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Gen<T> get_return_object() { | ||
return {}; | ||
} | ||
suspend_always initial_suspend(); | ||
suspend_always final_suspend() noexcept; | ||
void unhandled_exception(); | ||
void return_value(T t); | ||
|
||
template <typename U> | ||
auto await_transform(const Gen<U> &) { | ||
struct awaitable { | ||
bool await_ready() noexcept { return false; } | ||
void await_suspend(std::coroutine_handle<>) noexcept {} | ||
U await_resume() noexcept { return {}; } | ||
}; | ||
return awaitable{}; | ||
} | ||
}; | ||
}; | ||
|
||
Gen<int> foo_coro(int b); | ||
Gen<int> foo_coro(int b) { co_return b; } | ||
|
||
[[clang::coro_wrapper]] Gen<int> marked_wrapper1(int b) { return foo_coro(b); } | ||
|
||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
Gen<int> non_marked_wrapper(int b) { return foo_coro(b); } | ||
|
||
namespace using_decl { | ||
template <typename T> using Co = Gen<T>; | ||
|
||
[[clang::coro_wrapper]] Co<int> marked_wrapper1(int b) { return foo_coro(b); } | ||
|
||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
Co<int> non_marked_wrapper(int b) { return foo_coro(b); } | ||
} // namespace using_decl | ||
|
||
namespace lambdas { | ||
#define CORO_WRAPPER \ | ||
_Pragma("clang diagnostic push") \ | ||
_Pragma("clang diagnostic ignored \"-Wc++23-extensions\"") \ | ||
[[clang::coro_wrapper]] \ | ||
_Pragma("clang diagnostic pop") | ||
|
||
void foo() { | ||
auto coro_lambda = []() -> Gen<int> { | ||
co_return 1; | ||
}; | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
auto not_allowed_wrapper = []() -> Gen<int> { | ||
return foo_coro(1); | ||
}; | ||
auto allowed_wrapper = [] CORO_WRAPPER() -> Gen<int> { | ||
return foo_coro(1); | ||
}; | ||
} | ||
|
||
Gen<int> coro_containing_lambda() { | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
auto wrapper_lambda = []() -> Gen<int> { | ||
return foo_coro(1); | ||
}; | ||
co_return co_await wrapper_lambda(); | ||
} | ||
} // namespace lambdas | ||
|
||
namespace std_function { | ||
namespace std { | ||
template <typename> class function; | ||
|
||
template <typename ReturnValue, typename... Args> | ||
class function<ReturnValue(Args...)> { | ||
public: | ||
template <typename T> function &operator=(T) {} | ||
template <typename T> function(T) {} | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
ReturnValue operator()(Args... args) const { | ||
return callable_->Invoke(args...); // expected-note {{in instantiation of member}} | ||
} | ||
|
||
private: | ||
class Callable { | ||
public: | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
ReturnValue Invoke(Args...) const { return {}; } | ||
}; | ||
Callable* callable_; | ||
}; | ||
} // namespace std | ||
|
||
void use_std_function() { | ||
std::function<int(bool)> foo = [](bool b) { return b ? 1 : 2; }; | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
std::function<Gen<int>(bool)> test1 = [](bool b) { | ||
return foo_coro(b); | ||
}; | ||
std::function<Gen<int>(bool)> test2 = [](bool) -> Gen<int> { | ||
co_return 1; | ||
}; | ||
std::function<Gen<int>(bool)> test3 = foo_coro; | ||
|
||
foo(true); // Fine. | ||
test1(true); // expected-note 2 {{in instantiation of member}} | ||
test2(true); | ||
test3(true); | ||
} | ||
} // namespace std_function | ||
|
||
// different_promise_type | ||
class [[clang::coro_return_type]] Task{}; | ||
struct my_promise_type { | ||
Task get_return_object() { | ||
return {}; | ||
} | ||
suspend_always initial_suspend(); | ||
suspend_always final_suspend() noexcept; | ||
void unhandled_exception(); | ||
}; | ||
namespace std { | ||
template<> class coroutine_traits<Task, int> { | ||
using promise_type = my_promise_type; | ||
}; | ||
} // namespace std | ||
// expected-error@+1 {{neither a coroutine nor a coroutine wrapper}} | ||
Task foo(int) { return Task{}; } |
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.