Skip to content

Incorrect promise constructor used with lambda coroutines #91983

Open
@landelare

Description

@landelare

In the C++20 program below, the lambda has a non-static member operator(), but its object parameter is skipped when looking for a promise constructor overload. This results in the first overload being called, and an output of Wrong.

Tested on 18.1.5 and 19.0.0git (b5af667). When compiled with GCC or MSVC, the output is Correct, as expected.

Additionally, removing the second constructor should make the resulting program no longer compile, which is the case with MSVC and GCC, but not Clang.

#include <coroutine>
#include <iostream>

struct coro {};

struct promise {
    promise(int) {std::cout << "Wrong\n";}
    promise(auto, int){std::cout << "Correct\n";} // Remove me for an additional test case!

    coro get_return_object() {return {};}
    std::suspend_never initial_suspend() {return {};}
    std::suspend_never final_suspend() noexcept {return {};}
    void return_value(int) {}
    void unhandled_exception() {}
};

template<typename... T>
struct std::coroutine_traits<coro, T...> {
    using promise_type = promise;
};

int main() {
    int x = 0;
    [x](int y) -> coro { co_return x + y; }(1);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"coroutinesC++20 coroutineslambdaC++11 lambda expressions

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions