From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 4A0063857C63; Sat, 13 Mar 2021 13:54:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A0063857C63 From: "lemourin at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99576] New: [coroutines] desctructor of a temporary called too early within co_await expression Date: Sat, 13 Mar 2021 13:54:16 +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: 10.2.1 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: lemourin at gmail dot com 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 keywords 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: Sat, 13 Mar 2021 13:54:16 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D99576 Bug ID: 99576 Summary: [coroutines] desctructor of a temporary called too early within co_await expression Product: gcc Version: 10.2.1 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: lemourin at gmail dot com Target Milestone: --- ``` #if __has_include() #include namespace stdx { using std::coroutine_handle; using std::noop_coroutine; using std::suspend_always; using std::suspend_never; } // namespace stdx #elif __has_include() #include namespace stdx { using std::experimental::coroutine_handle; using std::experimental::noop_coroutine; using std::experimental::suspend_always; using std::experimental::suspend_never; } // namespace stdx #else #error "coroutines unsupported" #endif #include #include #include class Task { public: struct promise_type { struct final_awaitable { bool await_ready() noexcept { return false; } auto await_suspend(stdx::coroutine_handle coro) noexcep= t { return coro.promise().continuation; } void await_resume() noexcept {} }; Task get_return_object() { return Task(stdx::coroutine_handle::from_promise(*this)= ); } stdx::suspend_always initial_suspend() { return {}; } final_awaitable final_suspend() noexcept { return {}; } void unhandled_exception() { std::terminate(); } void return_void() {} stdx::coroutine_handle continuation =3D stdx::noop_coroutine(); }; Task(const Task&) =3D delete; Task(Task&& other) noexcept : handle_(std::exchange(other.handle_, nullptr)) {} Task& operator=3D(const Task&) =3D delete; Task& operator=3D(Task&& other) noexcept { handle_ =3D std::exchange(other.handle_, nullptr); return *this; } ~Task() { if (handle_) { handle_.destroy(); } } bool await_ready() const { return false; } auto await_suspend(stdx::coroutine_handle continuation) { handle_.promise().continuation =3D continuation; return handle_; } void await_resume() {} private: explicit Task(stdx::coroutine_handle handle) : handle_(handle) {} stdx::coroutine_handle handle_; }; struct RunTask { struct promise_type { RunTask get_return_object() { return {}; } stdx::suspend_never initial_suspend() { return {}; } stdx::suspend_never final_suspend() noexcept { return {}; } void return_void() {} void unhandled_exception() { std::terminate(); } }; }; struct Foo { Foo() { std::cerr << "Foo()\n"; } ~Foo() { std::cerr << "~Foo()\n"; } }; Task DoAsync() { std::cerr << "START TASK\n"; co_return co_await [foo =3D Foo{}]() -> Task { std::cerr << "IN LAMBDA\n"; co_return; }(); } RunTask Main() { co_await DoAsync(); } int main() { Main(); } ``` gcc outputs: ` START TASK Foo() ~Foo() IN LAMBDA ` while clang and msvc output: ` START TASK Foo() IN LAMBDA ~Foo() ` Is this code undefined behavior? Or is clang and msvc wrong?=