public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95540] New: [coroutine] coroutine_traits<> lookup for lambdas
@ 2020-06-04 17:03 bruck.michael at gmail dot com
  2020-06-04 17:24 ` [Bug c++/95540] " bruck.michael at gmail dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: bruck.michael at gmail dot com @ 2020-06-04 17:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95540
           Summary: [coroutine] coroutine_traits<> lookup for lambdas
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bruck.michael at gmail dot com
  Target Milestone: ---

This fails on gcc and clang

gcc: unable to find the promise type for this coroutine
clang: ...coroutine_traits<.., const (lambda at <source>:30:12) &>' has no
member named 'promise_type'

https://gcc.godbolt.org/z/kXzEZW

The clang error hints that the lookup is performed with P1 being the lambda
closure type. I suspect gcc does the same (there is potential for a more
detailed diagnostic here). This makes it impossible to use coroutine_traits<>
with lambdas.

n4835:
[dcl.fct.def.coroutine]
"The promise type of a coroutine is std::coroutine_traits<R, P1, . . . ,
Pn>::promise_type, where R is the return type of the function, and P1 . . . Pn
are the sequence of types of the function parameters, preceded by the type of
the implicit object parameter (12.4.1) if the coroutine is a non-static member
function. The promise type shall be a class type."

While a lambda does have a non-static member operator() the lambda expression
creates a closure object, not a member function. In any case either the
standard or the interpretation of the compilers prevents the use of
coroutine_traits<> for lambdas without a discernible benefit.


#ifndef __clang__
#include <coroutine>
#else
#include <experimental/coroutine>
namespace std { using namespace experimental; }
#endif

#include <cstdio>

struct pt
{
    using handle_t = std::coroutine_handle<pt>;
    auto get_return_object() noexcept { return handle_t::from_promise(*this); }

    std::suspend_never initial_suspend() const noexcept { return {}; }
    std::suspend_never final_suspend() const noexcept { return {}; }
    void return_void() const noexcept {}
    void unhandled_exception() const noexcept {}
};

template <> struct std::coroutine_traits<pt::handle_t>
    { using promise_type = pt; };

static pt::handle_t foo()
{ 
    printf("from function\n");
    co_return;
}

auto bar = []() -> pt::handle_t
{ 
    printf("from lambda\n");
    co_return;
};

int main()
{
    foo();
    bar();
}

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

end of thread, other threads:[~2020-06-11  7:40 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-04 17:03 [Bug c++/95540] New: [coroutine] coroutine_traits<> lookup for lambdas bruck.michael at gmail dot com
2020-06-04 17:24 ` [Bug c++/95540] " bruck.michael at gmail dot com
2020-06-05  7:32 ` iains at gcc dot gnu.org
2020-06-05 15:38 ` bruck.michael at gmail dot com
2020-06-05 15:54 ` iains at gcc dot gnu.org
2020-06-05 18:52 ` bruck.michael at gmail dot com
2020-06-05 19:19 ` iains at gcc dot gnu.org
2020-06-11  0:17 ` iains at gcc dot gnu.org
2020-06-11  7:40 ` bruck.michael at gmail dot com

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