From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 9D4E13858400; Thu, 19 Aug 2021 10:43:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D4E13858400 From: "alois1@gmx-topmail.de" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially Date: Thu, 19 Aug 2021 10:43:07 +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.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: alois1@gmx-topmail.de 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 attachments.created 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: Thu, 19 Aug 2021 10:43:07 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D101976 Bug ID: 101976 Summary: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: alois1@gmx-topmail.de Target Milestone: --- Created attachment 51324 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=3D51324&action=3Dedit Reduced preprocessed code GCC miscompiles the following C++ program: $ cat test.cpp #include struct task { struct promise_type { task get_return_object() { return {std::coroutine_handle::from_promise(*this)}; } std::suspend_never initial_suspend() { return {}; } std::suspend_never final_suspend() noexcept { return {}; } void unhandled_exception() {} void return_void() {} }; bool await_ready() { return true; } void await_suspend(std::coroutine_handle<>) {} void await_resume() { } std::coroutine_handle m_handle; }; extern "C" int puts(const char *s); struct nontrivial_move { nontrivial_move() { puts("nontrivial_move()"); } nontrivial_move(nontrivial_move &&) { puts("nontrivial_move(nontrivial_move &&)"); } ~nontrivial_move() { puts("~nontrivial_move()"); } char buf[128]{}; }; struct wrapper { nontrivial_move member; }; task subtask(wrapper) { co_return; } task main_task() { co_await subtask({}); } int main() { main_task(); } $ gcc -v Using built-in specs. COLLECT_GCC=3D/usr/bin/gcc COLLECT_LTO_WRAPPER=3D/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper OFFLOAD_TARGET_NAMES=3Dnvptx-none OFFLOAD_TARGET_DEFAULT=3D1 Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=3Dc,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=3D/usr --mandir=3D/usr/share/man --infodir=3D/usr/share/info --with-bugurl=3Dhttp://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=3Dposix --enable-checking=3Drelease --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=3Dgnu --enable-plugin --enable-initfini-array --with-isl=3D/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-li= nux/isl-install --enable-offload-targets=3Dnvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=3Dgeneric --with-arch_32=3Di686 --build=3Dx86_64-redhat-linux Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC)=20 $ g++ -std=3Dc++20 test.cpp; ./a.out nontrivial_move() nontrivial_move(nontrivial_move &&) ~nontrivial_move() ~nontrivial_move() ~nontrivial_move() Examination of the generated assembly code shows that the temporary wrapper object is erroneously copied under the assumption that it has trivial move constructor (the large buffer in nontrivial_move is just there to make this fact obvious). This behavior does not happen if the construction of the wrapper object or = the co_await happens in a separate statement. Furthermore, the behavior does not happen if the only argument of subtask is changed to have type nontrivial_m= ove (the move is elided entirely in this case, which is acceptable as far as I = can tell). A manually reduced version of the preprocessed code is attached, which I ha= ve confirmed to show the same behavior. I think that the observed incorrect behavior might be a symptom of the same underlying issue as one or more of the bugs 99576, 100611, 101243, 101367.=