Skip to content

lambda function return type deducing seems to be broken in the newest versions of clang #97680

@Life4gal

Description

@Life4gal

I wrote a piece of code for testing purposes:

#include <algorithm>
#include <functional>
#include <ranges>
#include <vector>

template <typename F>
struct y {
    F f;

    template <typename... Args>
    constexpr auto operator()(Args&&... args) const -> decltype(auto)  //
    {
        #if USE_INVOKE
        return std::invoke(f, *this, std::forward<Args>(args)...);
        #else
        return f(*this, std::forward<Args>(args)...);
        #endif
    }
};

struct list {
    std::vector<list> children;

    int data;
};

struct list_wrapper
{
    list l;
};

int main() {
    struct test {
        int a;
        int b;

        auto operator+(const test& o) const noexcept -> test {
            return {a + o.a, b + o.b};
        }
    };

    auto f1 = y{[](auto self, const list& l) -> test {
        return std::ranges::fold_left(
            l.children, test{.a = l.data % 2, .b = l.data % 4},
            [self](const test& total, const list& nl) -> test {
                return total + self(nl);
            });
    }};

    list_wrapper lw{.l{.data = 42}};
    lw.l.children.emplace_back(std::vector<list>{}, 1337);

#if CALL_F1
    const auto r1 = f1(lw.l);
#endif

    const auto f2 = [f1](const list_wrapper& l) -> test
    {
        return f1(l.l);
    };

    const auto r = f2(lw);

    return 0;
}

The result of the compilation is shown below (you can also check out the godbolt link)
image

I found this error with Clang-CL 17.0.3 on VisualStudio, but testing on godbolt I found that Clang 17.0.1 and Clang 18.1.0 don't have these errors.

Confusingly, in y::operator(), I can't use std::invoke unless I call f1(even though this call theoretically doesn't make any sense for our code).

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"lambdaC++11 lambda expressionsregression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions