| 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 |
// In order to test the contract violation diagnostic, we have to set |
| 9 |
// -fcontract-continuation-mode=on; this means that the code will emit |
| 10 |
// the message below - but before that the contract should have been checked. |
| 11 |
void process(int from, int to) |
| 12 |
{ |
| 13 |
if (from > to) |
| 14 |
std::cout << "would have been a disaster!" << std::endl; |
| 15 |
} |
| 16 |
|
| 17 |
template <typename T> |
| 18 |
struct generator |
| 19 |
{ |
| 20 |
struct promise_type |
| 21 |
{ |
| 22 |
template <typename... Args> |
| 23 |
promise_type(Args&&... args) { |
| 24 |
std::cout << "promise init" << std::endl; |
| 25 |
process(args...); |
| 26 |
} |
| 27 |
|
| 28 |
std::suspend_always yield_value(T) { return {}; } |
| 29 |
|
| 30 |
std::suspend_always initial_suspend() const noexcept { return {}; } |
| 31 |
std::suspend_never final_suspend() const noexcept { return {}; } |
| 32 |
void unhandled_exception() noexcept {} |
| 33 |
|
| 34 |
generator<T> get_return_object() noexcept { return {}; } |
| 35 |
}; |
| 36 |
}; |
| 37 |
|
| 38 |
namespace std { |
| 39 |
template <typename T, typename... Args> |
| 40 |
struct coroutine_traits<generator<T>, Args...> |
| 41 |
{ |
| 42 |
using promise_type = typename generator<T>::promise_type; |
| 43 |
}; |
| 44 |
|
| 45 |
}; |
| 46 |
|
| 47 |
generator<int> seq(int from, int to) [[pre: from <= to]] |
| 48 |
|
| 49 |
{ |
| 50 |
std::cout << "coro initial" << std::endl; |
| 51 |
for (int i = from; i <= to; ++i) { |
| 52 |
co_yield i; |
| 53 |
std::cout << "coro resumed" << std::endl; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
int main() { |
| 58 |
std::cout << "main initial" << std::endl; |
| 59 |
generator<int> s = seq(10, 5); |
| 60 |
(void)s; |
| 61 |
std::cout << "main continues" << std::endl; |
| 62 |
} |
| 63 |
|
| 64 |
// { dg-output "contract violation in function seq at .*.C:47: from \<= to.*(\n|\r\n|\r)" } |