public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter
@ 2021-04-16 22:08 riki--b at hotmail dot it
  2021-04-19  8:15 ` [Bug c++/100127] " iains at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: riki--b at hotmail dot it @ 2021-04-16 22:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 100127
           Summary: [coroutines] internal compiler error compiling promise
                    with custom awaiter
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: riki--b at hotmail dot it
  Target Milestone: ---

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

The following code (preprocessed source in attachments) does not compile and
causes a compiler segfault:

```
#include <coroutine>
#include <optional>

struct future
{
    using value_type = int;
    struct promise_type;
    using handle_type = std::coroutine_handle<promise_type>;

    handle_type _coroutine;

    future(handle_type h) : _coroutine{h} {}

    ~future() noexcept{
        if (_coroutine) {
            _coroutine.destroy();
        }
    }

    value_type get() {
        auto ptr = _coroutine.promise()._value;
        return *ptr;
    }

    struct promise_type {
        std::optional<value_type> _value = std::nullopt;

        future get_return_object() {
            return future{handle_type::from_promise(*this)};
        }
        void return_value(value_type val) {
            _value = static_cast<value_type &&>(val);
        }
        auto initial_suspend() noexcept {
            class awaiter {
                std::optional<value_type> & value;
            public:
                explicit awaiter(std::optional<value_type> & val) noexcept :
value{val} {}
                bool await_ready() noexcept { return value.has_value(); }
                void await_suspend(handle_type) noexcept { }
                value_type & await_resume() noexcept { return *value; }
            };

            return awaiter{_value};
        }
        std::suspend_always final_suspend() noexcept {
            return {};
        }
        void return_void() {}
        void unhandled_exception() {}
    };
};

future create_future()
{ co_return 2021; }

int main()
{ auto f = create_future(); }

```
Returning `std::suspend_never{}` instead of the awaiter compiles correctly.

Output of g++ -v -save-temps -std=c++20 -fcoroutines ${source}.cpp:
```
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --ma
ndir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/ --enable-languag
es=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --
enable-__cxa_atexit --enable-cet=auto --enable-checking=release
--enable-clocale=gnu --enable-default-pi
e --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libibe
rty --enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-thr
eads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror gdc_
include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++2a' '-fcoroutines'
'-shared-libgcc' '-mtune=generic' '-m
arch=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus -E -quiet -v -D_GNU_SOURCE
test_bug.cpp -mtune=generic
-march=x86-64 -std=c++2a -fcoroutines -fpch-preprocess -o test_bug.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/
include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0

/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/x86_64-pc-linux-gnu

/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++2a' '-fcoroutines'
'-shared-libgcc' '-mtune=generic' '-m
arch=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus -fpreprocessed test_bug.ii
-quiet -dumpbase test_bug.cp
p -mtune=generic -march=x86-64 -auxbase test_bug -std=c++2a -version
-fcoroutines -o test_bug.s
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl
version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.1, MPFR version
4.1.0, MPC version 1.2.1, isl
version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: f27fb68878a84470810fd2057eccecc9
test_bug.cpp: In function ‘future create_future()’:
test_bug.cpp:55:19: internal compiler error: Segmentation fault
   55 | { co_return 2021; }
      |                   ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <https://bugs.archlinux.org/> for instructions.
```

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
@ 2021-04-19  8:15 ` iains at gcc dot gnu.org
  2021-04-19 18:55 ` riki--b at hotmail dot it
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2021-04-19  8:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Iain Sandoe <iains at gcc dot gnu.org> ---
I think that this issue is already fixed in the released 10.3 branch and in
master (will shortly become GCC11).  Please could you try one of those?

$ ./gcc-10/gcc-10/bin/g++ -std=c++20 -fcoroutines pr100127.C -S -save-temps
pr100127.C: In function ‘future create_future()’:
pr100127.C:54:8: error: the coroutine promise type
‘std::__n4861::__coroutine_traits_impl<future, void>::promise_type’ {aka
‘future::promise_type’} declares both ‘return_value’ and ‘return_void’
   54 | future create_future()
      |        ^~~~~~~~~~~~~
pr100127.C:49:14: note: ‘return_void’ declared here
   49 |         void return_void() {}
      |              ^~~~~~~~~~~
pr100127.C:31:14: note: ‘return_value’ declared here
   31 |         void return_value(value_type val) {
      |              ^~~~~~~~~~~~

=== 

$ ./gcc-master/gcc-11-0-1/bin/g++ -std=c++20 -fcoroutines pr100127.C -S
-save-temps
pr100127.C: In function ‘future create_future()’:
pr100127.C:54:8: error: the coroutine promise type
‘std::__n4861::__coroutine_traits_impl<future, void>::promise_type’ {aka
‘future::promise_type’} declares both ‘return_value’ and ‘return_void’
   54 | future create_future()
      |        ^~~~~~~~~~~~~
pr100127.C:49:14: note: ‘return_void’ declared here
   49 |         void return_void() {}
      |              ^~~~~~~~~~~
pr100127.C:31:14: note: ‘return_value’ declared here
   31 |         void return_value(value_type val) {
      |              ^~~~~~~~~~~~

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
  2021-04-19  8:15 ` [Bug c++/100127] " iains at gcc dot gnu.org
