[Bug c++/101027] New: Short-circuit behavior not respected with co_await in while head

2021-06-11 Thread alois1--- via 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: alo...@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 

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<>) {}
bool await_resume() { return true; }

std::coroutine_handle 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.

[Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially

2021-08-19 Thread alois1--- via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101976

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: alo...@gmx-topmail.de
  Target Milestone: ---

Created attachment 51324
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51324&action=edit
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=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --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=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_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) 

$ g++ -std=c++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_move
(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 have
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.