public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dvratil at kde dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/105406] New: coroutines: since 11.3 co_await attempts to copy a move-only value when await_transform(T &) exists
Date: Wed, 27 Apr 2022 12:28:38 +0000	[thread overview]
Message-ID: <bug-105406-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105406
           Summary: coroutines: since 11.3 co_await attempts to copy a
                    move-only value when await_transform(T &) exists
           Product: gcc
           Version: 11.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dvratil at kde dot org
  Target Milestone: ---

Starting with gcc 11.3, gcc tries to copy an object to await_transform() even
if an overload that accepts a reference exists:

#include <coroutine>
#include <exception>

// A move-only awaitable
class MoveOnlyAwaitable {
public:
    MoveOnlyAwaitable() = default;
    MoveOnlyAwaitable(MoveOnlyAwaitable &&) = default;
    MoveOnlyAwaitable &operator=(MoveOnlyAwaitable &&) = default;

    MoveOnlyAwaitable(const MoveOnlyAwaitable &) = delete;
    MoveOnlyAwaitable &operator=(const MoveOnlyAwaitable &) = delete;

    bool await_ready() const noexcept { return false; }
    void await_suspend(std::coroutine_handle<>) noexcept {}
    void await_resume() {}
};

struct task {
    struct promise_type {
        auto initial_suspend() const { return std::suspend_never{}; }
        auto final_suspend() const noexcept { return std::suspend_never(); }
        auto get_return_object() { return task{}; }
        void return_void() {}
        void unhandled_exception() {}

        template<typename T>
        T &&await_transform(T &&t) {
            return static_cast<T &&>(t);
        }


    };

    bool await_ready() const { return false; }
    void await_suspend(std::coroutine_handle<> awaiter) {}
    void await_resume() {}
};

task myCoroutine() {
    // GCC: OK
    // clang: OK
    {
        co_await MoveOnlyAwaitable();
    }
    // GCC: OK
    // clang: OK
    {
        auto moveonly = MoveOnlyAwaitable();
        co_await std::move(moveonly);
    }

    // GCC <= 11.2: OK
    // GCC 11.3:ERROR:  error: use of deleted function
'MoveOnlyAwaitable::MoveOnlyAwaitable(const MoveOnlyAwaitable&)
    // clang: OK
    {
        auto moveonly = MoveOnlyAwaitable();
        co_await moveonly;
    }
}


Both gcc<11.3 and clang invoke MoveOnlyAwaitable&
task::promise_type::await_transform<MoveOnlyAwaitable&>(MoveOnlyAwaitable&) for
the last co_await, while GCC 11.3 and on seems to attempt to pass `moveOnly` by
value.

Manually instantiating the template, or creating a non-template overload that
accepts MoveOnlyAwaitable& doesn't work either.


I believe that this is a bug because calling

auto move_only = MoveOnlyAwaitable();
task::promise_type p;
p.await_transform(moveonly);

works even with GCC 11.3. As expected, the compiler produces the
await_transform<MoveOnlyAwaitable&>(MoveOnlyAwaitable&) overload, so it's weird
that it doesn't do so when `move_only` is passed to `co_await`.

             reply	other threads:[~2022-04-27 12:28 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-27 12:28 dvratil at kde dot org [this message]
2022-04-27 12:29 ` [Bug c++/105406] " dvratil at kde dot org
2022-04-27 12:36 ` [Bug c++/105406] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-04-27 13:12 ` jakub at gcc dot gnu.org
2023-03-16 12:01 ` [Bug c++/105406] [11/12/13 " cvs-commit at gcc dot gnu.org
2023-04-18 20:45 ` [Bug c++/105406] [11/12 " cvs-commit at gcc dot gnu.org
2023-04-18 20:50 ` [Bug c++/105406] [11 " jason at gcc dot gnu.org
2023-04-22  0:22 ` cvs-commit at gcc dot gnu.org
2023-05-29 10:06 ` jakub at gcc dot gnu.org
2023-08-11 19:32 ` jason 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-105406-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).