c++gccc++20c++-coroutinegcc12

co_await inside catch no longer compiling with GCC12


I have a code that looks like this:

auto func() -> asio::awaitable<void> {
    try {
        co_await async_operation();
    } catch(boost::system::system_error const& e) {
        co_return co_await another_async_operation();
    }
}

This code worked perfectly with GCC 11, but with GCC 12 it won't compile:

file.cpp:3:19: error: await expressions are not permitted in handlers
  3 |         co_return co_await another_async_operation();
    |                   ^~~~~~~~

Why is that and how can I fix it?


Solution

  • This is explicitly forbidden in [expr.await]/2:

    An await-expression shall appear only in a potentially-evaluated expression within the compound-statement of a function-body outside of a handler ([except.pre]).

    The error message here is pretty clear: you can't await in an exception handler. That it compiled before is a bug, this was always the rule (P0912R5).