From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 404FA385842B; Wed, 27 Apr 2022 12:28:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 404FA385842B From: "dvratil at kde dot 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 11.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dvratil at kde dot org 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 27 Apr 2022 12:28:39 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D105406 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() ev= en if an overload that accepts a reference exists: #include #include // A move-only awaitable class MoveOnlyAwaitable { public: MoveOnlyAwaitable() =3D default; MoveOnlyAwaitable(MoveOnlyAwaitable &&) =3D default; MoveOnlyAwaitable &operator=3D(MoveOnlyAwaitable &&) =3D default; MoveOnlyAwaitable(const MoveOnlyAwaitable &) =3D delete; MoveOnlyAwaitable &operator=3D(const MoveOnlyAwaitable &) =3D 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 T &&await_transform(T &&t) { return static_cast(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 =3D MoveOnlyAwaitable(); co_await std::move(moveonly); } // GCC <=3D 11.2: OK // GCC 11.3:ERROR: error: use of deleted function 'MoveOnlyAwaitable::MoveOnlyAwaitable(const MoveOnlyAwaitable&) // clang: OK { auto moveonly =3D MoveOnlyAwaitable(); co_await moveonly; } } Both gcc<11.3 and clang invoke MoveOnlyAwaitable& task::promise_type::await_transform(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 th= at accepts MoveOnlyAwaitable& doesn't work either. I believe that this is a bug because calling auto move_only =3D MoveOnlyAwaitable(); task::promise_type p; p.await_transform(moveonly); works even with GCC 11.3. As expected, the compiler produces the await_transform(MoveOnlyAwaitable&) overload, so it's w= eird that it doesn't do so when `move_only` is passed to `co_await`.=