public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "lewissbaker.opensource at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/97452] New: [coroutines] incorrect sequencing of await_resume() when multiple co_await expressions occur in a single statement
Date: Fri, 16 Oct 2020 01:40:26 +0000	[thread overview]
Message-ID: <bug-97452-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97452

            Bug ID: 97452
           Summary: [coroutines] incorrect sequencing of await_resume()
                    when multiple co_await expressions occur in a single
                    statement
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lewissbaker.opensource at gmail dot com
  Target Milestone: ---

See https://godbolt.org/z/9Kj9o8

Compile the following program with GCC trunk and flags: -std=c++20

----
#include <coroutine>
#include <exception>
#include <utility>
#include <cassert>
#include <cstdio>

struct task {
    struct promise_type {
        task get_return_object() noexcept {
            return
task{std::coroutine_handle<promise_type>::from_promise(*this)};
        }
        std::suspend_always initial_suspend() noexcept {
            std::puts("task initial_suspend");
            return {};
        }

        struct yield_awaiter {
            bool await_ready() noexcept { return false; }
            auto await_suspend(std::coroutine_handle<promise_type> h) noexcept
{
                std::puts("task yielding/returning value");
                return std::exchange(h.promise().continuation, {});
            }
            void await_resume() noexcept {
                std::puts("task resumed");
            }
        };

        yield_awaiter yield_value(int x) noexcept {
            value = x;
            return {};
        }

        void return_value(int x) noexcept {
            value = x;
        }

        yield_awaiter final_suspend() noexcept {
            return {};
        }

        [[noreturn]] void unhandled_exception() noexcept {
            std::terminate();
        }

        int value;
        std::coroutine_handle<> continuation;
    };

    explicit task(std::coroutine_handle<promise_type> coro) noexcept
    : coro(coro)
    {}

    task(task&& t) noexcept
    : coro(std::exchange(t.coro, {}))
    {}

    ~task() {
        if (coro) coro.destroy();
    }

    __attribute__((noinline)) bool await_ready() {
        std::puts("task::await_ready");
        return false;
    }

    __attribute__((noinline)) auto await_suspend(std::coroutine_handle<> h)
noexcept {
        std::puts("task::await_suspend");
        coro.promise().continuation = h;
        return coro;
    }

    __attribute__((noinline)) int await_resume() {
        std::puts("task::await_resume");
        return coro.promise().value;
    }

    std::coroutine_handle<promise_type> coro;
};

task two_values() {
    co_yield 1;
    co_return 2;
}

task example() {
    task t = two_values();
    int result = co_await t + co_await t;
    std::printf("result = %i (should be 3)\n", result);
    std::fflush(stdout);
    co_return result;
}

int main() {
    task t = example();
    t.coro.resume();
    assert(t.coro.done());
}
----

Expected output:
----
task initial_suspend
task initial_suspend
task::await_ready
task::await_suspend
task yielding/returning value
task::await_resume
task::await_ready
task::await_suspend
task resumed
task yielding/returning value
task::await_resume
result = 3 (should be 3)

Actual output:
----
task initial_suspend
task initial_suspend
task::await_ready
task::await_suspend
task yielding/returning value
task::await_ready
task::await_suspend
task resumed
task yielding/returning value
task::await_resume
task::await_resume
result = 4 (should be 3)


Note that the output indicates the the compiler is only calling await_resume()
on both awaiters immediately before evaluating the plus-expression rather than
evaluating await_resume() on the first awaiter immediately upon resuming from
the first suspend-point and before evaluating await_ready() for the second
awaiter.

             reply	other threads:[~2020-10-16  1:40 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16  1:40 lewissbaker.opensource at gmail dot com [this message]
2020-10-16  6:55 ` [Bug c++/97452] " iains at gcc dot gnu.org
2020-10-16  7:04 ` iains at gcc dot gnu.org
2020-10-17  3:15 ` davidledger at live dot com.au
2020-10-17  7:30 ` iains at gcc dot gnu.org
2020-10-29  6:11 ` davidledger at live dot com.au
2020-10-29  7:52 ` iains at gcc dot gnu.org
2020-12-02  9:01 ` davidledger at live dot com.au
2020-12-02  9:02 ` iains at gcc dot gnu.org
2021-04-08 12:02 ` rguenth at gcc dot gnu.org
2021-04-09 10:23 ` lewissbaker.opensource at gmail dot com
2021-05-04 12:31 ` rguenth at gcc dot gnu.org
2022-06-28 10:42 ` jakub at gcc dot gnu.org
2022-11-12  3:31 ` davidledger at live dot com.au
2023-01-19 14:08 ` davidledger at live dot com.au
2023-07-07  9:10 ` rguenth at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97452-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).