public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101244] New: Wrong path in coroutine returning ternary
@ 2021-06-28 13:42 victor.burckel at gmail dot com
  2021-06-28 13:43 ` [Bug c++/101244] " victor.burckel at gmail dot com
  2021-11-12  4:07 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: victor.burckel at gmail dot com @ 2021-06-28 13:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101244
           Summary: Wrong path in coroutine returning ternary
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: victor.burckel at gmail dot com
  Target Milestone: ---

In a coroutine returning a ternary expression with co_await, the co_await part
seems to be evaluated whatever the result of the condition:
See it on godbolt, I added a check of coro.done in the resume operation to
avoid a crash in the clang part as the coroutine is voluntarily resumed one
extra time
https://godbolt.org/z/vzGKoP8d3

#include <fmt/core.h>

#include <coroutine>
#include <functional>

#ifdef __clang__
namespace std::experimental {
using std::coroutine_handle;
using std::coroutine_traits;
}  // namespace std::experimental
#endif

struct task {
  struct promise_type;
  using handle_type = std::coroutine_handle<promise_type>;

  struct promise_type {
    auto initial_suspend() noexcept { return std::suspend_always{}; }
    auto final_suspend() noexcept { return std::suspend_always{}; }
    void return_value(int v) noexcept { value = v; }
    task get_return_object() noexcept {
      return task{handle_type::from_promise(*this)};
    }
    void unhandled_exception() noexcept {}

    int value{};
  };

  task(handle_type h) : h{h} {}
  ~task() {
    if (h) {
      fmt::print("destroying coroutine\n");
      h.destroy();
      fmt::print("coroutine destroyed\n");
    }
  }

  bool await_ready() const noexcept { return false; }
  bool await_suspend(std::coroutine_handle<>) noexcept {
    h.resume();
    return true;
  }
  int await_resume() noexcept { return h.promise().value; }

  void resume() {
    if (!h.done()) h.resume();
  }

  handle_type h;
};

struct S {
  S() { fmt::print("S::S()\n"); }
  S(const S&) { fmt::print("S::S(const S&)\n"); }
  S(S&&) { fmt::print("S::S(S&&)\n"); }
  ~S() { fmt::print("S::~S()\n"); }
};

task g(S) {
  fmt::print("in g()\n");
  co_return 1;
}

task f(bool b) {
  fmt::print("calling g()\n");
  co_return b ? co_await g(S{}) : 2;
}

int main() {
  auto task = f(false);
  fmt::print("resuming f\n");
  task.resume();
  fmt::print("resuming f\n");
  task.resume();
}

Gcc outputs
resuming f
calling g()
S::S()
S::S(S&&)
in g()
resuming f
destroying coroutine
S::~S()
coroutine destroyed
S::~S()
destroying coroutine
coroutine destroyed

While clang outputs
resuming f
calling g()
resuming f
destroying coroutine
coroutine destroyed

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

* [Bug c++/101244] Wrong path in coroutine returning ternary
  2021-06-28 13:42 [Bug c++/101244] New: Wrong path in coroutine returning ternary victor.burckel at gmail dot com
@ 2021-06-28 13:43 ` victor.burckel at gmail dot com
  2021-11-12  4:07 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: victor.burckel at gmail dot com @ 2021-06-28 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Victor Burckel <victor.burckel at gmail dot com> ---
If ternary is not put in the co_return part but split into assignement return,
the compiler crashes
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101149

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

* [Bug c++/101244] Wrong path in coroutine returning ternary
  2021-06-28 13:42 [Bug c++/101244] New: Wrong path in coroutine returning ternary victor.burckel at gmail dot com
  2021-06-28 13:43 ` [Bug c++/101244] " victor.burckel at gmail dot com
@ 2021-11-12  4:07 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-11-12  4:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-11-12
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed on the trunk.  Note if you don't want to use the fmt library in this
case you can replace fmt::print with __builtin_printf as the program here does
not use any special part of the fmt library.

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

end of thread, other threads:[~2021-11-12  4:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-28 13:42 [Bug c++/101244] New: Wrong path in coroutine returning ternary victor.burckel at gmail dot com
2021-06-28 13:43 ` [Bug c++/101244] " victor.burckel at gmail dot com
2021-11-12  4:07 ` 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).