Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/pr110872.C

raw
1 // { dg-additional-options "-fcontracts -fcontract-continuation-mode=on" }
2 // { dg-do run }
3 // { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
4
5 #include <iostream>
6 #include <coroutine>
7
8
9 template <typename T>
10 struct generator
11 {
12 struct promise_type
13 {
14 std::suspend_always yield_value(T) { return {}; }
15
16 std::suspend_always initial_suspend() const noexcept { return {}; }
17 std::suspend_never final_suspend() const noexcept { return {}; }
18 void unhandled_exception() noexcept {}
19
20 generator<T> get_return_object() noexcept { return {}; }
21 };
22
23 bool is_valid() { return false; }
24 };
25
26 namespace std {
27 template <typename T, typename... Args>
28 struct coroutine_traits<generator<T>, Args...>
29 {
30 using promise_type = typename generator<T>::promise_type;
31 };
32
33 };
34
35 generator<int> val(int v)
36 [[post g: g.is_valid()]]
37 {
38 std::cout << "coro initial" << std::endl;
39 co_yield v;
40 std::cout << "coro resumed" << std::endl;
41 }
42
43 int main() {
44 std::cout << "main initial" << std::endl;
45 generator<int> s = val(1);
46 (void)s;
47 std::cout << "main continues" << std::endl;
48 }
49
50 // { dg-output "contract violation in function val at .*.C:36: g.is_valid().*(\n|\r\n|\r)" }