1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 // UNSUPPORTED: c++98, c++03, c++11 11 12 #include <experimental/coroutine> 13 #include <cassert> 14 15 #include "test_macros.h" 16 17 using namespace std::experimental; 18 19 struct coro_t { 20 struct promise_type { get_return_objectcoro_t::promise_type21 coro_t get_return_object() { 22 coroutine_handle<promise_type>{}; 23 return {}; 24 } initial_suspendcoro_t::promise_type25 suspend_never initial_suspend() { return {}; } final_suspendcoro_t::promise_type26 suspend_never final_suspend() { return {}; } return_voidcoro_t::promise_type27 void return_void() {} unhandled_exceptioncoro_t::promise_type28 static void unhandled_exception() {} 29 }; 30 }; 31 32 struct B { ~BB33 ~B() {} await_readyB34 bool await_ready() { return true; } await_resumeB35 B await_resume() { return {}; } await_suspendB36 template <typename F> void await_suspend(F) {} 37 }; 38 39 40 struct A { ~AA41 ~A() {} await_readyA42 bool await_ready() { return true; } await_resumeA43 int await_resume() { return 42; } await_suspendA44 template <typename F> void await_suspend(F) {} 45 }; 46 47 int last_value = -1; set_value(int x)48void set_value(int x) { 49 last_value = x; 50 } 51 f(int n)52coro_t f(int n) { 53 if (n == 0) { 54 set_value(0); 55 co_return; 56 } 57 int val = co_await A{}; 58 ((void)val); 59 set_value(42); 60 } 61 g()62coro_t g() { B val = co_await B{}; } 63 main(int,char **)64int main(int, char**) { 65 last_value = -1; 66 f(0); 67 assert(last_value == 0); 68 f(1); 69 assert(last_value == 42); 70 71 return 0; 72 } 73