| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | #include <coroutine> |
| 12 | #include <cassert> |
| 13 | |
| 14 | #include "test_macros.h" |
| 15 | |
| 16 | struct coro_t { |
| 17 | struct promise_type { |
| 18 | coro_t get_return_object() { |
| 19 | std::coroutine_handle<promise_type>{}; |
| 20 | return {}; |
| 21 | } |
| 22 | std::suspend_never initial_suspend() { return {}; } |
| 23 | std::suspend_never final_suspend() noexcept { return {}; } |
| 24 | void return_void() {} |
| 25 | static void unhandled_exception() {} |
| 26 | }; |
| 27 | }; |
| 28 | |
| 29 | struct B { |
| 30 | ~B() {} |
| 31 | bool await_ready() { return true; } |
| 32 | B await_resume() { return {}; } |
| 33 | template <typename F> void await_suspend(F) {} |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | struct A { |
| 38 | ~A() {} |
| 39 | bool await_ready() { return true; } |
| 40 | int await_resume() { return 42; } |
| 41 | template <typename F> void await_suspend(F) {} |
| 42 | }; |
| 43 | |
| 44 | int last_value = -1; |
| 45 | void set_value(int x) { |
| 46 | last_value = x; |
| 47 | } |
| 48 | |
| 49 | coro_t f(int n) { |
| 50 | if (n == 0) { |
| 51 | set_value(0); |
| 52 | co_return; |
| 53 | } |
| 54 | int val = co_await A{}; |
| 55 | ((void)val); |
| 56 | set_value(42); |
| 57 | } |
| 58 | |
| 59 | coro_t g() { B val = co_await B{}; } |
| 60 | |
| 61 | int main(int, char**) { |
| 62 | last_value = -1; |
| 63 | f(n: 0); |
| 64 | assert(last_value == 0); |
| 65 | f(n: 1); |
| 66 | assert(last_value == 42); |
| 67 | |
| 68 | return 0; |
| 69 | } |
| 70 | |