xref: /llvm-project/clang/test/SemaCXX/coroutine-alloc-2.cpp (revision 69e920d42677872ac39d3475312092404193bf24)
1 // Tests that we wouldn't generate an allocation call in promise_type with (std::size_t, std::nothrow_t) in case we find promsie_type::get_return_object_on_allocation_failure;
2 // RUN: %clang_cc1 %s -std=c++20 %s -fsyntax-only -verify
3 
4 namespace std {
5 template <typename... T>
6 struct coroutine_traits;
7 
8 template <class Promise = void>
9 struct coroutine_handle {
10   coroutine_handle() = default;
from_addressstd::coroutine_handle11   static coroutine_handle from_address(void *) noexcept { return {}; }
12 };
13 
14 template <>
15 struct coroutine_handle<void> {
from_addressstd::coroutine_handle16   static coroutine_handle from_address(void *) { return {}; }
17   coroutine_handle() = default;
18   template <class PromiseType>
coroutine_handlestd::coroutine_handle19   coroutine_handle(coroutine_handle<PromiseType>) noexcept {}
20 };
21 
22 struct suspend_always {
await_readystd::suspend_always23   bool await_ready() noexcept { return false; }
await_suspendstd::suspend_always24   void await_suspend(std::coroutine_handle<>) noexcept {}
await_resumestd::suspend_always25   void await_resume() noexcept {}
26 };
27 
28 struct nothrow_t {};
29 constexpr nothrow_t nothrow = {};
30 
31 } // end namespace std
32 
33 using SizeT = decltype(sizeof(int));
34 
35 struct promise_on_alloc_failure_tag {};
36 
37 template <>
38 struct std::coroutine_traits<int, promise_on_alloc_failure_tag> {
39   struct promise_type {
get_return_objectstd::coroutine_traits::promise_type40     int get_return_object() { return 0; }
initial_suspendstd::coroutine_traits::promise_type41     std::suspend_always initial_suspend() { return {}; }
final_suspendstd::coroutine_traits::promise_type42     std::suspend_always final_suspend() noexcept { return {}; }
return_voidstd::coroutine_traits::promise_type43     void return_void() {}
unhandled_exceptionstd::coroutine_traits::promise_type44     void unhandled_exception() {}
get_return_object_on_allocation_failurestd::coroutine_traits::promise_type45     static int get_return_object_on_allocation_failure() { return -1; }
46     void *operator new(SizeT, std::nothrow_t) noexcept;
47   };
48 };
49 
f(promise_on_alloc_failure_tag)50 extern "C" int f(promise_on_alloc_failure_tag) { // expected-error 1+{{is not usable with the function signature of 'f'}}
51     co_return;
52 }
53