public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102508] New: ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102)
@ 2021-09-27 21:58 delleyves at gmx dot ch
  2021-09-30  9:55 ` [Bug c++/102508] " marxin at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: delleyves at gmx dot ch @ 2021-09-27 21:58 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102508
           Summary: ICE on coroutine when awaiting inside a statement
                    expression (in transform_local_var_uses, at
                    cp/coroutines.cc:2102)
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: delleyves at gmx dot ch
  Target Milestone: ---

Created attachment 51513
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51513&action=edit
Preprocessed source.

The following code ICEs with a segmentation fault (release-checking) / assert
(all-checking):
```
#include <coroutine>
#include <stdexcept>
#include <outcome-experimental.hpp>

namespace outcome = OUTCOME_V2_NAMESPACE;

template <typename T>
using result = outcome::experimental::status_result<T>;


template <typename result_type>
struct CoroAwaiter {
  template <typename LazyAsync>
  explicit CoroAwaiter(LazyAsync &&lazy_async) {
    (void)std::forward<LazyAsync>(lazy_async)();
  }

  bool await_ready() noexcept;
  void await_suspend(std::coroutine_handle<> awaiting_coroutine) noexcept;
  result_type &&await_resume() noexcept;
};

template <typename R>
struct CoroBase {
  using result_type = R;

  struct promise_type {
    template <typename LazyAsync>
    auto await_transform(
        LazyAsync &&lazy_async) {
      using nested_result_type = typename std::invoke_result_t<LazyAsync
&&>::result_type;
      return
CoroAwaiter<nested_result_type>(std::forward<LazyAsync>(lazy_async));
    }

    #ifdef __cpp_exceptions
      void unhandled_exception() noexcept;
    #endif

    template <typename V>
    void return_value(V &&value) noexcept;

    std::suspend_never initial_suspend() noexcept;
    std::suspend_always final_suspend() noexcept;
    static CoroBase get_return_object_on_allocation_failure() noexcept;
    CoroBase get_return_object() noexcept;
  };


  CoroBase() = default;
  CoroBase(CoroBase &&) = default;
  CoroBase(const CoroBase &) = delete;
};

template <typename T>
using Coro = CoroBase<result<T>>;


[[nodiscard]] Coro<bool> bar() noexcept;

[[nodiscard]] Coro<void> foo() noexcept {
#if true
    bool ok = OUTCOME_CO_TRYX(co_await [&]() { return bar(); });
#else
    auto res = co_await [&]() { return bar(); };
    if(!res.has_value()) {
      co_return outcome::failure(std::move(res).error());
    }
    bool ok = res.value();
#endif
    (void)ok;
    co_return outcome::success();
}
```
Replace the `#if true` with `#if false` and the code compiles fine - despite
the two branches roughly having the same effect, though `OUTCOME_CO_TRYX`
utilises a Statement Expression (GCC / Clang extension).

Unfortunately, I haven't been able to reproduce the ICE without involving the
outcome library.

This issue seems to exist since as long as coroutines are available in GCC. In
particular, the code provided above ICEs on godbolt.org with a segfault on all
GCC 10.1 through 11.2 as well as (trunk) and (coroutines), when compiled with
"--std=c++20 -fcoroutines" and the outcome library enabled. The preprocessed
sources are from a local, checking GCC build of current master (11.2.1), where
instead of segfaulting it ICEs in an assert at `in transform_local_var_uses, at
cp/coroutines.cc:2102`.

