public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head
@ 2021-06-11  9:02 alois1@gmx-topmail.de
  2021-11-30 20:50 ` [Bug c++/101027] " kiwixz at outlook dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: alois1@gmx-topmail.de @ 2021-06-11  9:02 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101027
           Summary: Short-circuit behavior not respected with co_await in
                    while head
           Product: gcc
           Version: 11.1.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 50983
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50983&action=edit
Reduced preprocessed code

GCC miscompiles the following C++ program:

$ cat test.cpp
#include <coroutine>

struct task
{
        struct promise_type
        {
                task get_return_object() { return
{std::coroutine_handle<promise_type>::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<>) {}
        bool await_resume() { return true; }

        std::coroutine_handle<promise_type> m_handle;
};

extern "C" void exit(int status);

task should_not_execute()
{
        exit(1);
        co_return;
}

task main_task()
{
        while (false && co_await should_not_execute());
}

int main()
{
        main_task();
}

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d,jit
--enable-offload-targets=nvptx-none,amdgcn-amdhsa, --without-cuda-driver
--enable-host-shared --enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/11 --enable-ssp --disable-libssp
--disable-libvtv --enable-cet=auto --disable-libcc1 --enable-plugin
--with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-11 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic
--with-build-config=bootstrap-lto-lean --enable-link-mutex
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.1 20210510 [revision 23855a176609fe8dda6abaf2b21846b4517966eb]
(SUSE Linux) 

$ g++ -std=c++20 test.cpp; ./a.out; echo $?
1

Expected output would be 0, as the && operator should short-circuit, and
should_not_execute therefore should never be started (in particular, the call
to exit should not be reached).

Interestingly, the unexpected behavior only happens in a while head, in an if
head or assignment to a variable the short-circuit behavior seems to be
respected.

For convenience, I have attached a manually reduced version of the preprocessed
code that I have confirmed to exhibit the same behavior.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/101027] Short-circuit behavior not respected with co_await in while head
  2021-06-11  9:02 [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head alois1@gmx-topmail.de
@ 2021-11-30 20:50 ` kiwixz at outlook dot com
  2023-08-02 15:10 ` sichert at in dot tum.de
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: kiwixz at outlook dot com @ 2021-11-30 20:50 UTC (permalink / raw)
  To: gcc-bugs

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

kiwixz at outlook dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kiwixz at outlook dot com

--- Comment #1 from kiwixz at outlook dot com ---
Hello, I encountered the same problem with gcc evaluating the two asio
coroutine operands of && even when the left one returned false.

See on godbolt: https://godbolt.org/z/Wo8z5PoP3

Expected output (confirmed on clang 12 with libc++):
a
0

GCC 11.2 or trunk:
a
b
0

---

#include <iostream>
#include <boost/asio.hpp>

namespace asio = boost::asio;

asio::awaitable<bool> a() {
    std::cout << "a\n";
    co_return false;
}

asio::awaitable<bool> b() {
    std::cout << "b\n";
    co_return false;
}

asio::awaitable<void> f() {
    std::cout << ((co_await a()) && (co_await b())) << "\n";
}

int main() {
    asio::io_context io;
    asio::co_spawn(io, f, asio::detached);
    io.run();
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/101027] Short-circuit behavior not respected with co_await in while head
  2021-06-11  9:02 [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head alois1@gmx-topmail.de
  2021-11-30 20:50 ` [Bug c++/101027] " kiwixz at outlook dot com
@ 2023-08-02 15:10 ` sichert at in dot tum.de
  2023-11-03 19:04 ` pinskia at gcc dot gnu.org
  2023-11-03 19:04 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: sichert at in dot tum.de @ 2023-08-02 15:10 UTC (permalink / raw)
  To: gcc-bugs

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

Moritz Sichert <sichert at in dot tum.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sichert at in dot tum.de

--- Comment #2 from Moritz Sichert <sichert at in dot tum.de> ---
Created attachment 55677
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55677&action=edit
Coroutine that segfaults because it does not short-circuit

I have encountered the same problem where co_await in a logical operation does
not short-circuit correctly. See the attached example.

The code segfaults because in the condition "foo && co_await foo->foo(123)" it
starts executing the coroutine even if foo is nullptr. For some reason the
error only occurs if the condition is nested in a logical-|| expression.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/101027] Short-circuit behavior not respected with co_await in while head
  2021-06-11  9:02 [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head alois1@gmx-topmail.de
  2021-11-30 20:50 ` [Bug c++/101027] " kiwixz at outlook dot com
  2023-08-02 15:10 ` sichert at in dot tum.de
@ 2023-11-03 19:04 ` pinskia at gcc dot gnu.org
  2023-11-03 19:04 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-03 19:04 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |oremanj at mit dot edu

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 112360 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/101027] Short-circuit behavior not respected with co_await in while head
  2021-06-11  9:02 [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head alois1@gmx-topmail.de
                   ` (2 preceding siblings ...)
  2023-11-03 19:04 ` pinskia at gcc dot gnu.org
@ 2023-11-03 19:04 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-03 19:04 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-11-03
     Ever confirmed|0                           |1

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-11-03 19:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-11  9:02 [Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head alois1@gmx-topmail.de
2021-11-30 20:50 ` [Bug c++/101027] " kiwixz at outlook dot com
2023-08-02 15:10 ` sichert at in dot tum.de
2023-11-03 19:04 ` pinskia at gcc dot gnu.org
2023-11-03 19:04 ` pinskia at gcc dot gnu.org

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).