@ 2021-04-19 18:55 ` riki--b at hotmail dot it
  2021-10-01 20:01 ` iains at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: riki--b at hotmail dot it @ 2021-04-19 18:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Riccardo Brugo <riki--b at hotmail dot it> ---
(In reply to Iain Sandoe from comment #1)
> I think that this issue is already fixed in the released 10.3 branch and in
> master (will shortly become GCC11).  Please could you try one of those?
> 
> $ ./gcc-10/gcc-10/bin/g++ -std=c++20 -fcoroutines pr100127.C -S -save-temps
> pr100127.C: In function ‘future create_future()’:
> pr100127.C:54:8: error: the coroutine promise type
> ‘std::__n4861::__coroutine_traits_impl<future, void>::promise_type’ {aka
> ‘future::promise_type’} declares both ‘return_value’ and ‘return_void’
>    54 | future create_future()
>       |        ^~~~~~~~~~~~~
> pr100127.C:49:14: note: ‘return_void’ declared here
>    49 |         void return_void() {}
>       |              ^~~~~~~~~~~
> pr100127.C:31:14: note: ‘return_value’ declared here
>    31 |         void return_value(value_type val) {
>       |              ^~~~~~~~~~~~
> 
> === 
> 
> $ ./gcc-master/gcc-11-0-1/bin/g++ -std=c++20 -fcoroutines pr100127.C -S
> -save-temps
> pr100127.C: In function ‘future create_future()’:
> pr100127.C:54:8: error: the coroutine promise type
> ‘std::__n4861::__coroutine_traits_impl<future, void>::promise_type’ {aka
> ‘future::promise_type’} declares both ‘return_value’ and ‘return_void’
>    54 | future create_future()
>       |        ^~~~~~~~~~~~~
> pr100127.C:49:14: note: ‘return_void’ declared here
>    49 |         void return_void() {}
>       |              ^~~~~~~~~~~
> pr100127.C:31:14: note: ‘return_value’ declared here
>    31 |         void return_value(value_type val) {
>       |              ^~~~~~~~~~~~

Removing the `return_void` member function will trigger a compilation error in
trunk (https://godbolt.org/z/7vfT89KEx) and a segmentation fault in 10.3
(https://godbolt.org/z/7b37sPbfo)

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
  2021-04-19  8:15 ` [Bug c++/100127] " iains at gcc dot gnu.org
  2021-04-19 18:55 ` riki--b at hotmail dot it
@ 2021-10-01 20:01 ` iains at gcc dot gnu.org
  2021-10-02 15:54 ` iains at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2021-10-01 20:01 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ice-on-valid-code
   Target Milestone|---                         |10.4
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-10-01
             Status|UNCONFIRMED                 |NEW

--- Comment #3 from Iain Sandoe <iains at gcc dot gnu.org> ---
confirmed with the return_void() commented out that there is an ICE with
master:

internal compiler error: tree check: accessed operand 4 of indirect_ref with 1
operands in coro_rewrite_function_body, at cp/coroutines.cc:4220
   55 | { co_return 2021; }

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
                   ` (2 preceding siblings ...)
  2021-10-01 20:01 ` iains at gcc dot gnu.org
@ 2021-10-02 15:54 ` iains at gcc dot gnu.org
  2021-12-17 16:58 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: iains at gcc dot gnu.org @ 2021-10-02 15:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Riccardo Brugo from comment #0)
> Created attachment 50621 [details]

Of course, we should not ICE - but ...

>     struct promise_type {
>         std::optional<value_type> _value = std::nullopt;
> 
>         future get_return_object() {
>             return future{handle_type::from_promise(*this)};
>         }
>         void return_value(value_type val) {
>             _value = static_cast<value_type &&>(val);
>         }
>         auto initial_suspend() noexcept {
>             class awaiter {
>                 std::optional<value_type> & value;
>             public:
>                 explicit awaiter(std::optional<value_type> & val) noexcept :
> value{val} {}
>                 bool await_ready() noexcept { return value.has_value(); }
>                 void await_suspend(handle_type) noexcept { }
>                 value_type & await_resume() noexcept { return *value; }
>             };
> 
>             return awaiter{_value};
>         }

The ICE is caused by the code handling a reference from the co_await - where it
was not expecting any value to be returned from the initial or final
await_resume() functions.

---

How do you expect to use the value returned by value_type & await_resume()
noexcept { return *value; } in this?

see: https://eel.is/c++draft/dcl.fct.def.coroutine#5

essentially the value(s) of the initial and final co_await expressions are
discarded.  So of course, you can have any side-effect you like from the
evaluation of the expressions - but there is no way for you to see the result
of the await_resume().

Ergo it might as well be void...

however, let's fix the ICE anyway.

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
                   ` (3 preceding siblings ...)
  2021-10-02 15:54 ` iains at gcc dot gnu.org
@ 2021-12-17 16:58 ` cvs-commit at gcc dot gnu.org
  2022-06-28 10:44 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-12-17 16:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:2466a8d0dd40d05cb4a239d7d4a21bbd9ffab698

commit r12-6046-g2466a8d0dd40d05cb4a239d7d4a21bbd9ffab698
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Sat Oct 2 17:20:08 2021 +0100

    coroutines: Handle initial awaiters with non-void returns [PR 100127].

    The way in which a C++20 coroutine is specified discards any value
    that might be returned from the initial or final await expressions.

    This ICE was caused by an initial await expression with an
    await_resume () returning a reference, the function rewrite code
    was not set up to expect this.

    Fixed by looking through any indirection present and by explicitly
    discarding the value, if any, returned by await_resume().

    It does not seem useful to make a diagnostic for this, since
    the user could define a generic awaiter that usefully returns
    values when used in a different position from the initial (or
    final) await expressions.

    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>

            PR c++/100127

    gcc/cp/ChangeLog:

            * coroutines.cc (coro_rewrite_function_body): Handle initial
            await expressions that try to produce a reference value.

    gcc/testsuite/ChangeLog:

            * g++.dg/coroutines/pr100127.C: New test.

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
                   ` (4 preceding siblings ...)
  2021-12-17 16:58 ` cvs-commit at gcc dot gnu.org
@ 2022-06-28 10:44 ` jakub at gcc dot gnu.org
  2023-03-03  5:36 ` cvs-commit at gcc dot gnu.org
  2023-07-07  9:36 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-28 10:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |10.5

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 10.4 is being released, retargeting bugs to GCC 10.5.

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
                   ` (5 preceding siblings ...)
  2022-06-28 10:44 ` jakub at gcc dot gnu.org
@ 2023-03-03  5:36 ` cvs-commit at gcc dot gnu.org
  2023-07-07  9:36 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-03  5:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Alexandre Oliva <aoliva@gcc.gnu.org>:

https://gcc.gnu.org/g:1e4122f1159ace52c114c011013adce25172d77b

commit r13-6437-g1e4122f1159ace52c114c011013adce25172d77b
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Fri Mar 3 01:47:00 2023 -0300

    [PR100127] Test for coroutine header in clang-compatible tests

    The test is compatible with clang as well as gcc, but ISTM that
    testing for the __clang__ macro is just as potentially error-prone as
    macros that used to be GCC-specific are now defined in compilers that
    aim for GCC compatibility.  Use a __has_include feature test instead.


    for  gcc/testsuite/ChangeLog

            PR c++/100127
            * g++.dg/coroutines/pr100127.C: Test for header rather than
            compiler macro.
            * g++.dg/coroutines/pr100772-a.C: Likewise.
            * g++.dg/coroutines/pr100772-b.C: Likewise.

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

* [Bug c++/100127] [coroutines] internal compiler error compiling promise with custom awaiter
  2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
                   ` (6 preceding siblings ...)
  2023-03-03  5:36 ` cvs-commit at gcc dot gnu.org
@ 2023-07-07  9:36 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07  9:36 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.5                        |12.0
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED
      Known to fail|                            |10.5.0

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
Fixed in GCC 12, not a regression.

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

end of thread, other threads:[~2023-07-07  9:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-16 22:08 [Bug c++/100127] New: [coroutines] internal compiler error compiling promise with custom awaiter riki--b at hotmail dot it
2021-04-19  8:15 ` [Bug c++/100127] " iains at gcc dot gnu.org
2021-04-19 18:55 ` riki--b at hotmail dot it
2021-10-01 20:01 ` iains at gcc dot gnu.org
2021-10-02 15:54 ` iains at gcc dot gnu.org
2021-12-17 16:58 ` cvs-commit at gcc dot gnu.org
2022-06-28 10:44 ` jakub at gcc dot gnu.org
2023-03-03  5:36 ` cvs-commit at gcc dot gnu.org
2023-07-07  9:36 ` rguenth 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).