The full compilation output is:
```
Using built-in specs.
COLLECT_GCC=/Users/yves/SDKs/gcc-11-20210926/bin/g++-11-20210926
Target: x86_64-apple-darwin20.6.0
Configured with: /var/tmp/yves/gcc-11-20210926_source/configure
--prefix=/Users/yves/SDKs/gcc-11-20210926 --enable-bootstrap
--enable-checking=yes --enable-languages=c,c++,lto
--with-default-libstdcxx-abi=new --enable-libstdcxx-debug --enable-plugin
--disable-multilib
--with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
--with-pkgversion=yves-Yves-MBookPro15 --program-suffix=-11-20210926
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.2.1 20210926 (yves-Yves-MBookPro15) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-D' 'EMBCPP_OUTCOME_SINGLE_HEADER' '-D'
'OUTCOME_DISABLE_EXECINFO' '-I'
'/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/../deps/outcome/single-header'
'-Wall' '-Wextra' '-fno-rtti' '-fno-exceptions' '-O0' '-g3' '-fcoroutines'
'-std=c++20' '-o' 'test.o' '-c' '-mmacosx-version-min=11.5.0'
'-asm_macosx_version_min=11.5' '-shared-libgcc' '-mtune=core2'

/Users/yves/SDKs/gcc-11-20210926/libexec/gcc/x86_64-apple-darwin20.6.0/11.2.1/cc1plus
-E -quiet -v -I
/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/../deps/outcome/single-header
-dD -D__DYNAMIC__ -D EMBCPP_OUTCOME_SINGLE_HEADER -D OUTCOME_DISABLE_EXECINFO
/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/test-case.cpp -fPIC
-mmacosx-version-min=11.5.0 -mtune=core2 -std=c++20 -Wall -Wextra -fno-rtti
-fno-exceptions -fcoroutines -g3 -fworking-directory -O0 -fpch-preprocess -o
test.ii
ignoring nonexistent directory
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory
"/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/../../../../x86_64-apple-darwin20.6.0/include"
ignoring nonexistent directory
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:

/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/../deps/outcome/single-header

/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/../../../../include/c++/11.2.1

/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/../../../../include/c++/11.2.1/x86_64-apple-darwin20.6.0

/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/../../../../include/c++/11.2.1/backward

/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/include

/Users/yves/SDKs/gcc-11-20210926/lib/gcc/x86_64-apple-darwin20.6.0/11.2.1/include-fixed
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-D' 'EMBCPP_OUTCOME_SINGLE_HEADER' '-D'
'OUTCOME_DISABLE_EXECINFO' '-I'
'/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/../deps/outcome/single-header'
'-Wall' '-Wextra' '-fno-rtti' '-fno-exceptions' '-O0' '-g3' '-fcoroutines'
'-std=c++20' '-o' 'test.o' '-c' '-mmacosx-version-min=11.5.0'
'-asm_macosx_version_min=11.5' '-shared-libgcc' '-mtune=core2'

/Users/yves/SDKs/gcc-11-20210926/libexec/gcc/x86_64-apple-darwin20.6.0/11.2.1/cc1plus
-fpreprocessed test.ii -fPIC -quiet -dumpbase test.cpp -dumpbase-ext .cpp
-mmacosx-version-min=11.5.0 -mtune=core2 -g3 -O0 -Wall -Wextra -std=c++20
-version -fno-rtti -fno-exceptions -fcoroutines -o test.s
GNU C++20 (yves-Yves-MBookPro15) version 11.2.1 20210926
(x86_64-apple-darwin20.6.0)
        compiled by GNU C version 11.2.1 20210926, GMP version 6.1.0, MPFR
version 3.1.6, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
GNU C++20 (yves-Yves-MBookPro15) version 11.2.1 20210926
(x86_64-apple-darwin20.6.0)
        compiled by GNU C version 11.2.1 20210926, GMP version 6.1.0, MPFR
version 3.1.6, MPC version 1.0.3, isl version isl-0.18-GMP

GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: cd23c6af9953d6c303dd89c0d734e2b7
/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/test-case.cpp: In
function ‘Coro<void> foo()’:
/Users/yves/epymetrics/core-fw/compiler-crash-evaluation/test-case.cpp:72:1:
internal compiler error: in transform_local_var_uses, at cp/coroutines.cc:2102
   72 | }
      | ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://gcc.gnu.org/bugs/> for instructions.
```

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

* [Bug c++/102508] ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102)
  2021-09-27 21:58 [Bug c++/102508] New: ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102) delleyves at gmx dot ch
@ 2021-09-30  9:55 ` marxin at gcc dot gnu.org
  2021-10-21 23:52 ` kacper.slominski72 at gmail dot com
  2021-10-22 20:11 ` iains at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-09-30  9:55 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iains at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-09-30

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

* [Bug c++/102508] ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102)
  2021-09-27 21:58 [Bug c++/102508] New: ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102) delleyves at gmx dot ch
  2021-09-30  9:55 ` [Bug c++/102508] " marxin at gcc dot gnu.org
@ 2021-10-21 23:52 ` kacper.slominski72 at gmail dot com
  2021-10-22 20:11 ` iains at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: kacper.slominski72 at gmail dot com @ 2021-10-21 23:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Kacper Słomiński <kacper.slominski72 at gmail dot com> ---
Was about to report this myself; here is a minimal test case that reproduces
this issue without using any external libraries. It causes a slightly different
ICE in gimplify_expr in gimplify.c:14879 for GCC 11.2, but with small tweaks
you can get the one in transform_local_var_uses as well:

#include <coroutine>

struct coro {
        bool await_ready() { return true; }
        void await_suspend(std::coroutine_handle<>) { }
        int await_resume() { return 0; }

        struct promise_type {
                coro get_return_object() {
                        return {};
                }

                std::suspend_never initial_suspend() {
                        return {};
                }

                std::suspend_never final_suspend() noexcept {
                        return {};
                }

                template <typename T>
                void return_value(T &&) {}

                void unhandled_exception() {}
        };
};

coro fn() { co_return 1; };

coro foo() {
        ({
                auto ex = co_await fn();
                co_return ex;
        });
        co_return 0;
}

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

* [Bug c++/102508] ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102)
  2021-09-27 21:58 [Bug c++/102508] New: ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102) delleyves at gmx dot ch
  2021-09-30  9:55 ` [Bug c++/102508] " marxin at gcc dot gnu.org
  2021-10-21 23:52 ` kacper.slominski72 at gmail dot com
@ 2021-10-22 20:11 ` iains at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: iains at gcc dot gnu.org @ 2021-10-22 20:11 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Iain Sandoe <iains at gcc dot gnu.org> ---
thanks for the simplified test case.
As noted in pr98935, statement expressions are not handled (I deferred handling
them in the initial implementation since they are an extension)... however, it
is intended to support them when possible.

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

end of thread, other threads:[~2021-10-22 20:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-27 21:58 [Bug c++/102508] New: ICE on coroutine when awaiting inside a statement expression (in transform_local_var_uses, at cp/coroutines.cc:2102) delleyves at gmx dot ch
2021-09-30  9:55 ` [Bug c++/102508] " marxin at gcc dot gnu.org
2021-10-21 23:52 ` kacper.slominski72 at gmail dot com
2021-10-22 20:11 ` iains 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).