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 // UNSUPPORTED: ubsan
11
12 // <coroutine>
13 // struct noop_coroutine_promise;
14 // using noop_coroutine_handle = coroutine_handle<noop_coroutine_promise>;
15 // noop_coroutine_handle noop_coroutine() noexcept;
16
17 #include <coroutine>
18 #include <cassert>
19 #include <type_traits>
20
21 #include "test_macros.h"
22
23 static_assert(std::is_same<std::coroutine_handle<std::noop_coroutine_promise>, std::noop_coroutine_handle>::value, "");
24 static_assert(std::is_same<decltype(std::noop_coroutine()), std::noop_coroutine_handle>::value, "");
25
26 // template <> struct coroutine_handle<noop_coroutine_promise> : coroutine_handle<>
27 // {
28 // // [coroutine.handle.noop.observers], observers
29 // constexpr explicit operator bool() const noexcept;
30 // constexpr bool done() const noexcept;
31
32 // // [coroutine.handle.noop.resumption], resumption
33 // constexpr void operator()() const noexcept;
34 // constexpr void resume() const noexcept;
35 // constexpr void destroy() const noexcept;
36
37 // // [coroutine.handle.noop.promise], promise access
38 // noop_coroutine_promise& promise() const noexcept;
39
40 // // [coroutine.handle.noop.address], address
41 // constexpr void* address() const noexcept;
42
main(int,char **)43 int main(int, char**)
44 {
45 auto h = std::noop_coroutine();
46 std::coroutine_handle<> base = h;
47
48 assert(h);
49 assert(base);
50
51 assert(!h.done());
52 assert(!base.done());
53
54 h.resume();
55 h.destroy();
56 h();
57 static_assert(h, "");
58 static_assert(h.done() == false, "");
59
60 // [coroutine.handle.noop.resumption]p2
61 // Remarks: If noop_coroutine_handle is converted to
62 // coroutine_handle<>, calls to operator(), resume and
63 // destroy on that handle will also have no observable
64 // effects.
65 base.resume();
66 base.destroy();
67 base();
68 assert(base);
69 assert(base.done() == false);
70
71 TEST_IGNORE_NODISCARD h.promise();
72 assert(h.address() == base.address());
73 assert(h == base);
74 assert(h.address() != nullptr);
75 assert(std::coroutine_handle<>::from_address(h.address()) == base);
76
77 return 0;
78 }
79