From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id EA0293858CDA; Fri, 3 Nov 2023 06:30:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EA0293858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1698993025; bh=MfY4QpuhDXlaY5GN5iGAs7o3Au/LtOcX7Y9koMkmDc4=; h=From:To:Subject:Date:From; b=JVLF14PhG/9uyK/64Au3ZPY6hhwbJwGMUuRu894JZbJrxg7/P59+0+ydhy1Ohym65 O8EpXNi4cqKJHy/8OYYcMNjqcy2NwtcWVfIXMYMbP2NRDgWkZxRHLvE3Mr/5U9paNs Rc3B1YFG/O93FIaAiqsC4PJ0kPwMCu78oQSi6ywo= From: "oremanj at mit dot edu" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/112360] New: [coroutines] unreachable 'co_await' still creates a suspension point Date: Fri, 03 Nov 2023 06:30:24 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: oremanj at mit dot edu X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112360 Bug ID: 112360 Summary: [coroutines] unreachable 'co_await' still creates a suspension point Product: gcc Version: 14.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: oremanj at mit dot edu Target Milestone: --- A coroutine function of the form: task example() { do { co_await awaitable{}; } while (false && co_await awaitable{}); co_return; } will execute the awaitable's await_ready() and await_suspend() methods twic= e, even though execution of the abstract machine only reaches one co_await statement. This reproduces on several tested versions from 11.1 to current master, at either of optimization level -O0 or -O2. While the construct may seem a bit nonsensical, it does show up in practice; it's simplified from something that appears after macro expansion when using the popular Catch2 testing library if you write an assertion about the value of a co_await expression. Compiler Explorer link: https://godbolt.org/z/EoP7EnEnf Full test case is below. On gcc it prints: ready suspend resume ready suspend returned final suspend clang (14 through 17) shows the more expected ready suspend resume returned final suspend command line: gcc -std=3Dc++20 -Wall -Wextra t.c -o t --- test case follows --- #include extern "C" int puts(const char* s); struct task { struct promise_type { std::suspend_always initial_suspend() { return {}; } std::suspend_always final_suspend() noexcept { puts("final suspend"); return {}; } void return_void() { puts("returned"); } void unhandled_exception() {} task get_return_object() { return task{this}; } }; promise_type* promise; }; struct awaitable { bool await_ready() const noexcept { puts("ready"); return false; } void await_suspend(std::coroutine_handle<> h) { puts("suspend"); h.resume(); } bool await_resume() { puts("resume"); return false; } }; task example() { do { co_await awaitable{}; } while (false && co_await awaitable{}); co_return; } int main() { using handle =3D std::coroutine_handle; task t =3D example(); handle h =3D handle::from_promise(*t.promise); h.resume(); }=